Skip to main content

Help you load frontend asset into your headless flask app without using static folder config.

Project description

head_switcher: Flask Single Page App Frontend Packaging Tool

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)

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

head_switcher-0.0.1.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

head_switcher-0.0.1-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

Supported by

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