Skip to main content

Add your description here

Project description

A "GOV.UK Frontend" theme for Flask-Admin

This package is designed to make it trivial to re-skin a Flask-Admin extension to have a consistent look and feel to many UK Government services, by using layouts, components and patterns from the great GOV.UK Design System (among other x-gov resources).

Demo

Image

How to integrate with Flask-Admin

Make sure your Flask app's Jinja environment is configured to load templates from at least the sources listed below. Then initialise Flask-Admin and xgovuk-flask-admin appropriately, making sure to pass the GOV.UK Frontend Theme to Flask-Admin.

from flask import Flask
from jinja2 import PackageLoader, ChoiceLoader, PrefixLoader

from flask_admin import Admin
from xgovuk_flask_admin import XGovukFlaskAdmin, XGovukFrontendTheme

app = Flask(...)
app.jinja_options = {
    "loader": ChoiceLoader(
        [
            PrefixLoader({"govuk_frontend_jinja": PackageLoader("govuk_frontend_jinja")}),
            PrefixLoader({"govuk_frontend_wtf": PackageLoader("govuk_frontend_wtf")}),
            PackageLoader("xgovuk_flask_admin"),
        ]
    )
}

admin = Admin(app, theme=XGovukFrontendTheme())
xgovuk_flask_admin = XGovukFlaskAdmin(app)

Writing model views

All of your SQLAlchemy model fields should derive from xgovuk-flask-admin's XGovukModelView, not from Flask-Admin's sqla.ModelView.

To auto-generate CRUD pages for SQLAlchemy DB models, write sub-classes of XGovukModelView (which itself derives from Flask-Admin's sqla.ModelView):

from xgovuk_flask_admin import XGovukModelView

class UserModelView(XGovukModelView):
    page_size = 15
    can_create = True
    can_edit = True
    can_set_page_size = True
    page_size_options = [10, 15, 25, 50]

    form_args = {"email": {"validators": [Email()]}}

    column_filters = [
        "age",
        "job",
        "email",
        "created_at",
        "favourite_colour",
        "last_logged_in_at",
    ]

    column_searchable_list = ["email", "name"]

    can_export = True
    export_types = ["csv"]

    column_descriptions = {
        "age": "User's age in years",
        "email": "Email address for contacting the user",
        "created_at": "Date the user account was created",
        "last_logged_in_at": "Date and time of the user's last login",
    }

And then attach these model views to the Flask-Admin instance:

    flask_admin.add_view(UserModelView(User, db.session))

Writing custom views

Create a new Jinja2 template that extends from "admin/master.html" (which is a proxy for admin/base.html). Generally all you should need to implement is the action_panel block, like so:

{% block action_panel %}
<div class="govuk-grid-row">
  <div class="govuk-grid-column-two-thirds">
    ... your HTML here ...
  </div>
</div>
{% endblock %}

Recognition / attribution

This extension / theme has only been made possible by standing on a huge corpus of existing work across government, including but not limited to:

And, of course, Flask-Admin itself.

Compatibility

Python

xgovuk-flask-admin will aim to be compatible with all supported versions of Python released from 2025, but this package was put together in 2025 and I'm placing a lower bound of support on Python 3.12.

Flask-Admin

Not all features of Flask-Admin are currently supported, or likely to be supported in the future.

Feature Support status Notes
Nested service navigation menu items 🔮 Support Flask-Admin's "menu category" better, probably JS-only
Create new records
List existing records
Keyword search when listing records
Apply filters when listing records
Sort results
Changing page size when listing results
Custom page sizes
Performing bulk actions on results
Editing record fields when listing results 🤔 Flask-Admin allows you to edit columns from the "list model" view; I'm not sure we want/need to support for this XGovuk-Flask-Admin when taking into account design+accessibility factors
View existing records
Edit existing records
Delete existing records
Edit relationships
Optimise/support 'large' relationships 🔮 If a related table is large, performance will degrade rapidly because every related record is a choice in the select/dropdown menu. A future approach might be to dynamically load results from an API call
Edit date+datetime fields
Edit one-to-one relationships
Edit one-to-many relationships
Standardised default GOV.UK error messages 🔮 It would be nice to have standard error messages for common validations, eg email format or dates
Show full related model forms inline 🤔 Flask-Admin allows rendering the full form of a related model inline of the main model (eg forms to edit posts by a user when editing a user). It might be nice to support this; some design thinking to do

Please raise an issue in this repository if you need functionality from Flask-Admin that isn't supported yet.

GOV.UK Frontend

xgovuk-flask-admin is packaged with compiled static assets that are served directly by the extension. This is a technical constraint applied because govuk-frontend-jinja and govuk-frontend-wtf are themselves designed for specific versions of GOV.UK Frontend.

Since xgovuk-flask-admin compiles assets from GOV.UK Frontend (and other x-gov frontend components), it will be locked to specific versions of govuk-frontend-jinja and govuk-frontend-wtf.

It may be possible to compile bundles for various versions of GOV.UK Frontend, in order to support various combinations of govuk-frontend-jinja and govuk-frontend-wtf in the future, but this is not currently planned. Please comment on https://github.com/samuelhwilliams/xgovuk-flask-admin/issues/1 if this would be useful.

The latest release of xgovuk-flask-admin will generally track the latest release of GOV.UK Frontend, govuk-frontend-jinja, and govuk-frontend-wtf.

Package Currently supported version
GOV.UK Frontend 5.12.0
govuk-frontend-jinja 3.8.0
govuk-frontend-wtf 3.2.0

xgovuk-flask-admin uses govuk-colour("purple") as its brand colour on the (somewhat unfounded/unresearched) belief that this isn't a colour used for branding in many places, so it's broadly "available". Given it will be unfamiliar, it visually signifies that you're in an unusual (read: privileged) interface. Because we pre-compile assets, we need to choose some colour, and I wanted to avoid black, GOV.UK blue, or common brands. Please comment on https://github. com/samuelhwilliams/xgovuk-flask-admin/issues/7 if customisation/extension is needed.

Because this theme uses common, well-tested components from across UK Government, it should be reasonably accessible out of the box and will generally be progressively enhanced (ie functional without javascript). However, some components used in xgovuk-flask-admin are not 'officially' fully supported or fully accessible, and xgovuk-flask-admin applies some customisation to styling and interactions on some components in order to work with Flask-Admin and provide a (hopefully) more usable design and experience.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xgovuk_flask_admin-0.0.2.tar.gz (447.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xgovuk_flask_admin-0.0.2-py3-none-any.whl (454.9 kB view details)

Uploaded Python 3

File details

Details for the file xgovuk_flask_admin-0.0.2.tar.gz.

File metadata

  • Download URL: xgovuk_flask_admin-0.0.2.tar.gz
  • Upload date:
  • Size: 447.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xgovuk_flask_admin-0.0.2.tar.gz
Algorithm Hash digest
SHA256 9738345f7692530f65de249f3104b86a2c9d45ea5099c4200ec7d2f2320de00a
MD5 9a45f5b7c910453352270945a42089b1
BLAKE2b-256 9ca57480707ee7fdb6f3ee2547622e41883a6720326d947765c143b1514e2157

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgovuk_flask_admin-0.0.2.tar.gz:

Publisher: publish.yml on samuelhwilliams/xgovuk-flask-admin

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xgovuk_flask_admin-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for xgovuk_flask_admin-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e49978c2b7c9e97b6465e0a20fccbe10db22048af3205eeaeb7265d2f15ff95a
MD5 010122773ba1d381d30cafa029cf2f54
BLAKE2b-256 99b75c16d16887b04f8659c7730cd1092533b023ff3d9b745123ff5284a3c033

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgovuk_flask_admin-0.0.2-py3-none-any.whl:

Publisher: publish.yml on samuelhwilliams/xgovuk-flask-admin

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page