Harshdeep
Harshdeep

Reputation: 5824

Define URL root with flask

I have multiple routes which have the same URL root. Example:

Can I define abc/def to be URL root. (Something similar to what can be done in Java using Spring or Apache CXF)

Thanks

Upvotes: 2

Views: 12810

Answers (4)

renatodamas
renatodamas

Reputation: 19585

Just use Blueprint from Flask:

app = Flask(__name__)
bp = Blueprint('', __name__, url_prefix="/abc/def")

def upload():
    pass

def list():
    pass

bp.add_url_rule('/upload', 'upload', view_func=upload)
bp.add_url_rule('/list', 'list', view_func=list)
...

app.register_blueprint(bp)

Upvotes: 0

dganesh2002
dganesh2002

Reputation: 2230

I needed similar so called "context-root". I did it in conf file under /etc/httpd/conf.d/ using WSGIScriptAlias :

myapp.conf

<VirtualHost *:80>
    WSGIScriptAlias /myapp /home/<myid>/myapp/wsgi.py

    <Directory /home/<myid>/myapp>
        Order deny,allow
        Allow from all
    </Directory>

</VirtualHost>

So now I can access my app as : http://localhost:5000/myapp

See the guide - http://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html

Upvotes: 0

In flask-restful it is possible to prefix all your routes on api initialization:

>>> app = Flask(__name__)
>>> api = restful.Api(app, prefix='/abc/def')

You can then wire your resources ignoring any prefixes:

>>> api.add_resource(MyResource, '/upload')
>>> ...

Upvotes: 5

Bhargav
Bhargav

Reputation: 918

You can use the APPLICATION_ROOT key for your app's config.

app.config['APPLICATION_ROOT'] = "/abc/def"

source - Add a prefix to all Flask routes

Upvotes: 3

Related Questions