Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')
like image 804
Mitch Avatar asked May 13 '15 02:05

Mitch


People also ask

What is variable-name in flask?

Flask – Variable Rules. This variable part is marked as <variable-name>. It is passed as a keyword argument to the function with which the rule is associated. In the following example, the rule parameter of route () decorator contains <name> variable part attached to URL ‘/hello’. Hence, if the...

How do I create a dynamic URL in flask?

Flask – Variable Rules. It is possible to build a URL dynamically, by adding variable parts to the rule parameter. This variable part is marked as <variable-name>. It is passed as a keyword argument to the function with which the rule is associated.

Is it possible to integrate flask with Matplotlib?

Im a student learning how to use flask and planning to integrate it with Matplotlib ( The graph maker library). I have got inputs from the user and stored them as variables. However, the variables are not callable upon in later code. This is my code for flask:

How do I exit a code loop in flask?

Using abort function to exit from the code loop, in conjunction to redirect. Here the code refers to a set of codes that will generate some error, as applicable, and stop any further execution of the application. How does redirect Function Works in Flask?


1 Answers

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')
like image 113
PepperoniPizza Avatar answered Oct 23 '22 06:10

PepperoniPizza