-
Notifications
You must be signed in to change notification settings - Fork 1
Home
This is a forked repository. Have a look at
mitsuhiko’s flask wiki
for more information.
- Full custom rules, only if you want.
- Named rules.
- Full control of the static path.
- Optional configuration loading when creating the Flask’s instance.
- Multiple templates dirs.
- Loading of templates from relative paths, absolute paths, packages and egg files.
- The routing is now managed by the Werkzeug’s Map and Rule wrappers, and not by the Flask’s layer.
- Is it possible to name the urls and pass the view function by string or by reference.
- The OPTIONS url parameter is still there, and you can use it also in custom Map rules.
- The “route” decorator is still mantained, but in @app.url_map.route().
- The Flask.add_url_rule() method no longer exist because it is possible to add a url simply by Flask.url_map.add().
- The helper url_for() still work.
- The internal Flask.view_functions no longer exist because the view functions are loaded directly inside the Rules.
- Removed all the Flask’s module system, because it will be possible to create a custom system.
- Loading of the configuration passing it when creating the Flask instance.
- Now the static_root and the static_path are configurable from the config.
I mantained the compatibility as possible, the following are the unique
changes. The tests runs all only replacing the app.route() decorator and the app.add_url_rule() method.
- Replace the decorator @app.route(…) with @app.url_map.route(…).
- Replace the method app.add_url_rule(…) with:
from flask.wrappers import Rule
app.url_map.add(Rule(…))
- Flask “static_path” optional init parameter is moved to the configuration (see next chapter).
- Flask’s module system do not longer exist.
By file:
import Flask
Flask(__name__, 'settings')
By object:
import Flask
import settings
Flask(__name__, settings)
By dictionary:
import Flask
settings = {
'DEBUG': True
'STATIC_PATH': '/media'
}
Flask(__name__, settings)
By environment variable:
import Flask
Flask(__name__, 'FLASK_CONFIG')
In the settings file:
STATIC_PATH = '/media' # browser path
STATIC_ROOT = 'www/media' # directory where found the static files
It is possible to use the app.url_map object or also create a new Map() and
set to it.
Add a view replacing the default Flask’s map with a custom one:
from flask.wrappers import Map, Rule
app.url_map = Map([Rule('/', endpoint='my.package.view.index')])
Add a view by decorator:
from flask.wrappers import Rule
@app.url_map.route('/')
def index():
return 'Hello'
Bind a view by string:
from flask.wrappers import Rule
def index():
return 'Hello'
Rule('/', endpoint='my.package.view.index')
Bind a view by reference:
from flask.wrappers import Rule
def index():
return 'Hello'
Rule('/', endpoint=index)
Bing a view by named reference:
from flask.wrappers import Rule
def index():
return 'Hello'
Rule('/', view_func=index, endpoint='hello index')
Through the configuration it is possible to load templates from additional custom paths.
It is possible to load templates from relative paths, absolute paths, packages and egg files.
Only the packages must have a “templates” directory.
Add some relative and absolute paths, in the settings.py file:
TEMPLATES = (('relative/path', '/home/user/absolute/path'),)
Add some packages:
TEMPLATES = ((), ('my.own.package', 'other.package'))
Or simply both:
TEMPLATES = ('relative/path', '/home/user/absolute/path'), ('my.own.package', 'other.package')
- Documentation not updated, I wait if Armin or others likes the things. Actually the official documentation is this wiki.