How to have separate routers module in Flask? - python

How can I decouple this class? I would like to put the paths in another file, is it possible to move the routes in another file?
#api.route('/home', '/api/email')
class Server(Resource):
def create_server(app, oauth=None):
if not oauth:
oauth = default_provider(app)
app = prepare_app(app)
#app.before_request
def load_current_user():
user = User.query.get(1)
g.user = user
#app.route('/home')
def home():
return 'home'
#app.route('/oauth/authorize', methods=['GET', 'POST'])
#oauth.authorize_handler
def authorize(*args, **kwargs):
return True
Those
#app.route('/home') # and
#app.route('/oauth/authorize', methods=['GET', 'POST'])
have to be in another file.
My attempt was this, I tried to create a file for routers:
class Router():
def __init__(self, app, oauth):
self.app = app
self.oauth = oauth
#app.route('/home')
def home():
return 'home'
I'm getting this error:
NameError: name 'app' is not defined

Well, I see a package you can use for flask projects called Flask-Via [pypi], inspired by the Django URL configuration system and designed to add similar functionality to Flask applications that have grown beyond a simple single file application. The following example is given from the docs of this project:
from flask import Flask
from flask.ext.via import Via
from flask.ext.via.routers.default import Functional
app = Flask(__name__)
def foo(bar=None):
return 'Foo View!'
routes = [
Functional('/foo', foo),
Functional('/foo/<bar>', foo, endpoint='foo2'),
]
via = Via()
via.init_app(app, route_module='flask_via.examples.basic')
if __name__ == "__main__":
app.run(debug=True)
I think this is exactly what you want :) and you can move it to another python module for example called routers.py.

Related

Flask - Redirect all routes of a particular Blueprint to external site

i have a requirement where i want all routes of a particular blueprint be redirected to a external webpage.
For the below example i would want '/Store', '/Store/home', '/Store/products' be redirected to lets say 'google.com'. in actuall scenario there could be multiple routes mapped to a single blueprint
i would want the other blueprint '/Online' untouched.
The use case here is that these modules(routes of blueprint1) have been moved to a different domain and i would want users to be redirected if they visit any of the url with the prefix 'Store'
Does anyone know of any function/workaround which can be used to achieve this?
from flask import Flask
from flask import Blueprint
app = Flask(__name__)
blueprint1 = Blueprint('example_blueprint', __name__)
blueprint2 = Blueprint('example_blueprint2', __name__)
#app.route('/')
def index():
return "this is from root"
#blueprint1.route('/')
def index_b1():
return "This is default route of blue print1"
#blueprint1.route('/home')
def index_b2():
return "This is homepage of blue print"
#blueprint1.route('/products')
def index_b3():
return "This is productpage of blue print"
#blueprint2.route('/')
def index_o1():
return "This is default route of blue print 2"
app.register_blueprint(blueprint1,url_prefix='/Store')
app.register_blueprint(blueprint2,url_prefix='/Online')
if __name__ == "__main__":
app.run(debug=True)
Use after_request (or before_request)
#blueprint1.after_request
def after_request_func(response):
return redirect('https://www.pythonkitchen.com')
#blueprint2.after_request
def after_request_func(response):
return redirect('https://www.pythonkitchen.com')

Overwrite route in flask blueprint

There is a blueprint with a lot of useful routes defined, but I have no control over it (can not change it's code in any way)
Trying to reuse it in a different app but one of the blueprint's endpoints must be overloaded. How can I achieve that?
I tried just adding a new route to blueprint on top of the existing one:
#blueprint.route('/my/route', methods=['PUT', 'POST'])
def my_new_view_func(program, project):
# some new behavior for the endpoint
As the result there is duplicate url_rule in app.url_map.iter_rules():
<Rule '/my/route' (PUT, POST) -> my_view_func>,
<Rule '/my/route' (PUT, POST) -> my_new_view_func>,
and when requesting /my/route old viewer my_view_func gets executed
Can I somehow get rid of the old url rule? Or maybe there is a better way to overwrite the route?
There are 2 solutions which I found. First:
from flask import Flask, Blueprint
simple_page = Blueprint('simple_page', __name__, )
#simple_page.route('/my/route/')
def my():
# for example it's a registered route somewhere...
return 'default'
#simple_page.route('/my/route/')
def new_my():
# new endpoint / should works instead my()
return 'new'
# map of views which we won't register in Flask app
# you can store this somewhere in settings
SKIP_VIEWS = (
# route, view function
('/my/route/', my, ),
)
class CustomFlask(Flask):
def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
# Flask registers views when an application starts
# do not add view from SKIP_VIEWS
for rule_, view_func_ in SKIP_VIEWS: # type: str, func
if rule_ == rule and view_func == view_func_:
return
return super(CustomFlask, self).add_url_rule(rule, endpoint, view_func, **options)
app = CustomFlask(__name__)
app.register_blueprint(simple_page)
app.run(debug=True)
Second way:
two.py - default blueprint with endpoint
from flask import Blueprint
bp_two = Blueprint('simple_page2', __name__, )
#bp_two.route('/my/route/')
def default():
return 'default'
test.py - your blueprint + app
from flask import Flask, Blueprint
from two import bp_two
your_bp = Blueprint('simple_page', __name__, )
#your_bp.route('/my/route/')
def new_route():
return 'new'
app = Flask(__name__)
# register blueprint and turn off '/my/route/' endpoint
app.register_blueprint(bp_two, **{'url_defaults': {'/my/route/': None}})
app.register_blueprint(your_bp)
app.run(debug=True)
Run app. Open /my/route/. You will see that default endpoint wasn't add/works.
Hope this helps.

Flask blueprint doesn't work without prefix

Hi I have a Flask app structured in following way and I have problem with blueprints setup. Whatever I do, they only work with url_prefix set up. It works currently as /main/verify but as it is a small app I would love to have an endpoint like /verify. What's interesting I managed to make it work with / route, but for the same configuration it didn't work for the /verify. I am pretty clueless right now, I can live with it as it is, but I really wonder what am I doing wrong.
Here is the code:
__init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import config
db = SQLAlchemy()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
db.init_app(app)
from main import main
app.register_blueprint(main)
return app
main/__init__.py
from flask import Blueprint
main = Blueprint('main', __name__, url_prefix='/main')
from . import views
main/views.py
from flask import request, jsonify
from . import main
#main.route('/')
def index():
return "Hello world"
#main.route('/verify')
def verify():
url = request.args['url']
query = request.args['query']
return jsonify({ ... })
As I see you didn't register blueprint without prefix. If you need to register endpoints without prefix you must create a new instance of Blueprint
main = Blueprint('main', __name__, url_prefix='/main')
# main endpoints(with prefix /main)...
#main.route('/')
def index_main():
return "Hello world from /main/"
# routes without any prefix
default = Blueprint('default', __name__)
#default.route('/')
def index():
return "Hello world from /"
app = Flask(__name__)
app.register_blueprint(main)
app.register_blueprint(default)
Hope this helps.

url_for Builderror in flask extension with pluggable views

I am building a simple extension for flask and have a problem with the url_for function not being able to build the urls within the extension.
Can somebody help me figure out what I am missing here?
I simplified the code to demonstrate the issue (all of the url_for calls raise a werkzeug BuildError exception):
import flask
import flask.views
import logging
logging.basicConfig(level=logging.DEBUG)
class MyFlaskExt(object):
def __init__(self, app=None):
if app is not None:
self.init_app(app)
def init_app(self, app):
self.blueprint = flask.Blueprint('myext', __name__, static_folder='static', template_folder='templates')
self.blueprint.add_url_rule('/', view_func=RootView.as_view('root'), endpoint='root')
self.blueprint.add_url_rule('/var/<somevar>', view_func=VarView.as_view('var'), endpoint='var')
self.blueprint.add_url_rule('/novar', view_func=NoVarView.as_view('novar'), endpoint='novar')
app.register_blueprint(self.blueprint)
class RootView(flask.views.View):
def dispatch_request(self):
logging.debug(flask.url_for('novar'))
logging.debug(flask.url_for('novar', _external=True))
return flask.redirect(flask.url_for('var', somevar='test'))
class VarView(flask.views.View):
def dispatch_request(self, somevar):
return "SUCCESS! ({})".format(somevar)
class NoVarView(flask.views.View):
def dispatch_request(self):
return "SUCCESS!"
if __name__ == '__main__':
app = flask.Flask(__name__)
app.debug = True
my_ext = MyFlaskExt()
my_ext.init_app(app)
logging.debug(app.url_map)
app.run()
Blueprint endpoints are registered under the name of the Blueprint. If I remember correctly you will either need to prepend your url_for calls with "." (if they will all be operating under the same blueprint) or use the full name:
def dispatch_request(self):
logging.debug(flask.url_for('.novar'))
logging.debug(flask.url_for('.novar', _external=True))
return flask.redirect(flask.url_for('.var', somevar='test'))

Flask, Blueprint, current_app

I am trying to add a function in the Jinja environment from a blueprint (a function that I will use into a template).
Main.py
app = Flask(__name__)
app.register_blueprint(heysyni)
MyBluePrint.py
heysyni = Blueprint('heysyni', __name__)
#heysyni.route('/heysyni'):
return render_template('heysyni.html', heysini=res_heysini)
Now in MyBluePrint.py, I would like to add something like :
def role_function():
return 'admin'
app.jinja_env.globals.update(role_function=role_function)
I will then be able to use this function in my template. I cannot figure out how I can access the application since
app = current_app._get_current_object()
returns the error:
working outside of request context
How can I implement such a pattern ?
The message error was actually pretty clear :
working outside of request context
In my blueprint, I was trying to get my application outside the 'request' function :
heysyni = Blueprint('heysyni', __name__)
app = current_app._get_current_object()
print(app)
#heysyni.route('/heysyni/')
def aheysyni():
return 'hello'
I simply had to move the current_app statement into the function. Finally it works that way :
Main.py
from flask import Flask
from Ablueprint import heysyni
app = Flask(__name__)
app.register_blueprint(heysyni)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
Ablueprint.py
from flask import Blueprint, current_app
heysyni = Blueprint('heysyni', __name__)
#heysyni.route('/heysyni/')
def aheysyni():
# Got my app here
app = current_app._get_current_object()
return 'hello'

Categories

Resources