Flask: Want to import file of helper functions
Just import your file as usual and use functions from it:
# foo.py
def bar():
return 'hey everyone!'
And in the main file:
# main.py
from flask import render_template
from app import app
from foo import bar
def getRankingList():
return 'hey everyone!'
@app.route("/")
@app.route("/index")
def index():
rankingsList = getRankingsList()
baz = bar() # Function from your foo.py
return render_template('index.html', rankingsList=rankingsList)
if __name__ == '__main__':
app.run(debug=True)
Simply have another python script file (for example helpers.py
) in the same directory as your main flask .py file.
Then at the top of your main flask file, you can do import helpers
which will let you access any function in helpers by adding helpers.
before it (for example helpers.exampleFunction()
).
Or you can do from helpers import exampleFunction
and use exampleFunction()
directly in your code. Or from helpers import *
to import and use all the functions directly in your code.