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 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_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: Iterable[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

{% for message in get_flashed_messages() %}
  <li>{{ message }}</li>
{% endfor %}

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_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.

  • 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) Union[quart.wrappers.response.Response, werkzeug.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.safe_join(directory: Union[bytes, str, os.PathLike], *paths: Union[bytes, str, os.PathLike]) pathlib.Path

Safely join the paths to the known directory to return a full path.

  • NotFound – if the full path does not share a commonprefix with

  • the directory.

async quart.helpers.send_file(filename_or_io: Union[bytes, str, os.PathLike, _io.BytesIO], mimetype: Optional[str] = None, as_attachment: bool = False, attachment_filename: Optional[str] = None, add_etags: bool = True, cache_timeout: Optional[int] = None, conditional: bool = False, last_modified: Optional[datetime.datetime] = None) quart.wrappers.response.Response

Return a Response to send the filename given.

  • 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: Union[bytes, str, os.PathLike], file_name: str, *, mimetype: Optional[str] = None, as_attachment: bool = False, attachment_filename: Optional[str] = None, add_etags: bool = True, cache_timeout: Optional[int] = None, conditional: bool = True, last_modified: Optional[datetime.datetime] = None) quart.wrappers.response.Response

Send a file from a given directory.

  • 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,

def index() -> AsyncGenerator[bytes, None]:
    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.

  • 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.