Answer the question
In order to leave comments, you need to log in
How can I easily include route in flask?
Good afternoon.
It is necessary to put the routes in a separate file in the flask project.
But there is no way to do it.
I made a simple example for clarity:
app.py
from flask import Flask
app = Flask(__name__)
import route
if __name__ == '__main__':
app.run()
from app import app
@app.route('/')
def hello_world():
return 'Hello World!'
Answer the question
In order to leave comments, you need to log in
Flask uses Blueprints for this. The code should look something like this:
app.py
from flask import Flask
from route import app_route
app = Flask(__name__)
app.register_blueprint(app_route)
if __name__ == '__main__':
app.run(debug=True)
from flask import Blueprint
app_route = Blueprint('route', __name__)
@app_route.route('/')
def index():
return 'Hello world!'
You have a cyclic import, in Python this is not possible. Read the documentation about code organization in Flask projects, it has everything you need.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question