Forums

How to call a function in Flask?

I have this function in a file, (calendarfunction.py) which I want to 'call' from the main app file (flask_app.py) and then use the calender for the main route ie:madmartin.pythonanywhere.com/

here's the code:

def calendar_today():
now = datetime.datetime.now()
c = calendar.TextCalendar(calendar.SUNDAY)
month = c.formatmonth(now.year, now.month)
print(month)

calendar_today()

so how to call from within flask_app.py?

deleted

so how to convert this (flask_app.py):

from flask import Flask
from calendarfunction import calendar_today
app = Flask(__name__)

@app.route('/') def hello_world(): return 'Hello MadMartin, PA5,from Flask!

basically I want the last line to do something like:

'return the function def_calendartoday()'

is it possible to do this in a straighforward manner without having to do blueprints etc??

for example, putting this in:

from flask import Flask

from calendarfunction import calendar_today

app = Flask(name)

@app.route('/')

calendar_today()

just throws up a syntax error

Check out this blog post on converting a function to a Flask website.

ok, thanks