Skip to content Skip to sidebar Skip to footer

Basic Flask: Adding Helpful Functions

I've written a python script that works in terminal and am porting it to the web using Flask. I've gone through parts of a tutorial (specifically: http://blog.miguelgrinberg.com/p

Solution 1:

You're not restricted to one function per view -- you can have as many as you want.

from flask import Flask
app = Flask(__name__)

deff():
    ...
defg():
    ...
@app.route('/index')defindex():
    <here you can use f and g>
    ...

Functions don't need to correspond to views -- only the @app.route(...) decorator makes it do that.

If you have a large number of other functions, it couldn't hurt to put them in another file. Then you can import the file and use them as above.

Post a Comment for "Basic Flask: Adding Helpful Functions"