Mitch
Mitch

Reputation: 177

How to use a local variable in other functions flask?

I have a function that I want to call each time a web visitor hits '/'(home page). I would also line to use the results of that function in other functions. Any recommendations on how to do this?

@app.route('/')
def home():
   store = index_generator() #This is the local variable I would like to use 
   return render_template('home.html')

 @app.route('/home_city',methods = ['POST'])
 def home_city():
   CITY=request.form['city']
   request_yelp(DEFAULT_LOCATION=CITY,data_store=store) """I would like to use the results here"""
   return render_template('bank.html')

Upvotes: 9

Views: 4418

Answers (1)

PepperoniPizza
PepperoniPizza

Reputation: 9112

See the Flask session docs. You need to do some minor settings.

from flask import session

@app.route('/')
def home():
   store = index_generator()
   session['store'] = store
   return render_template('home.html')

 @app.route('/home_city',methods = ['POST'])
 def home_city():
   CITY=request.form['city']
   store = session.get('store')
   request_yelp(DEFAULT_LOCATION=CITY,data_store=store)
   return render_template('bank.html')

Upvotes: 5

Related Questions