Cannot set up blueprints with flask-restful

196 views Asked by At

I have read this documentation for setting up flask-restful with blueprints. But for some reason my curl requests go through the routes.py. Here is my file structure:

backend
   |-tools.py
   |-app/
      |-project/
          |-__init__.py
          |-heatmap/
               |-__init__.py
               |-routes.py

The backend/app/project/__init__.py file is where I have all the blueprints registered and where I have a method that creates a flask app that I then call in backend/tools.py. Here is the snippet of how I register the blueprints and of the method:

backend/app/project/__init__.py

def register_blueprints(app):
    from .heatmap import heatmap_blueprint

    app.register_blueprint(
        heatmap_blueprint,
        url_prefix='/heatmap'
    )

def create_app(debug=False):
    app = Flask(__name__)
    app.debug = debug
    app.url_map.strict_slashes = False
    app.config['SECRET_KEY'] = 'super_secret'

    initialize_extensions(app)
    register_blueprints(app)

    @app.teardown_appcontext
    def close_db(error):
        """Closes the database again at the end of the request."""
        for db_enum in DB:
            db = g.pop(db_enum.value, None)

            if db is not None:
                db.close()

    return app

Here is the snippet of the heatmap routes.py:

backend/app/project/heatmap/routes.py

from flask import request, render_template

from app.core.heatmap_python.Heatmaps import Heatmaps
from . import heatmap_blueprint


@heatmap_blueprint.route('/', methods=["GET", "POST"])
def index():

    if request.method == "GET":
        return render_template('heatmap/index.html')
    elif request.method == "POST":

        data = request.form.to_dict()

        h = Heatmaps(data)
        response = h.run()

        return render_template('heatmap/results.html', response=response)

Here is the __init__.py file of the heatmap directory: backend/app/project/heatmap/__init__.py

from flask_restful import abort, Resource, Api
from webargs import fields
from webargs.flaskparser import use_args
# from investment_tools import api
from flask import Blueprint, url_for
heatmap_blueprint = Blueprint('heatmap', __name__, template_folder='templates',
                                        static_folder='static', static_url_path='/heatmap/static')

from . import routes

api = Api(heatmap_blueprint)
# API

HEATMAPS = {
    'A': {'asd': 'asd'},
    'B': {'bcd': 'bcd'}
}


def abort_if_arg_doesnt_exist(args):
    print(args)


heatmap_args = {
    # All are required arguments
    'currency': fields.Str(required=True),
    'field': fields.Str(required=True),
    'sector': fields.Str(required=True),
    'seniority': fields.Str(required=True),
}


class HeatmapApi(Resource):
    @use_args(heatmap_args)
    def post(self, args):
        print("The args are: ")
        print(args['currency'])
        print(args['field'])
        return "<h1>Hello World</h1>", 201


api.add_resource(HeatmapApi, '/heatmap')

And so when I'm making POST requests in this set up, they go through the routes.py file. If I comment out this part:

@heatmap_blueprint.route('/', methods=["GET", "POST"])
def index():

    if request.method == "GET":
        return render_template('bheatmap/index.html')
    elif request.method == "POST":

        data = request.form.to_dict()

        h = Heatmaps(data)
        response = h.run()

        return render_template('heatmap/results.html', response=response)

Then the post endpoint in the backend/app/project/heatmap/__init__.py does not seem to be registered, as I get an error that there is no such endpoint: TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement. . So my set up must be flawed. Can you please point me to what is incorrect here?

0

There are 0 answers