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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file head_switcher-0.0.2.tar.gz
.
File metadata
- Download URL: head_switcher-0.0.2.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c9ef3cc679c4169f01b6e3a5d812c90b1afabee383e1f36f8fd0d45c3e42076 |
|
MD5 | 7a31ca7b61c5df8519bd0517df543de6 |
|
BLAKE2b-256 | 51f7c78635689824bd19f0c1cecfe27500193e709482b6cdb4cfb47a96d769f7 |
File details
Details for the file head_switcher-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: head_switcher-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8e035051a11874f6ee44c1fd72877747a9618f53c47a97a661a7f82906a2cdc |
|
MD5 | 362035a980852356868688d0b8904d25 |
|
BLAKE2b-256 | ba9768dc0237872f470a17e72d0828d3c9676ec9fe39c38cf77f071ecfb94a6a |