Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

how to reinitialise global variable in flask app python

$
0
0

Here is the Flask app.py

from flask import Flask, redirect, url_for, requestimport trainerapp = Flask(__name__)@app.route('/success/<name>')def success(name):    gValue = trainer.myFunction()    name = name + gValue    return 'welcome %s' % name@app.route('/login', methods=['POST', 'GET'])def login():    if request.method == 'POST':        user = request.form['nm']        return redirect(url_for('success', name=user))    else:        user = request.args.get('nm')        return redirect(url_for('success', name=user))if __name__ == '__main__':    app.run(debug=True, threaded=True, use_reloader=False)

HTML file

<html><body>   <form action = "http://localhost:5000/login" method = "post"><p>Enter Name:</p><p><input type = "text" name = "nm" /></p><p><input type = "submit" value = "submit" /></p></form>  </body></html>

trainer.py

from utils import sumValuedef myFunction():   pullValue = sumValue()   return pullValue

utils.py

aggregateValue = " defaultValue"def sumValue():    global aggregateValue    x = " mainValue"    x1 = x + aggregateValue    aggregateValue = " modifiedValue"    x2 = x1 + aggregateValue    print(x2)    return x2

running the app

C:\test>python app.py * Serving Flask app 'app' * Debug mode: onWARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5000Press CTRL+C to quit127.0.0.1 - - [09/Jan/2024 16:12:57] "POST /login HTTP/1.1" 302 - mainValue defaultValue modifiedValue127.0.0.1 - - [09/Jan/2024 16:12:57] "GET /success/Value HTTP/1.1" 200 -127.0.0.1 - - [09/Jan/2024 16:15:59] "POST /login HTTP/1.1" 302 - mainValue modifiedValue modifiedValue127.0.0.1 - - [09/Jan/2024 16:15:59] "GET /success/Value HTTP/1.1" 200 -

in the first run we get

mainValue defaultValue modifiedValue

in 2nd run

mainValue modifiedValue modifiedValue

as the 2nd run is a fresh run, I expect the middle value should be defaultValue instead of modifiedValue,

The global declaration in utils.py is not getting initialized during 2nd run,

If we Ctrl+C and start again, memory is cleared to get defaultValue

May I know how to initialize the global variable here?


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>