Skip to main content

head_switcher: Flask Single Page App Frontend Packaging Tool

codecov

This package simplifies the process of packaging and loading frontend assets for your headless flask app. In another word, this package install and switch head for your headless flask apps.

Usage

You need to package your single page app (SPA) frontend assets into a .frontend file using our cli tool, and then you can ship the .frontend file with your python packages or simply load it from the file system.

Package frontend

Assuming you are using create-react-app(CRA), and are using the default build configurations.

npm run build
# This will create a `build/` folder for the product build.
build-frontend-pack build/ -o path-to-frontend.frontend

How to ship frontend assets with your python project

The following guide assumes that you are using a similar setup with this tutorial: Packaging Python Projects

And we assume your package is called example_package.

After using the cli tool to package your frontend assets, i.e. build-frontend-pack build/ -o spa_ui.frontend, you can save the spa_ui.frontend file to the following location src/example_package/assets/ui.frontend, and create an empty file src/example_package/assets/__init__.py.

In your pyproject.toml file, under tool.hatch.build section, make sure the artifacts field contains the entry "*.frontend"

[tool.hatch.build]
artifacts = [
    "*.frontend"
]

Now in your application, you can load the front assets using load_from_package_resources('example_package.assets', 'ui.frontend').

from head_switcher import install_to_flask, load_from_package_resources

from flask import Flask

assets = load_from_package_resources('example_package.assets', 'ui.frontend')

app = Flask(__name__)
install_to_flask(assets, app)

Install frontend from a file

If you choose to load frontend from a file, you can do it like this.

from head_switcher import install_to_flask, load_from_file_path

from flask import Flask

assets = load_from_file_path('path-to-frontend.frontend')

app = Flask(__name__)
install_to_flask(assets, app)

Metadata

Release files for head_switcher 0.0.2

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

Source distribution (sdist)

Source distribution for head_switcher 0.0.2
File Size Uploaded
head_switcher-0.0.2.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for head_switcher 0.0.2
File Interpreter ABI Platform
head_switcher-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.7 kB

Release files / head_switcher-0.0.2.tar.gz

Download URL head_switcher-0.0.2.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4c9ef3cc679c4169f01b6e3a5d812c90b1afabee383e1f36f8fd0d45c3e42076
BLAKE2b-256 checksum
How to use checksums
51f7c78635689824bd19f0c1cecfe27500193e709482b6cdb4cfb47a96d769f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.27.0

Release files / head_switcher-0.0.2-py3-none-any.whl

Download URL head_switcher-0.0.2-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8e035051a11874f6ee44c1fd72877747a9618f53c47a97a661a7f82906a2cdc
BLAKE2b-256 checksum
How to use checksums
ba9768dc0237872f470a17e72d0828d3c9676ec9fe39c38cf77f071ecfb94a6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.27.0

Release history Release notifications | RSS feed

This release

0.0.2 This release

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