Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use external methods as route decorators in Python/Flask?

Tags:

python

flask

My main app file is currently a series of method definitions, each attached to a route. I've got 3 distinct parts to my app (main, admin, api). I'm trying to split out methods into external files for better maintenance but I like Flask's simplicity in using route decorators for my application's URLs.

One of my routes currently looks like this:

# index.py
@application.route('/api/galleries')
def get_galleries():
    galleries = {
        "galleries": # get gallery objects here
    }
    return json.dumps(galleries)

But I'd like to extract the get_galleries method into a file containing methods for my API:

import api
@application.route('/api/galleries')
api.get_galleries():

The problem is that when I do that I get an error. Is this possible, and if so how do I do it?

like image 944
commadelimited Avatar asked Jun 16 '13 02:06

commadelimited


People also ask

Can you use multiple decorators to route URLs to a function in Flask?

We can use multiple decorators by stacking them.

What is the app route decorator used for in a Flask application?

To bind a function to an URL path we use the app. route decorator. In the below example, we have implemented the above routing in the flask.

How do you make a routing in Flask?

Creating A Flask Application And Route This code imports the Flask library, creates an app and defines the homepage for a simple web server. from flask import Flask app = Flask(__name__) @app. route('/') def home(): return 'Hello World! '

How do you pass parameters on a Flask route?

flask route params Parameters can be used when creating routes. A parameter can be a string (text) like this: /product/cookie . So you can pass parameters to your Flask route, can you pass numbers? The example here creates the route /sale/<transaction_id> , where transaction_id is a number.


2 Answers

Like stated in the other comment, you can call app.route('/')(api.view_home()) or use Flask's app.add_url_rule() http://flask.pocoo.org/docs/api/#flask.Flask.add_url_rule

Flask's @app.route() code:

def route(self, rule, **options):
    def decorator(f):
        endpoint = options.pop('endpoint', None)
        self.add_url_rule(rule, endpoint, f, **options)
        return f
    return decorator

You can do the following:

## urls.py

from application import app, views

app.add_url_rule('/', 'home', view_func=views.home)
app.add_url_rule('/user/<username>', 'user', view_func=views.user)

And then:

## views.py

from flask import request, render_template, flash, url_for, redirect

def home():
    render_template('home.html')

def user(username):
    return render_template('user.html', username=username)

Is the method I use for breaking things down. Define all your urls in it's own file and then import urls in your __init__.py that runs app.run()

In your case:

|-- app/
|-- __init__.py (where app/application is created and ran)
|-- api/
|   |-- urls.py
|   `-- views.py

api/urls.py

from application import app

import api.views

app.add_url_rule('/call/<call>', 'call', view_func=api.views.call)

api/views.py

from flask import render_template

def call(call):
    # do api call code.
like image 173
bnlucas Avatar answered Oct 12 '22 10:10

bnlucas


A decorator is just a special function.

routed_galleries = application.route('/api/galleries')(api.get_galleries)

And in fact, depending on what the decorator does you may not need to keep the result around at all.

application.route('/api/galleries')(api.get_galleries)
like image 27
Ignacio Vazquez-Abrams Avatar answered Oct 12 '22 09:10

Ignacio Vazquez-Abrams