Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bezborodow/flask-blueprints
Register all Flask Blueprints (including nested) from a package directory.
https://github.com/bezborodow/flask-blueprints
autoimport flask flask-blueprints importlib
Last synced: 18 days ago
JSON representation
Register all Flask Blueprints (including nested) from a package directory.
- Host: GitHub
- URL: https://github.com/bezborodow/flask-blueprints
- Owner: bezborodow
- License: bsd-3-clause
- Created: 2023-07-21T11:41:54.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-03-04T06:44:42.000Z (8 months ago)
- Last Synced: 2024-07-07T01:43:07.201Z (4 months ago)
- Topics: autoimport, flask, flask-blueprints, importlib
- Language: Python
- Homepage: https://pypi.org/project/flask-blueprints/
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Flask Blueprint Autoimporter
**`flask-blueprints`** will import and register all nested Flask Blueprints under a given package directory.
## Synopsis
Register all Flask Blueprints (including nested) from a package directory.
## Usage
Create an empty package `app.blueprints` and register your blueprints:
```python
from flask import Flask
from flask_blueprints import register_blueprintsapp = Flask(__name__)
register_blueprints(app, 'app.blueprints')
```Blueprints can now be placed under this package, as such:
```
.
└── app
├── blueprints
│ ├── account
│ │ ├── account.py
│ │ ├── contact
│ │ │ ├── contact.py
│ │ │ └── __init__.py
│ │ └── __init__.py
│ ├── api
│ │ ├── account
│ │ │ ├── account.py
│ │ │ ├── contact
│ │ │ │ ├── contact.py
│ │ │ │ └── __init__.py
│ │ │ └── __init__.py
│ │ ├── __init__.py
│ │ └── user
│ │ ├── __init__.py
│ │ └── tribute.py
│ ├── __init__.py
│ └── project
│ ├── documents.py
│ ├── __init__.py
│ ├── project.py
│ └── search.py
└── __init__.py
```Blueprint `__init__.py` files should look something like the following:
```python
from flask import Blueprintbp = Blueprint('project', __name__)
from . import documents
from . import project
from . import search
```(Note that the top-level `app/blueprints/__init__.py` is not a blueprint
package, and therefore should not have a blueprint defined.)The blueprint files themselves can then define the routes. For example:
```python
from flask import redirect, url_for
from . import bp@bp.post('/project//store')
def store(project_id):
# ...
return redirect(url_for('project.show', project_id=project_id))
```If the blueprint variable is not `bp`, then this can be customised:
```python
register_blueprints(app, 'app.blueprints', bp='bp')
```## Further Reading
* [`importlib` — The implementation of `import`](https://docs.python.org/3/library/importlib.html)
* [`pkgutil` — Package extension utility](https://docs.python.org/3/library/pkgutil.html)
* [Modular Applications with Blueprints](https://flask.palletsprojects.com/en/2.3.x/blueprints/)