How to divide flask app into multiple py files?

PythonFlask

Python Problem Overview


My flask application currently consists of a single test.py file with multiple routes and the main() route defined. Is there some way I could create a test2.py file that contains routes that were not handled in test.py?

@app.route('/somepath')
def somehandler():
    # Handler code here

I am concerned that there are too many routes in test.py and would like to make it such that I can run python test.py, which will also pick up the routes on test.py as if it were part of the same file. What changes to I have to make in test.py and/or include in test2.py to get this to work?

Python Solutions


Solution 1 - Python

You can use the usual Python package structure to divide your App into multiple modules, see the Flask docs.

However,

> Flask uses a concept of blueprints for making application components and supporting common patterns within an application or across applications.

You can create a sub-component of your app as a Blueprint in a separate file:

simple_page = Blueprint('simple_page', __name__, template_folder='templates')
@simple_page.route('/<page>')
def show(page):
    # stuff

And then use it in the main part:

from yourapplication.simple_page import simple_page

app = Flask(__name__)
app.register_blueprint(simple_page)

Blueprints can also bundle specific resources: templates or static files. Please refer to the Flask docs for all the details.

Solution 2 - Python

You can use simple trick which is import flask app variable from main inside another file, like:

test_routes.py

from __main__ import app

@app.route('/test', methods=['GET'])
def test():
    return 'it works!'

and in your main files, where you declared flask app, import test-routes, like:

app.py

from flask import Flask, request, abort

app = Flask(__name__)

# import declared routes
import test_routes

It works from my side.

Solution 3 - Python

This task can be accomplished without blueprints and tricky imports using Centralized URL Map

app.py

import views
from flask import Flask

app = Flask(__name__)

app.add_url_rule('/', view_func=views.index)
app.add_url_rule('/other', view_func=views.other)

if __name__ == '__main__':
    app.run(debug=True, use_reloader=True)

views.py

from flask import render_template

def index():
    return render_template('index.html')

def other():
    return render_template('other.html')

Solution 4 - Python

I would like to recommend flask-empty at GitHub.

It provides an easy way to understand Blueprints, multiple views and extensions.

Solution 5 - Python

If you need split blueprint to separate files you can use snippet:

# app.py

from blueprint_module import blueprint

app = Flask(__name__)
app.register_blueprint(blueprint)
# blueprint_module\__init__.py

from flask import Blueprint

blueprint = Blueprint('my_blueprint', __name__)

from . import page
# blueprint_module\page.py

from . import blueprint

@blueprint.route("/url", methods=['GET'])
def hello():
  return 'hello world'

Solution 6 - Python

Dividing the app into blueprints is a great idea. However, if this isn't enough, and if you want to then divide the Blueprint itself into multiple py files, this is also possible using the regular Python module import system, and then looping through all the routes that get imported from the other files.

I created a Gist with the code for doing this:

https://gist.github.com/Jaza/61f879f577bc9d06029e

As far as I'm aware, this is the only feasible way to divide up a Blueprint at the moment. It's not possible to create "sub-blueprints" in Flask, although there's an issue open with a lot of discussion about this:

https://github.com/mitsuhiko/flask/issues/593

Also, even if it were possible (and it's probably do-able using some of the snippets from that issue thread), sub-blueprints may be too restrictive for your use case anyway - e.g. if you don't want all the routes in a sub-module to have the same URL sub-prefix.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRolandoView Question on Stackoverflow
Solution 1 - PythonpixelistikView Answer on Stackoverflow
Solution 2 - PythonNimer EsamView Answer on Stackoverflow
Solution 3 - PythonBleizView Answer on Stackoverflow
Solution 4 - PythonJonathan Simon PratesView Answer on Stackoverflow
Solution 5 - PythonIvan BryzzhinView Answer on Stackoverflow
Solution 6 - PythonJazaView Answer on Stackoverflow