Thoughts when I read the flask doc
- what is thread-local, why we choose to use thread-local, its advantages and
disadvantages. - why do we need
app = Flask(__name__), the__name__?
http://flask.pocoo.org/docs/0.10/quickstart/ - build url(generate ulr):
url_for() - Inside templates you also have access to the request, session and g objects
as well as the get_flashed_messages() function. - How that object can be global and how Flask manages to still be threadsafe:
the answer is context locals. after_request()is not guaranreed that the decorated function will run if
an exception is raised, if you want that, please useteardown_request-
what is 'g' object? "This object stores information for one request only and
is available from within each function." -
Standard Context, following variables are available by default. Note that
these will not show up in the context of imported templates. This is
partially caused by performance considerations, partially to keep things
explicit.- config
- request
- session
- g
- url_for
- get_flashed_messages
- Controlling Autoescaping:
- In the Python code, wrap the HTML string in a Markup object before
passing it to the template. This is in general the recommended way. - Inside the template, use the |safe filter to explicitly mark a string
as safe HTML ({{ myvariable|safe }}) - Temporarily disable the autoescape system altogether.
- In the Python code, wrap the HTML string in a Markup object before
- built-in filters and customed filters
- use Context Processors to inject values and functions to the template
- what's the difference between the inject functions and the filters?
- test with
test_clientortest_request_context. Note thatbefore_request
andafter_requestfunctions are not automatically called if you'are using
test_client, usepreprocess_requestandprocess_responseinstead. You
can use a with block to keep the context around for a litter longer. You can
access therequst,gandsessionobjects if you're using
test_request_context. - using
flask.appcontext_pushedsignal to fake resources and context. -
using
session_transactionif you want to modify the session,flask.session
can only access the seesion. -
try send email when there are some errors, try to log to a file.
-
load configuration for flask:
- hardcode it(use uppercase)
from_object()from_envar()from_pyfile()
- what's the difference between
from_envar()andfrom_pyfile()? - Instance Folders is designed to not be under version control and be
deployment specific. It’s the perfect place to drop things that either
change at runtime or configuration files. -
load the default configuration and then overwrite it for production mode.
-
signals: subscriber and sender, decouple
-
core signals:
- flask.template_rendered
- flask.request_started
- flask.requst_finished
- flask.got_request_exception
- flask.requst_tearing_down
- flask.appcontext_tearing_down
- flask.appcontext_pushed
- flask.appcontext_pop
- flask.message_flashed
-
pluggle views: class-based dispatching; method-based dispatching; RESTful API;
-
difference between
before_requestandpreprocess_request -
Application factories: the downside is that you cannot use the application
object in the blueprints at import time. You can however use it from within
a request. -
How ?: call a function from a blueprint when the application is setting up so
that you have a place to modify attributes of the application (like hooking in
before / after request handlers etc.)
Thoughts when I read flask source code
- still can't understand url_defaults and url_value_preprocessor
- what's runserver support mentioned int Flask#run docstring: maybe should be
run - what's the usage for Flask#endpoint
Useful Links
Orgnize your project
- Organizing your project
- Larger Applications
- Building websites in Python with Flask
- Getting bigger with Flask
- How I Structure My Flask Applications