Skip to main content

flask-error-templating

Create Flask HTTP error handlers that use template rendering. This is a very small and simple idea but I couldn't find anything like it so I made it myself.

Installation

Install with pip install flask-error-templating.

Usage

create_http_error_handlers(app, error_pages, page_template_file, **kwargs)

Parameters

app

app is a handle to your Flask object. Need I write more?

error_pages

error_pages is a list of ErrorPage objects. It accepts three arguments: error_code, message and long_message. error_code and message are required; long_message is optional and if it is not present then it will not be rendered into the template. Note that it's possible to have some ErrorPage objects with long_message set and others without.

Example of error_pages:

error_pages = [
    ErrorPage(400, 'Bad request'),
    ErrorPage(400, 'Access is denied to this page.'),
    ErrorPage(403, 'You are forbidden to view this page.',
        'A very long message that we also want to display in the long_message field'),
    ErrorPage(404, 'The page you are looking for does not exist'),
    ErrorPage(418, 'I\'m a teapot!')
]
page_template_file

page_template_file is the filename of a HTML file in your projects templates folder. Parameters supplied to the file for template rendering are error_code, message and long_message. See the above paragraph for information on these parameters. If long_message is not present then an empty string will be rendered in its place - this allows the same template to serve pages with long message and also without.

Example of page_template_file:

<!DOCTYPE html>
<html>
    <body>
        <h1>{{ error_code }}</h1>
        <h2>{{ message }}</h2>
        <br>
        <p>{{ long_message }}</p>
    </body>
</html>
keyword arguments

Often, you will want to pass things like the name of your app to the template when it is being rendered. To allow passing this value, all keyword arguments after page_template_file will be passed to Flask's render_template() function.

Complete basic example:

from flask import *
from flask_error_templating import ErrorPage, create_http_error_handlers

app = Flask(__name__)

@app.route('/')
def homepage():
    return '<h1>Homepage</h1>'

error_pages = [
    ErrorPage(400, 'Bad request'),
    ErrorPage(400, 'Access is denied to this page.'),
    ErrorPage(403, 'You are forbidden to view this page.',
        'A very long message that we also want to display in the long_message field'),
    ErrorPage(404, 'The page you are looking for does not exist'),
    ErrorPage(418, 'I\'m a teapot!')
]
create_http_error_handlers(app, error_pages, 'http_error.html', app_name='Some testing app')

if __name__ == '__main__':
    app.run()

Metadata

Release files for flask-error-templating 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-error-templating 1.1.1
File Size Uploaded
flask-error-templating-1.1.1.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-error-templating 1.1.1
File Interpreter ABI Platform
flask_error_templating-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.1 kB

Release files / flask-error-templating-1.1.1.tar.gz

Download URL flask-error-templating-1.1.1.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
9a907e00a5c55d9efb49e839ba31734f83605b7489456c766f8a5dffce061abc
BLAKE2b-256 checksum
How to use checksums
13be989789be1e6719d79d39cb9d5f6a22e5d1cdf91cd554300f8f901ec4bddf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

Release files / flask_error_templating-1.1.1-py3-none-any.whl

Download URL flask_error_templating-1.1.1-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e6117da9b1636dbd3feb754cb8c3cda088425801e968030ee2f4f40b35a140af
BLAKE2b-256 checksum
How to use checksums
f2e39e9f9cf1d132249b785c3eeca303829e5d6dc038f6223274c23f89804695
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page