quart.helpers module¶
-
quart.helpers.
find_package
(name: str) → Tuple[Optional[pathlib.Path], pathlib.Path]¶ Finds packages install prefix (or None) and it’s containing Folder
-
async
quart.helpers.
flash
(message: str, category: str = 'message') → None¶ Add a message (with optional category) to the session store.
This is typically used to flash a message to a user that will be stored in the session and shown during some other request. For example,
@app.route('/login', methods=['POST']) async def login(): ... await flash('Login successful') return redirect(url_for('index'))
allows the index route to show the flashed messsages, without having to accept the message as an argument or otherwise. See
get_flashed_messages()
for message retrieval.
-
quart.helpers.
get_debug_flag
() → bool¶ Reads QUART_DEBUG environment variable to determine whether to run the app in debug mode. If unset, and development mode has been configured, it will be enabled automatically.
-
quart.helpers.
get_env
(default: Optional[str] = 'production') → str¶ Reads QUART_ENV environment variable to determine in which environment the app is running on. Defaults to ‘production’ when unset.
-
quart.helpers.
get_flashed_messages
(with_categories: bool = False, category_filter: List[str] = []) → Union[List[str], List[Tuple[str, str]]]¶ Retrieve the flashed messages stored in the session.
This is mostly useful in templates where it is exposed as a global function, for example
<ul> {% for message in get_flashed_messages() %} <li>{{ message }}</li> {% endfor %} </ul>
Note that caution is required for usage of
category_filter
as all messages will be popped, but only those matching the filter returned. Seeflash()
for message creation.
-
quart.helpers.
get_template_attribute
(template_name: str, attribute: str) → Any¶ Load a attribute from a template.
This is useful in Python code in order to use attributes in templates.
- Parameters
template_name – To load the attribute from.
attribute – The attribute name to load
-
async
quart.helpers.
make_push_promise
(path: str) → None¶ Create a push promise, a simple wrapper function.
This takes a path that should be pushed to the client if the protocol is HTTP/2.
-
async
quart.helpers.
make_response
(*args: Any) → quart.wrappers.response.Response¶ Create a response, a simple wrapper function.
This is most useful when you want to alter a Response before returning it, for example
response = make_response(render_template('index.html')) response.headers['X-Header'] = 'Something'
-
quart.helpers.
stream_with_context
(func: Callable) → Callable¶ Share the current request context with a generator.
This allows the request context to be accessed within a streaming generator, for example,
@app.route('/') def index() -> AsyncGenerator[bytes, None]: @stream_with_context async def generator() -> bytes: yield request.method.encode() yield b' ' yield request.path.encode() return generator()
-
quart.helpers.
url_for
(endpoint: str, *, _anchor: Optional[str] = None, _external: Optional[bool] = None, _method: Optional[str] = None, _scheme: Optional[str] = None, **values: Any) → str¶ Return the url for a specific endpoint.
This is most useful in templates and redirects to create a URL that can be used in the browser.
- Parameters
endpoint – The endpoint to build a url for, if prefixed with
.
it targets endpoint’s in the current blueprint._anchor – Additional anchor text to append (i.e. #text).
_external – Return an absolute url for external (to app) usage.
_method – The method to consider alongside the endpoint.
_scheme – A specific scheme to use.
values – The values to build into the URL, as specified in the endpoint rule.