Self contained modules and Django style URL routing for Flask.
Flask-MicroServices is a simple, lightweight attempt at bringing self contained module hierarchy to Flask. Better project organization through separation of concerns, isolating the different sections of your app into separate modules, or ‘microservices’.
An opinionated, but minimal approach to higher maintainability with Flask.
- Django style route definitions
- Simple, modular, microservices inspired architecture
- Dynamic, overridable resolution of Static / Template directories
Problems this plugin solves:
- Allow Blueprint defined template folders to override parent templates, rather than the other way around
- Allow Blueprint defined static folders to resolve from and override /static, rather than having to define individual /static_module_name folders
- Enable modular, but centralized definition of routes with a cleaner syntax so that you aren’t forced to hunt for @app.route() decorators or use the arcane blueprint syntax in complex projects
- Allow you to drop in / drop out sections of functionality at will just by passing the name of the module for portability, testing, and modularity
Flask-MicroServices is not exceptionally complex. In fact, it is quite small– 200-ish lines of code, but it can bring a high level of reasonability to the way you write your Flask applications.
Check out the example project at ./example, or read below for a minimal example use-case.
- Project Layout
- `__init__.py <#approot-init>`__
- `file.txt <#module-static-file>`__
- `main.html <#module-template>`__
- `__init__.py <#module-init>`__
- `urls.py <#module-urls>`__
- `views.py <#module-views>`__
You don’t necessarily have to define your app within __init__.py, but this is how we will initialize our app for the purpose of this example.
from flask_microservices import MicroServicesApp app = MicroServicesApp(__name__) enabled_modules = [ # Normally, we'd define more modules to enable: # 'home', # 'forum', # 'settings', # We will enable just one, for now: 'admin' ] # By default, this will assume your modules directory is "./modules" if a second argument is not provided. app.register_urls(enabled_modules) app.run()
This is the heart of every module, and is required for the app to be able to enable it.
from flask_microservices import Router from . import urls MODULE_NAME = 'admin' IMPORT_NAME = __name__ # These blueprints are what is collected when you run app.register_urls() blueprint = Router.create_blueprint(MODULE_NAME, IMPORT_NAME) blueprint.register_urls(urls.urlpatterns)
Your URL definitions for each module go here. Routes defined here follow all the normal patterns of `@app.route() <http://flask.pocoo.org/docs/0.12/api/#url-route-registrations>`__, with the exception of endpoint being renamed to name, and the order of view_func and name being reversed.
When a name is provided here, as with a normal blueprint it will become namespaced. A value of name='home' will become resolveable with url_for('admin.home').
from flask_microservices import url from . import views urlpatterns = [ url('/admin/', view_func=views.admin_panel, name='home'), ## Example URLs: ### Minimal: # url('/admin/simple/', view_func=views.admin_simple) ### Advanced # url('/admin/roles/add/', view_func=views.admin_panel_roles_add, name='role_add', methods=['GET', 'POST']), # url('/admin/roles/edit/', view_func=views.admin_panel_roles_edit, name='role_edit', methods=['GET', 'POST']), ]
This is where your views are defined. As your project scales farther, you may want to separate your logic into files such as a.py, b.py, and import them into your views.py with from . import a, b in order to make them visible to urls.py.
from flask import render_template from ExampleApp.ExampleWrappers import admin_access_required @admin_access_required def admin_panel(): return render_template('admin/main.html')
Templates folder resolves as normal. The MicroServicesApp instance will check all module template directories before trying to resolve from the root template folder. An important caveat to this approach is to remember that when two modules both possess conflicting templates, they will be resolved in the order that they were defined in the enabled_modules value that you passed to app.register_urls().
<html> <h1> Holy cow! </h1> <p> If the router was unable to find this file, then it would try your root level templates folder at `project_root/appname/templates/admin/main.html` before failing.</p> </html>
Module Static File
Static folder resolves as normal. The MicroServicesApp instance will behave with respect to static files in an identical manner to how it handles template files. See above for caveats.
I am a file! If I could not be found, the Router would attempt to find `project_root/appname/static/file.txt` before 404'ing.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size & hash SHA256 hash help||File type||Python version||Upload date|
|Flask_MicroServices-0.34.5-py3-none-any.whl (11.2 kB) Copy SHA256 hash SHA256||Wheel||any||Jan 27, 2017|
|Flask-MicroServices-0.34.5.zip (35.3 kB) Copy SHA256 hash SHA256||Source||None||Jan 27, 2017|