Flask: get current blueprint webroot

user3758232

I am building a Flask app with a blueprint mounted on two different endpoint (one is a legacy alias to the other).

In my blueprint class:

ldp = Blueprint('ldp', __name__)

@ldp.route('/<path:uuid>', methods=['GET'])
@ldp.route('/', defaults={'uuid': None}, methods=['GET'],
        strict_slashes=False)

def get_resource(uuid):
    # Route code...

In my main server code:

app = Flask(__name__)
app.config.update(config['flask'])

app.register_blueprint(ldp, url_prefix='/new_ep')
# Legacy endpoint. @TODO Deprecate.
app.register_blueprint(ldp, url_prefix='/old_ep')

How can I get the actual URL of the request up to the /old_ep or /new_ep part in the route method, e.g. http://localhost:5000/new_ep?

So far I have used

request.host_url + request.path.split('/')[1]

but it looks quite inelegant and possibly error-prone. I would like to use the information from the blueprint setup if possible.

Thanks for your help.

EDIT: I could get to the Blueprint instance from within the request with

current_app.blueprints[request.blueprint]

and I was hoping that the url_prefix attribute that I set when registering the blueprint was there, but it is None instead. As I read from the documentation for the supposedly related iter_blueprints() method, apparently these blueprints are listed without regard of how many times and with which parameters they were registered. Too bad.

Josh J

Here is a full working example to get the idea based off issue 612

from flask import Flask, Blueprint, url_for, request, g

bp = Blueprint('whatever', __name__)

@bp.url_defaults
def bp_url_defaults(endpoint, values):
    url_prefix = getattr(g, 'url_prefix', None)
    if url_prefix is not None:
        values.setdefault('url_prefix', url_prefix)

@bp.url_value_preprocessor
def bp_url_value_preprocessor(endpoint, values):
    g.url_prefix = values.pop('url_prefix')

@bp.route('/something')
def index():
    return 'host prefix is %s%s' % (request.host_url, g.url_prefix)

app = Flask(__name__)

app.register_blueprint(bp, url_prefix='/new_ep', url_defaults={'url_prefix': 'new_ep'})
app.register_blueprint(bp, url_prefix='/old_ep', url_defaults={'url_prefix': 'old_ep'})

Web Output example

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't get flask blueprint to work

From Java

Flask: get current route

From Dev

Flask Session not persistant in blueprint

From Dev

Inject dependencies in Flask blueprint

From Dev

Overwrite route in flask blueprint

From Dev

Flask Blueprint 404

From Java

flask blueprint template folder

From Dev

Flask Blueprint structure

From Dev

Flask - Forms - Blueprint

From Dev

Flask blueprint and registering path

From Dev

Blueprint error in Flask: NameError: name of blueprint is not defined

From Dev

Calling method from Flask Blueprint

From Dev

Flask Blueprint Subdomain Not Working GAE

From Dev

How to init restless as flask blueprint

From Dev

Running a Flask blueprint from the program

From Dev

Query while declaring Flask Blueprint

From Dev

get current connection in flask socket.io

From Dev

How to get SelectField current value with Flask WTF

From Dev

How to get QuerySelectField current value with Flask WTF

From Dev

How to get current date time in python flask

From Dev

Get the current function name in the Flask template context

From Dev

Flask: Update Code Reference for: current_app._get_current_object()

From Dev

Flask - A blueprint's name collision occurred

From Dev

Flask BluePrint route not working with multiple parameteres

From Dev

Flask python blueprint logic code separation

From Dev

Flask: Blueprint not showing custom error page

From Dev

Flask: Template in Blueprint Inherit from Template in App?

From Dev

Flask Blueprint pass object to another file

From Dev

How to pass arbitrary arguments to a flask blueprint?