Skip to content
Myr

rewrite Scaffold docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 9f7c602 · Browse files · 3 files+208 added−169 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+208 added−169 removed

  • other:+208 added−169 removed

3 files+208 added−169 removed

modified:src/flask/app.py+3 added-69 removed
344 unmodified lines
345 #:346 #: .. versionadded:: 0.8347 session_interface = SecureCookieSessionInterface()348349 # TODO remove the next three attrs when Sphinx :inherited-members: works350 # https://github.com/sphinx-doc/sphinx/issues/741351352 #: The name of the package or module that this app belongs to. Do not353 #: change this once it is set by the constructor.354 import_name = None355356 #: Location of the template files to be added to the template lookup.357 #: ``None`` if templates should not be added.358 template_folder = None359360 #: Absolute path to the package on the filesystem. Used to look up361 #: resources contained in the package.362 root_path = None363349 def __init__(350 self,351 import_name,352 static_url_path=None,
648 unmodified lines
1001 view_func=None,1002 provide_automatic_options=None,1003 **options,1004 ):1020 """Connects a URL rule. Works exactly like the :meth:`route`1021 decorator. If a view_func is provided it will be registered with the1022 endpoint.10231024 Basically this example::10251026 @app.route('/')1027 def index():1028 pass10291030 Is equivalent to the following::10311032 def index():1033 pass1034 app.add_url_rule('/', 'index', index)10351036 If the view_func is not provided you will need to connect the endpoint1037 to a view function like so::10381039 app.view_functions['index'] = index10401041 Internally :meth:`route` invokes :meth:`add_url_rule` so if you want1042 to customize the behavior via subclassing you only need to change1043 this method.10441045 For more information refer to :ref:`url-route-registrations`.10461047 .. versionchanged:: 0.21048 `view_func` parameter added.10491050 .. versionchanged:: 0.61051 ``OPTIONS`` is added automatically as method.10521053 :param rule: the URL rule as string1054 :param endpoint: the endpoint for the registered URL rule. Flask1055 itself assumes the name of the view function as1056 endpoint1057 :param view_func: the function to call when serving a request to the1058 provided endpoint1059 :param provide_automatic_options: controls whether the ``OPTIONS``1060 method should be added automatically. This can also be controlled1061 by setting the ``view_func.provide_automatic_options = False``1062 before adding the rule.1063 :param options: the options to be forwarded to the underlying1064 :class:`~werkzeug.routing.Rule` object. A change1065 to Werkzeug is handling of method options. methods1066 is a list of methods this rule should be limited1067 to (``GET``, ``POST`` etc.). By default a rule1068 just listens for ``GET`` (and implicitly ``HEAD``).1069 Starting with Flask 0.6, ``OPTIONS`` is implicitly1070 added and handled by the standard request handling.1071 """1005 if endpoint is None:1006 endpoint = _endpoint_from_view_func(view_func)1007 options["endpoint"] = endpoint1008 methods = options.pop("methods", None)
946 unmodified lines
1955 ctx.auto_pop(error)19561957 def __call__(self, environ, start_response):1958 """The WSGI server calls the Flask application object as the2026 WSGI application. This calls :meth:`wsgi_app` which can be2027 wrapped to applying middleware."""1959 WSGI application. This calls :meth:`wsgi_app`, which can be1960 wrapped to apply middleware.1961 """1962 return self.wsgi_app(environ, start_response)
modified:src/flask/blueprints.py+4 added-19 removed
128 unmodified lines
129130 warn_on_modifications = False131 _got_registered_once = False132133 #: Blueprint local JSON encoder class to use.134 #: Set to ``None`` to use the app's :class:`~flask.app.Flask.json_encoder`.133 #: Blueprint local JSON encoder class to use. Set to ``None`` to use134 #: the app's :class:`~flask.Flask.json_encoder`.135 json_encoder = None136 #: Blueprint local JSON decoder class to use.137 #: Set to ``None`` to use the app's :class:`~flask.app.Flask.json_decoder`.136 #: Blueprint local JSON decoder class to use. Set to ``None`` to use137 #: the app's :class:`~flask.Flask.json_decoder`.138 json_decoder = None139140 # TODO remove the next three attrs when Sphinx :inherited-members: works141 # https://github.com/sphinx-doc/sphinx/issues/741142143 #: The name of the package or module that this app belongs to. Do not144 #: change this once it is set by the constructor.145 import_name = None146147 #: Location of the template files to be added to the template lookup.148 #: ``None`` if templates should not be added.149 template_folder = None150151 #: Absolute path to the package on the filesystem. Used to look up152 #: resources contained in the package.153 root_path = None154140 def __init__(141 self,142 name,143 import_name,
307 unmodified lines
modified:src/flask/scaffold.py+201 added-81 removed
1import importlib.util2import os3import pkgutil4import sys5from collections import defaultdict
35 unmodified lines
41 return update_wrapper(wrapper_func, f)424344class Scaffold:44 """A common base for :class:`~flask.app.Flask` and45 """Common behavior shared between :class:`~flask.Flask` and46 :class:`~flask.blueprints.Blueprint`.4748 :param import_name: The import name of the module where this object49 is defined. Usually :attr:`__name__` should be used.50 :param static_folder: Path to a folder of static files to serve.51 If this is set, a static route will be added.52 :param static_url_path: URL prefix for the static route.53 :param template_folder: Path to a folder containing template files.54 for rendering. If this is set, a Jinja loader will be added.55 :param root_path: The path that static, template, and resource files56 are relative to. Typically not set, it is discovered based on57 the ``import_name``.5859 .. versionadded:: 2.0.060 """6162 name: str63 _static_folder = None64 _static_url_path = None6552 #: Skeleton local JSON decoder class to use.53 #: Set to ``None`` to use the app's :class:`~flask.app.Flask.json_encoder`.66 #: JSON encoder class used by :func:`flask.json.dumps`. If a67 #: blueprint sets this, it will be used instead of the app's value.68 json_encoder = None6956 #: Skeleton local JSON decoder class to use.57 #: Set to ``None`` to use the app's :class:`~flask.app.Flask.json_decoder`.70 #: JSON decoder class used by :func:`flask.json.loads`. If a71 #: blueprint sets this, it will be used instead of the app's value.72 json_decoder = None7374 def __init__(75 self,76 import_name,63 static_folder="static",77 static_folder=None,78 static_url_path=None,79 template_folder=None,80 root_path=None,81 ):
21 unmodified lines
103 #: once the application has been discovered and blueprints have104 #: been registered.105 self.cli = AppGroup()10693 #: A dictionary of all view functions registered. The keys will94 #: be function names which are also used to generate URLs and95 #: the values are the function objects themselves.107 #: A dictionary mapping endpoint names to view functions.108 #:109 #: To register a view function, use the :meth:`route` decorator.110 #:111 #: This data structure is internal. It should not be modified112 #: directly and its format may change at any time.113 self.view_functions = {}11499 #: A dictionary of all registered error handlers. The key is ``None``100 #: for error handlers active on the application, otherwise the key is101 #: the name of the blueprint. Each key points to another dictionary102 #: where the key is the status code of the http exception. The103 #: special key ``None`` points to a list of tuples where the first item104 #: is the class for the instance check and the second the error handler105 #: function.115 #: A data structure of registered error handlers, in the format116 #: ``{scope: {code: {class: handler}}}```. The ``scope`` key is117 #: the name of a blueprint the handlers are active for, or118 #: ``None`` for all requests. The ``code`` key is the HTTP119 #: status code for ``HTTPException``, or ``None`` for120 #: other exceptions. The innermost dictionary maps exception121 #: classes to handler functions.122 #:123 #: To register an error handler, use the :meth:`errorhandler`124 #: decorator.125 #:126 #: This data structure is internal. It should not be modified127 #: directly and its format may change at any time.128 self.error_handler_spec = defaultdict(lambda: defaultdict(dict))129111 #: A dictionary with lists of functions that will be called at the112 #: beginning of each request. The key of the dictionary is the name of113 #: the blueprint this function is active for, or ``None`` for all114 #: requests. To register a function, use the :meth:`before_request`130 #: A data structure of functions to call at the beginning of131 #: each request, in the format ``{scope: [functions]}``. The132 #: ``scope`` key is the name of a blueprint the functions are133 #: active for, or ``None`` for all requests.134 #:135 #: To register a function, use the :meth:`before_request`136 #: decorator.137 #:138 #: This data structure is internal. It should not be modified139 #: directly and its format may change at any time.140 self.before_request_funcs = defaultdict(list)141118 #: A dictionary with lists of functions that should be called after119 #: each request. The key of the dictionary is the name of the blueprint120 #: this function is active for, ``None`` for all requests. This can for121 #: example be used to close database connections. To register a function122 #: here, use the :meth:`after_request` decorator.142 #: A data structure of functions to call at the end of each143 #: request, in the format ``{scope: [functions]}``. The144 #: ``scope`` key is the name of a blueprint the functions are145 #: active for, or ``None`` for all requests.146 #:147 #: To register a function, use the :meth:`after_request`148 #: decorator.149 #:150 #: This data structure is internal. It should not be modified151 #: directly and its format may change at any time.152 self.after_request_funcs = defaultdict(list)153125 #: A dictionary with lists of functions that are called after126 #: each request, even if an exception has occurred. The key of the127 #: dictionary is the name of the blueprint this function is active for,128 #: ``None`` for all requests. These functions are not allowed to modify129 #: the request, and their return values are ignored. If an exception130 #: occurred while processing the request, it gets passed to each131 #: teardown_request function. To register a function here, use the132 #: :meth:`teardown_request` decorator.154 #: A data structure of functions to call at the end of each155 #: request even if an exception is raised, in the format156 #: ``{scope: [functions]}``. The ``scope`` key is the name of a157 #: blueprint the functions are active for, or ``None`` for all158 #: requests.159 #:134 #: .. versionadded:: 0.7160 #: To register a function, use the :meth:`teardown_request`161 #: decorator.162 #:163 #: This data structure is internal. It should not be modified164 #: directly and its format may change at any time.165 self.teardown_request_funcs = defaultdict(list)166137 #: A dictionary with list of functions that are called without argument138 #: to populate the template context. The key of the dictionary is the139 #: name of the blueprint this function is active for, ``None`` for all140 #: requests. Each returns a dictionary that the template context is141 #: updated with. To register a function here, use the142 #: :meth:`context_processor` decorator.167 #: A data structure of functions to call to pass extra context168 #: values when rendering templates, in the format169 #: ``{scope: [functions]}``. The ``scope`` key is the name of a170 #: blueprint the functions are active for, or ``None`` for all171 #: requests.172 #:173 #: To register a function, use the :meth:`context_processor`174 #: decorator.175 #:176 #: This data structure is internal. It should not be modified177 #: directly and its format may change at any time.178 self.template_context_processors = defaultdict(179 list, {None: [_default_template_ctx_processor]}180 )181147 #: A dictionary with lists of functions that are called before the148 #: :attr:`before_request_funcs` functions. The key of the dictionary is149 #: the name of the blueprint this function is active for, or ``None``150 #: for all requests. To register a function, use151 #: :meth:`url_value_preprocessor`.182 #: A data structure of functions to call to modify the keyword183 #: arguments passed to the view function, in the format184 #: ``{scope: [functions]}``. The ``scope`` key is the name of a185 #: blueprint the functions are active for, or ``None`` for all186 #: requests.187 #:153 #: .. versionadded:: 0.7188 #: To register a function, use the189 #: :meth:`url_value_preprocessor` decorator.190 #:191 #: This data structure is internal. It should not be modified192 #: directly and its format may change at any time.193 self.url_value_preprocessors = defaultdict(list)194156 #: A dictionary with lists of functions that can be used as URL value157 #: preprocessors. The key ``None`` here is used for application wide158 #: callbacks, otherwise the key is the name of the blueprint.159 #: Each of these functions has the chance to modify the dictionary160 #: of URL values before they are used as the keyword arguments of the161 #: view function. For each function registered this one should also162 #: provide a :meth:`url_defaults` function that adds the parameters163 #: automatically again that were removed that way.195 #: A data structure of functions to call to modify the keyword196 #: arguments when generating URLs, in the format197 #: ``{scope: [functions]}``. The ``scope`` key is the name of a198 #: blueprint the functions are active for, or ``None`` for all199 #: requests.200 #:165 #: .. versionadded:: 0.7201 #: To register a function, use the :meth:`url_defaults`202 #: decorator.203 #:204 #: This data structure is internal. It should not be modified205 #: directly and its format may change at any time.206 self.url_default_functions = defaultdict(list)207208 def __repr__(self):209 return f"<{type(self).__name__} {self.name!r}>"
157 unmodified lines
367 """368 return self._method_route("PATCH", rule, options)369370 def route(self, rule, **options):331 """A decorator that is used to register a view function for a332 given URL rule. This does the same thing as :meth:`add_url_rule`333 but is used as a decorator. See :meth:`add_url_rule` and334 :ref:`url-route-registrations` for more information.371 """Decorate a view function to register it with the given URL372 rule and options. Calls :meth:`add_url_rule`, which has more373 details about the implementation.374375 .. code-block:: python376377 @app.route("/")378 def index():340 return "Hello World"379 return "Hello, World!"380381 See :ref:`url-route-registrations`.382383 The endpoint name for the route defaults to the name of the view384 function if the ``endpoint`` parameter isn't passed.385386 The ``methods`` parameter defaults to ``["GET"]``. ``HEAD`` and387 ``OPTIONS`` are added automatically.388342 :param rule: The URL rule as a string.343 :param options: The options to be forwarded to the underlying389 :param rule: The URL rule string.390 :param options: Extra options passed to the391 :class:`~werkzeug.routing.Rule` object.392 """393394 def decorator(f):
11 unmodified lines
406 view_func=None,407 provide_automatic_options=None,408 **options,409 ):410 """Register a rule for routing incoming requests and building411 URLs. The :meth:`route` decorator is a shortcut to call this412 with the ``view_func`` argument. These are equivalent:413414 .. code-block:: python415416 @app.route("/")417 def index():418 ...419420 .. code-block:: python421422 def index():423 ...424425 app.add_url_rule("/", view_func=index)426427 See :ref:`url-route-registrations`.428429 The endpoint name for the route defaults to the name of the view430 function if the ``endpoint`` parameter isn't passed. An error431 will be raised if a function has already been registered for the432 endpoint.433434 The ``methods`` parameter defaults to ``["GET"]``. ``HEAD`` is435 always added automatically, and ``OPTIONS`` is added436 automatically by default.437438 ``view_func`` does not necessarily need to be passed, but if the439 rule should participate in routing an endpoint name must be440 associated with a view function at some point with the441 :meth:`endpoint` decorator.442443 .. code-block:: python444445 app.add_url_rule("/", endpoint="index")446447 @app.endpoint("index")448 def index():449 ...450451 If ``view_func`` has a ``required_methods`` attribute, those452 methods are added to the passed and automatic methods. If it453 has a ``provide_automatic_methods`` attribute, it is used as the454 default if the parameter is not passed.455456 :param rule: The URL rule string.457 :param endpoint: The endpoint name to associate with the rule458 and view function. Used when routing and building URLs.459 Defaults to ``view_func.__name__``.460 :param view_func: The view function to associate with the461 endpoint name.462 :param provide_automatic_options: Add the ``OPTIONS`` method and463 respond to ``OPTIONS`` requests automatically.464 :param options: Extra options passed to the465 :class:`~werkzeug.routing.Rule` object.466 """467 raise NotImplementedError468469 def endpoint(self, endpoint):366 """A decorator to register a function as an endpoint.367 Example::470 """Decorate a view function to register it for the given471 endpoint. Used if a rule is added without a ``view_func`` with472 :meth:`add_url_rule`.473369 @app.endpoint('example.endpoint')474 .. code-block:: python475476 app.add_url_rule("/ex", endpoint="example")477478 @app.endpoint("example")479 def example():371 return "example"480 ...481373 :param endpoint: the name of the endpoint482 :param endpoint: The endpoint name to associate with the view483 function.484 """485486 def decorator(f):487 self.view_functions[endpoint] = f
2 unmodified lines
490 return decorator491492 @setupmethod493 def before_request(self, f):384 """Registers a function to run before each request.494 """Register a function to run before each request.495386 For example, this can be used to open a database connection, or to load387 the logged in user from the session.496 For example, this can be used to open a database connection, or497 to load the logged in user from the session.498389 The function will be called without any arguments. If it returns a390 non-None value, the value is handled as if it was the return value from391 the view, and further request handling is stopped.499 .. code-block:: python500501 @app.before_request502 def load_user():503 if "user_id" in session:504 g.user = db.session.get(session["user_id"])505506 The function will be called without any arguments. If it returns507 a non-``None`` value, the value is handled as if it was the508 return value from the view, and further request handling is509 stopped.510 """511 self.before_request_funcs[None].append(f)512 return f513514 @setupmethod515 def after_request(self, f):398 """Register a function to be run after each request.516 """Register a function to run after each request to this object.517400 Your function must take one parameter, an instance of401 :attr:`response_class` and return a new response object or the402 same (see :meth:`process_response`).518 The function is called with the response object, and must return519 a response object. This allows the functions to modify or520 replace the response before it is sent.521404 As of Flask 0.7 this function might not be executed at the end of the405 request in case an unhandled exception occurred.522 If a function raises an exception, any remaining523 ``after_request`` functions will not be called. Therefore, this524 should not be used for actions that must execute, such as to525 close resources. Use :meth:`teardown_request` for that.526 """527 self.after_request_funcs[None].append(f)528 return f529
15 unmodified lines
545 functions are called just before the request context moves from the546 stack of active contexts. This becomes relevant if you are using547 such constructs in tests.548429 Generally teardown functions must take every necessary step to avoid430 that they will fail. If they do execute code that might fail they549 Teardown functions must avoid raising exceptions, since they . If they550 execute code that might fail they551 will have to surround the execution of these code by try/except552 statements and log occurring errors.553554 When a teardown function was called because of an exception it will
316 unmodified lines