quart.helpers module#

quart.helpers.abort(code: int | Response, *args: Any, **kwargs: Any) NoReturn#

Raise an HTTPException for the given status code.

quart.helpers.find_package(name: str) tuple[Path | None, 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 messages, 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_flashed_messages(with_categories: bool = False, category_filter: Iterable[str] = ()) 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. See flash() for message creation.

quart.helpers.get_load_dotenv(default: bool = True) bool#

Get whether the user has disabled loading default dotenv files by setting QUART_SKIP_DOTENV. The default is True, load the files. :param default: What to return if the env var isn’t set.

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) 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.redirect(location: str, code: int = 302) Response#

Redirect to the location with the status code.

async quart.helpers.send_file(filename_or_io: bytes | str | PathLike | BytesIO, mimetype: str | None = None, as_attachment: bool = False, attachment_filename: str | None = None, add_etags: bool = True, cache_timeout: int | None = None, conditional: bool = False, last_modified: datetime | None = None) Response#

Return a Response to send the filename given.

Parameters:
  • filename_or_io – The filename (path) to send, remember to use safe_join().

  • mimetype – Mimetype to use, by default it will be guessed or revert to the DEFAULT_MIMETYPE.

  • as_attachment – If true use the attachment filename in a Content-Disposition attachment header.

  • attachment_filename – Name for the filename, if it differs

  • add_etags – Set etags based on the filename, size and modification time.

  • last_modified – Used to override the last modified value.

  • cache_timeout – Time in seconds for the response to be cached.

async quart.helpers.send_from_directory(directory: bytes | str | PathLike, file_name: str, *, mimetype: str | None = None, as_attachment: bool = False, attachment_filename: str | None = None, add_etags: bool = True, cache_timeout: int | None = None, conditional: bool = True, last_modified: datetime | None = None) Response#

Send a file from a given directory.

Parameters:
  • directory – Directory that when combined with file_name gives the file path.

  • file_name – File name that when combined with directory gives the file path.

See send_file() for the other arguments.

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: str | None = None, _external: bool | None = None, _method: str | None = None, _scheme: str | None = 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.