AiDev
AiDev

Reputation: 1234

How to import routes from another file in Flask - no frontend or view library solutions please, lightweight solution for Flask backend only

Let's take a boilerplate flask server that is 100% backend code.

File A:

from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/')
def index():
    return jsonify({"msg": "flask server live"})

if __name__ == '__main__':
    app.run(debug=True, threaded=True, port=5070)

If I want to add another route such as ('/test_route') and import it from another file (File B), how do I go about that? It seems simple, but all the other answers I have come across get into views, blueprints, front end tools, etc. All this is overkill and adds bloat. I just want a simple way to import additional routes from other files without adding a frontend or complexity to a dedicated backend Flask server.

In other words, how do I avoid putting every single route for my app all in File A?

ie add something like this to File A

from routes.file_b import file_b_routes

where File B is a file like:

from flask import jsonify, request
@app.route('/test', methods=['GET'])
def test():

    return jsonify({"msg": "test route"})

Sadly this does not work because then @app doesn't exist in File B, and when attempted to be imported directly in File B causes conflicting errors.

Upvotes: 0

Views: 4158

Answers (2)

Jürgen Gmach
Jürgen Gmach

Reputation: 6093

Basically, you explained the problem clearly - in a separate file, there is no app instance available.

There are two ways to work around this problem.

a) put the routes within a closure

# file: other_routes

def init_views(app):
    @app.route("/v1/overview")
    def overview():
        ...

Then, you can import the init_views function and initialize the routes.

from flask import Flask, jsonify
from other_routes import init_views

app = Flask(__name__)
init_views(app)

b) use blueprints

from flask import Blueprint

other_routes = Blueprint("other_routes", __name__)

@other_routes.route("/v1/overview")
def overview():
    ...

and then import the blueprint instance and register it.

from flask import Flask, jsonify
from other_routes import other_routes

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

I would not call two or three lines of code an overkill and bloat - it is just the way it works.

I cannot recommend the videos of FlaskCon2020 enough. There some base concepts of Flask got really exceptionally well explained.

I recommend to watch the one about the application factory pattern, which also explains the blueprint concept:

https://www.youtube.com/watch?v=xNo-eOfZH5Q&t=2s

Upvotes: 3

Kate
Kate

Reputation: 1836

If I understand it right:

Say you want to reuse the route '/' (index).

What you should do is:

  1. import the file that contains that route eg:

     from routes.api_routes import api_routes
    
  2. then call the function like this:

     return api_routes.index()
    

This will produce: return jsonify({"msg": "test route"})

Does this help ?

Upvotes: 0

Related Questions