Last active
March 10, 2024 18:26
-
-
Save Jaza/61f879f577bc9d06029e to your computer and use it in GitHub Desktop.
Example of how to split a Flask blueprint into multiple files, with routes in each file, and of how to register all those routes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Note: can't put in a subdirectory for this example (GitHub Gists | |
doesn't allow subdirectories). Recommended to move this file to | |
foo/bar.py. | |
""" | |
routes = [] | |
def ping_bar(): | |
return 'Ping bar' | |
routes.append(dict( | |
rule='/ping-bar/', | |
view_func=ping_bar)) | |
def save_bar(): | |
return 'Save bar' | |
routes.append(dict( | |
rule='/save-bar/', | |
view_func=save_bar, | |
options=dict(methods=['POST',]))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Note: can't put in a subdirectory for this example (GitHub Gists | |
doesn't allow subdirectories). Recommended to move this file to | |
foo/baz.py. | |
""" | |
routes = [] | |
def call_baz(): | |
return 'Call baz' | |
routes.append(dict( | |
rule='/call-baz/', | |
view_func=call_baz)) | |
def delete_baz(): | |
return 'Delete baz' | |
routes.append(dict( | |
rule='/delete-baz/', | |
view_func=delete_baz, | |
options=dict(methods=['GET', 'POST']))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
app = Flask(__name__) | |
app.debug = True | |
@app.route('/') | |
def home(): | |
return 'App home' | |
from foo import mod | |
app.register_blueprint(mod, url_prefix='/foo') | |
if __name__ == '__main__': | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Note: can't put in a subdirectory for this example (GitHub Gists | |
doesn't allow subdirectories). Recommended to move this file to | |
foo/__init__.py, and to change the imports to | |
'from .bar' and 'from .baz'. | |
""" | |
from flask import Blueprint | |
mod = Blueprint('foo', __name__) | |
@mod.route('/') | |
def home(): | |
return 'Foo home' | |
from bar import routes as bar_routes | |
from baz import routes as baz_routes | |
routes = ( | |
bar_routes + | |
baz_routes) | |
for r in routes: | |
mod.add_url_rule( | |
r['rule'], | |
endpoint=r.get('endpoint', None), | |
view_func=r['view_func'], | |
**r.get('options', {})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Flask==0.10.1 |
why don't you simply use Nesting Blueprints ? https://flask.palletsprojects.com/en/3.0.x/blueprints/#nesting-blueprints ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool