Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass arbitrary arguments to a flask blueprint?

I have a flask api which I have wrapped up in an object. Doing this has made unit testing a breeze, because I can instantiate the api with a variety of different settings depending on whether it is in production, test, or whatehaveyou.

I am now trying to extend the api a bit, and for that I'm using a blueprint. The problem is that I cannot figure out how to pass arguments to the blueprint. My routes require information like which database to access, and that information is not static. How can I pass this information into a blueprint? I have included code below as an example:

api.py:

class MyApi(object):
    def __init__(self, databaseURI):
     self.app = Flask(__name__)
     self.app.register_blueprint(myblueprint)

blueprint.py

myblueprint= Blueprint('myblueprint', __name__)
@myblueprint.route('/route', methods=['GET'])
def route():
  database = OpenDatabaseConnection(databaseURI)

There is a related question here: How do I pass constructor arguments to a Flask Blueprint?

But the people who answer the question solve the op's use-case specific problem without actually answering the question of how to pass arbitrary arguments to a blueprint.

like image 351
melchoir55 Avatar asked Feb 20 '15 23:02

melchoir55


People also ask

Which argument is passed to a flask?

Definition of Flask URL Parameters. Flask URL parameters is defined as a set of arguments in form of a query string that is passed to a web application through Flask. These parameters get bonded to the URL.

How do you use blueprints in flask?

To use any Flask Blueprint, you have to import it and then register it in the application using register_blueprint() . When a Flask Blueprint is registered, the application is extended with its contents. While the application is running, go to http://localhost:5000 using your web browser.

What is Blueprint in flask RESTful?

Blueprint: it is used to structure the Flask application into different components, making it easier to structure the application based on different functionality. ' Flask-RESTful: extension of Flask that helps with building REST APIs quickly and following the REST architecture.

What is a view function in flask?

A view function is the code you write to respond to requests to your application. Flask uses patterns to match the incoming request URL to the view that should handle it. The view returns data that Flask turns into an outgoing response.


2 Answers

You could create the blueprint dynamically in a constructor function:

def construct_blueprint(database):

    myblueprint = Blueprint('myblueprint', __name__)

    @myblueprint.route('/route', methods=['GET'])
    def route():
        database = database

    return(myblueprint)
like image 171
syntonym Avatar answered Sep 22 '22 17:09

syntonym


Use Flask config system (app.config) to store all your config data, then from your Blueprint read your Application Context using current_app.

Store in app.config:

app.config[DATABASE_URI] = databaseURI

Read application context:

databaseURI = current_app.config[DATABASE_URI]

Sample code

main.py

from flask import Flask
from blueprint import myblueprint

app = Flask(__name__)
app.register_blueprint(myblueprint)
app.config[DATABASE_URI] = databaseURI

blueprint.py

from flask import current_app

myblueprint= Blueprint('myblueprint', __name__)
@myblueprint.route('/route', methods=['GET'])
def route():
  databaseURI = current_app.config[DATABASE_URI]
  database = OpenDatabaseConnection(databaseURI)
like image 31
ssoler Avatar answered Sep 21 '22 17:09

ssoler