quart.helpers module#

quart.helpers.get_debug_flag()#

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.

Return type:

bool

quart.helpers.get_load_dotenv(default=True)#

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.

Parameters:

default (bool)

Return type:

bool

async quart.helpers.make_response(*args)#

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'
Parameters:

args (Any)

Return type:

Response | Response

async quart.helpers.make_push_promise(path)#

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.

Parameters:

path (str)

Return type:

None

async quart.helpers.flash(message, category='message')#

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.

Parameters:
  • message (str)

  • category (str)

Return type:

None

quart.helpers.get_flashed_messages(with_categories=False, category_filter=())#

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.

Parameters:
  • with_categories (bool)

  • category_filter (Iterable[str])

Return type:

list[str] | list[tuple[str, str]]

quart.helpers.get_template_attribute(template_name, attribute)#

Load a attribute from a template.

This is useful in Python code in order to use attributes in templates.

Parameters:
  • template_name (str) – To load the attribute from.

  • attribute (str) – The attribute name to load

Return type:

Any

quart.helpers.url_for(endpoint, *, _anchor=None, _external=None, _method=None, _scheme=None, **values)#

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 (str) – The endpoint to build a url for, if prefixed with . it targets endpoint’s in the current blueprint.

  • _anchor (str | None) – Additional anchor text to append (i.e. #text).

  • _external (bool | None) – Return an absolute url for external (to app) usage.

  • _method (str | None) – The method to consider alongside the endpoint.

  • _scheme (str | None) – A specific scheme to use.

  • values (Any) – The values to build into the URL, as specified in the endpoint rule.

Return type:

str

quart.helpers.stream_with_context(func)#

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()
Parameters:

func (Callable)

Return type:

Callable

quart.helpers.find_package(name)#

Finds packages install prefix (or None) and it’s containing Folder

Parameters:

name (str)

Return type:

tuple[Path | None, Path]

async quart.helpers.send_from_directory(directory, file_name, *, mimetype=None, as_attachment=False, attachment_filename=None, add_etags=True, cache_timeout=None, conditional=True, last_modified=None)#

Send a file from a given directory.

Parameters:
  • directory (bytes | str | PathLike) – Directory that when combined with file_name gives the file path.

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

  • mimetype (str | None)

  • as_attachment (bool)

  • attachment_filename (str | None)

  • add_etags (bool)

  • cache_timeout (int | None)

  • conditional (bool)

  • last_modified (datetime | None)

Return type:

Response

See send_file() for the other arguments.

async quart.helpers.send_file(filename_or_io, mimetype=None, as_attachment=False, attachment_filename=None, add_etags=True, cache_timeout=None, conditional=False, last_modified=None)#

Return a Response to send the filename given.

Parameters:
  • filename_or_io (bytes | str | PathLike | BytesIO) – The filename (path) to send, remember to use safe_join().

  • mimetype (str | None) – Mimetype to use, by default it will be guessed or revert to the DEFAULT_MIMETYPE.

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

  • attachment_filename (str | None) – Name for the filename, if it differs

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

  • last_modified (datetime | None) – Used to override the last modified value.

  • cache_timeout (int | None) – Time in seconds for the response to be cached.

  • conditional (bool)

Return type:

Response

quart.helpers.abort(code, *args, **kwargs)#

Raise an HTTPException for the given status code.

Parameters:
  • code (int | Response)

  • args (Any)

  • kwargs (Any)

Return type:

NoReturn

quart.helpers.redirect(location, code=302)#

Redirect to the location with the status code.

Parameters:
  • location (str)

  • code (int)

Return type:

Response