Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split Python Flask app into multiple files

I'm having trouble understanding how to split a flask app into multiple files.

I'm creating a web service and I want to split the api's into different files (AccountAPI.py, UploadAPI.py, ...), just so I don't have one huge python file.

I've read that you can do this with Blueprints, but I'm not entirely sure that route is the right one for me.

Ultimately I want to run one Main python file and include other files so that when it runs, they are considered one big file.

For example if I have Main.py and AccountAPI.py I want to be able to do this:

Main.py:

from flask import Flask import AccountAPI  app = Flask(__name__)  @app.route("/") def hello():     return "Hello World!"  if __name__ == "__main__":     app.run() 

AccountAPI.py:

@app.route("/account") def accountList():     return "list of accounts" 

I know with this example it obviously won't work, but is it possible to do something like that?

Thanks

like image 961
user1751547 Avatar asked Mar 05 '13 18:03

user1751547


People also ask

How do I split a Flask into multiple files?

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.

Are python flasks multithreaded?

As of Flask 1.0, flask server is multi-threaded by default. Each new request is handled in a new thread. This is a simple Flask application using default settings.

How do you route a Flask?

Routing in FlaskThe route() decorator in Flask is used to bind an URL to a function. As a result when the URL is mentioned in the browser, the function is executed to give the result. Here, URL '/hello' rule is bound to the hello_world() function.

Can you run multiple Flask apps?

You can combine not only Flask applications but any WSGI application. This would allow you to run a Django and a Flask application in the same interpreter side by side if you want. The usefulness of this depends on how the applications work internally.


2 Answers

Yes, Blueprints are the right way to do it. What you are trying to do can be achieved like this:

Main.py

from flask import Flask from AccountAPI import account_api  app = Flask(__name__)  app.register_blueprint(account_api)  @app.route("/") def hello():     return "Hello World!"  if __name__ == "__main__":     app.run() 

AccountAPI.py

from flask import Blueprint  account_api = Blueprint('account_api', __name__)  @account_api.route("/account") def accountList():     return "list of accounts" 

If this is an option, you might consider using different URL prefixes for the different APIs/Blueprints in order to cleanly separate them. This can be done with a slight modification to the above register_blueprint call:

app.register_blueprint(account_api, url_prefix='/accounts') 

For further documentation, you may also have a look at the official docs.

like image 197
cyroxx Avatar answered Sep 24 '22 13:09

cyroxx


Using Blueprint you can add your routes in the routes directory.

Structure

app.py routes     __init__.py     index.py     users.py 

__init__.py

from flask import Blueprint routes = Blueprint('routes', __name__)  from .index import * from .users import * 

index.py

from flask import render_template from . import routes  @routes.route('/') def index():     return render_template('index.html') 

users.py

from flask import render_template from . import routes  @routes.route('/users') def users():     return render_template('users.html') 

app.py

from routes import * app.register_blueprint(routes) 

If you want to add a new route file, say accounts.py, you just need to create the file accounts.py in the routes directory, just like index.py and users.py, then import it in the routes.__init__.py file

from .accounts import * 
like image 24
Searene Avatar answered Sep 25 '22 13:09

Searene