Flask router from other file
Here example with Blueprint
. Structure of files:
/project_folder server.py urls.py urls2.py
server.py:
from flask import Flaskfrom urls import urls_blueprintfrom urls2 import urls2_blueprintapp = Flask(__name__)# register routes from urlsapp.register_blueprint(urls_blueprint)# we can register routes with specific prefixapp.register_blueprint(urls2_blueprint, url_prefix='/urls2')if __name__ == "__main__": app.run(debug=True)
urls.py:
from flask import Blueprinturls_blueprint = Blueprint('urls', __name__,)@urls_blueprint.route('/')def index(): return 'urls index route'
urls2.py:
from flask import Blueprinturls2_blueprint = Blueprint('urls2', __name__,)@urls2_blueprint.route('/')def index(): return 'urls2 index route'
Run server and open http://localhost:5000/
and http://localhost:5000/urls2/
.
Hope this helps.