Adding multiple endpoints into a Flask-RESTplus namespace from multiple files - python

I am working with Flask-Resplus API
I want to create multiple endpoints into a single namespace. That is simple, but if I want to split the code to add endpoints into multiple files, getting issues there.
Following is my app file:
from flask_restplus import Namespace, Api
from flask import Blueprint
from test_controller1 import test_ns
blueprint = Blueprint('api', __name__)
api = Api(blueprint,
title='Test API',
version='1.0',
description='Test API',
)
api.add_namespace(test_ns, path='/test')
test_controller1.py
#test_ns.route("/test1")
class Test(Resource):
def put(self):
pass
test_controller2.py
from test_controller1 import test_ns
#test_ns.route("/test2")
class Test(Resource):
def get(self):
pass
If I import test_ns from test_controller_1, only test1 endpoint will be added in the namespace.
How can I add both the endpoints(available in different files) in the same namespace?

This can be done by defining Namespace(with the same name) across the classes.
test_controller1.py
test_ns1 = Namespace("test", "Namespace for test")
test_controller2.py
test_ns2 = Namespace("test", "Namespace for test")
Add both the namespaces to the blueprint.

Related

Flask-Restplus middleware

I have made an api with restplus
from flask_restplus import Namespace, Resource, fields
from flask import request
from flask_restplus import abort
api = Namespace('myproject', description='My API')
#api.route('/run')
class MyProject(Resource):
#api.doc(params={'name':'name'})
def post(self):
#doing stuff
return {'status':'started'}
The point of this route is to run another function and return {'status':'started'} immediatly without waiting for the function to end.
I've found this https://stackoverflow.com/a/51013358/1540114
but I have trouble using it in my project
from what I understood the AfterResponse is a middleware that I can apply to my route
I tried using it like this
....
api = Namespace('myproject', description='My API')
AfterResponse(api)
#api.after_response
def say_hi():
print("hi")
#api.route('/run')
class MyProject(Resource):
....
but it gave me an error stating that api doesn't have a wsgi_app property

How to access flask config variables outside application context according to my project structure?

spinngod.py - flask app starter code
from app import create_app
import sys
run_profile = str(sys.argv[1]) if len(sys.argv) >= 2 else 'development'
app = create_app(run_profile)
print("App Root Path:" + app.root_path)
if __name__ == '__main__':
print sys.path
app.run(debug=True, host='0.0.0.0')
app/init.py - creates flask app
def create_app(profile_name):
print "currently active profile:" + profile_name
app = Flask(__name__)
############# configurations ####################
app.config.from_object(config[profile_name])
configure_app(app)
configure_app_logger(app)
#################### blueprint registration and rest_plus namespace additions ###############
from api_1_0 import api as api_1_0_blueprint
from api_1_0.restplus import api_restplus
# ************************************************** #
api_restplus.init_app(api_1_0_blueprint)
api_restplus.add_namespace(application_namespace)
api_restplus.add_namespace(pipeline_template_namespace)
api_restplus.add_namespace(loadbalancer_namespace)
api_restplus.add_namespace(servergroup_namespace)
api_restplus.add_namespace(task_namespace)
# ************************************************** #
app.register_blueprint(api_1_0_blueprint)
##############################################################
return app
I want to access flask config variables defined in config.py in some other files which are outside application context. The app configuration depends on which profile it is started with (dev,stage or production) which is being passed from command line as an arg.
The only way that I can think of accessing config variables outside app context is to set profile (dev,stage or prod) as an environment variable and
then import directly from config file.
The second way that I tried was to move creation of flask app in app/init.py outside method.
This is how I am trying to access config variables in another class.
import requests
class Client(object):
def __init__(self):
from app import app
print "fjaijflkajsf" + app.config['SPINNAKER_BASE_URL']
pass
Is there a way better of doing this in flask ?
From the docs:
Rather than passing the application around to each function, the current_app and g proxies are accessed instead.
The Flask application object has attributes, such as config, that are useful to access within views and CLI commands. However, importing the app instance within the modules in your project is prone to circular import issues.
Flask solves this issue with the application context. Rather than referring to an app directly, you use the the current_app proxy, which points to the application handling the current activity.
You import current_app like this:
from flask import current_app
and then access the config or other attributes like this:
config = current_app.config
Example:
src/application.py (where config is set in the context)
create_app():
app = Flask('app')
app.config.from_object(some_class)
return app
src/module/another_module.py
from flask import current_app
def function_that_requires_config():
config = current_app.config
Alternative:
src/application.py (where config is set in the context)
APP = create_app(os.environ.get('FLASK_ENV'))
src/module/another_module.py
from src.application import APP
def function_that_requires_config():
config_value = APP.config.get(config_key, default_value)
Not sure if it is good to put it here as it may not respond to the question directly, but here is the cleanest way i've figured to use config values outside of requests, without having to pass config as a param.
The solution is actually pretty simple, juste consider the part of your code as a flask_extension.
my exemple will be the use of external api, with ROOT_URL in the config file, and i don't want to make api call from within my routes, so the api is in its own module.
in my create_app fuction:
from flask import Flask
from .api import api
from .configmodule import Config
from .model import db
def create_app(environment):
app = Flask(__name__)
app.config.from_object(Config.get_config(environment))
db.init_app(app)
api.init_app(app) # here i use api.init_app the same way i do for sqlalchemy
and in api/init.py
class Api:
def init_app(self, app):
self.config = app.config
api = Api()
and in any files in my api modude i can now write
from . import api
def foo():
print(api.config.get("API_ROOT_URL"))
this can even be improved if you feel the need to access some other global app vars from your module.

Python, Flask: Importing functions and variables inside blueprint

How to import a function and variable from app/__init__.py and app/blueprint/__init__.py , respectively, inside app/blueprint/views.py ?
app/__init__.py
def main():
<..>
app/blueprint/__init__.py
from flask import Blueprint
blueprint = Blueprint('blueprint', __name__, template_folder='templates')
app/blueprint/views.py
import blueprint
import main
from app.__init__ import *
from app.blueprint.__init__ import *
should import all the functions and variables from both files.
However, though I don't think init file is supposed to be used for this.
Below examples of Flask Blueprints I used my project, learnt structure from Udemy tutorial, I think the idea is generally the init files are used to make a Python directory into a package so you can import stuff within it. You'd probable better create new files with the functions (less often variables) you want to import, maybe experts will confirm, but I think generally you leave Python init files blank unless you really know what you're doing.
from flask import Flask, render_template
from Source.common.database import Database
from Source.models.users.views import user_blueprint
from Source.models.street_lists.views import street_list_blueprint
# from Source.models.street_reports.views import street_report_blueprint
__author__ = "Will Croxford, with some base structure elements based on Github: jslvtr, \
from a different tutorial web application for online price scraping"
app = Flask(__name__)
app.config.from_object('Source.config')
app.secret_key = "123"
app.register_blueprint(user_blueprint, url_prefix="/users")
app.register_blueprint(street_list_blueprint, url_prefix="/streetlists")
# app.register_blueprint(street_report_blueprint, url_prefix="/streetreports")
#app.before_first_request
def init_db():
Database.initialize()
#app.route('/')
def home():
return render_template('home.jinja2')
#app.route('/about_popup.jinja2')
def info_popup():
return render_template('about_popup.jinja2')
Flask Views file example:
# In this model, views.py files are the Flask Blueprint for this object.
# ie they describe what HTTP API endpoints are associated to objects of this class.
from flask import Blueprint, render_template, request, redirect, url_for
from Source.models.street_lists.street_list import StreetList
__author__ = 'jslvtr'
street_list_blueprint = Blueprint('street_lists', __name__)
#street_list_blueprint.route('/')
def index():
prop_query = StreetList.get_from_mongo(streetpart="bum")
return render_template('street_lists/street_list.jinja2', stores=prop_query)
You can look at pocoo.org flask doc examples, and search other SO questions for Flask blueprint template examples I think. Good luck!
I read the blog suggested by Will Croxford, and here's the solution to my problem:
app/blueprint/views.py
from app import main
from app.blueprint import blueprint

Is it possible to import flask configuration values in modules without circular import?

I'm using Flask with Blueprints to get a skeleton for my website and I'm having a problem using configuration classes deep in my application.
Here's some dummy code that explains how I've set everything up:
websiteconfig.py
class Config(object):
pass
class ProductionConfig(Config):
DEBUG = False
class DevelopmentConfig(Config):
DEBUG = True
website/__ init __.py:
# Some app code and config loading
app = Flask('website')
app.config.from_object('websiteconfig.DevelopmentConfig')
# Import some random blueprint
from website import users
app.register_blueprint(users.api)
# This works:
# print app.config['DEBUG']
website/users/__ init __.py:
from flask import Blueprint
from website.users.models import test
api = Blueprint('users', __name__, url_prefix='/users')
# This works:
# print api.config['DEBUG']
# From models
print test()
website/users/models.py:
# How can I reach the config variables here?
def test():
# I want config['DEBUG'] here
How can I reach the configuration variables stored in the class I load in app.py deep inside the users package?
Is a circular import like from website import app (inside models.py) an accepted solution?
If not, is there some simple solution I've missed?
I believe you can use flask's current_app idiom for that.
http://flask.pocoo.org/docs/api/#flask.current_app
from flask import current_app
def test():
return current_app.config.get('some_config_value')

blueprint of blueprints (Flask)

I have a series of blueprints I'm using, and I want to be able to bundle them further into a package I can use as seamlessly as possible with any number of other applications. A bundle of blueprints that provides an entire engine to an application. I sort of created my own solution, but it is manual and requires too much effort to be effective. It doesn't seem like an extension, and it is more than one blueprint(several that provide a common functionality).
Is this done? How?
(Application dispatching methods of tying together several programs might work isn't what I'm looking for)
Check this out: Nesting Blueprints → https://flask.palletsprojects.com/en/2.0.x/blueprints/#nesting-blueprints
parent = Blueprint('parent', __name__, url_prefix='/parent')
child = Blueprint('child', __name__, url_prefix='/child')
parent.register_blueprint(child)
app.register_blueprint(parent)
I wish the Blueprint object has a register_blueprint function just as the Flask object does. It would automatically place and registered blueprints under the current Blueprints' url.
The simplest way would be to create a function that takes an instance of a Flask application and registers all your blueprints on it in one go. Something like this:
# sub_site/__init__.py
from .sub_page1 import bp as sb1bp
from .sub_page2 import bp as sb2bp
# ... etc. ...
def register_sub_site(app, url_prefix="/sub-site"):
app.register_blueprint(sb1bp, url_prefix=url_prefix)
app.register_blueprint(sb2bp, url_prefix=url_prefix)
# ... etc. ...
# sub_site/sub_page1.py
from flask import Blueprint
bp = Blueprint("sub_page1", __name__)
#bp.route("/")
def sub_page1_index():
pass
Alternately, you could use something like HipPocket's autoload function (full disclosure: I wrote HipPocket) to simplify the import handling:
# sub_site/__init__.py
from hip_pocket.tasks import autoload
def register_sub_site(app,
url_prefix="/sub-site",
base_import_name="sub_site"):
autoload(app, base_import_name, blueprint_name="bp")
However, as it currently stands you couldn't use the same structure as example #1 (HipPocket assumes you are using packages for each Blueprint). Instead, your layout would look like this:
# sub_site/sub_page1/__init__.py
# This space intentionally left blank
# sub_site/sub_page1/routes.py
from flask import Blueprint
bp = Blueprint("sub_page1", __name__)
#bp.route("/")
def sub_page1_index():
pass
I have solution for myself how to load blueprints defined in configuration, so then you can have something like CORE_APPS = ('core', 'admin', 'smth') in config and when you construct app you can register those apps (of course those strings in CORE_APPS must be the names of the files you want to import in your python path).
So I'm using function to create app:
app = create_app()
def create_app():
app = Flask(__name__)
# I have class for my configs so configuring from object
app.config.from_object('configsClass')
# does a lot of different stuff but the main thing could help you:
from werkzeug.utils import import_string
for app in app.config['CORE_APPS']
real_app = import_string(app)
app.register_blueprint(real_app)
After that your blueprint should be registered. Of course you can have different format in configs to support custom url prefixes and so on and so on :)
Of course you can also do something like this in your main blueprint, so in the application creation you will need to register that one main blueprint.

Categories