ThePloki ThePloki - 2 months ago 22
Python Question

GAE Flask global variable persistent across browsers

Here's my Flask setup:

main.py


from flask import Flask
from app.views import main_bp

app = Flask(__name__)
app.register_blueprint(main_bp, url_prefix='')


app.config.py
(Shortened with "..." for this example)

CONFIG = {
'SITE_NAME': 'Test Site',
'SSL': False,
'DEBUG': True,
'LOGGED_IN': False,
...
}


views.py


from config import CONFIG

@main_bp.route('/')
@main_bp.route('/<page_slug>/')
def fallback(page_slug='home'):
# Check if logged in
if CONFIG['LOGGED_IN']:
return 'Logged In'
else
return 'Logged Out'


Now if I change
CONFIG['LOGGED_IN']
to
True
in one of my browsers (Let's say Google Chrome), and then open another browser to the website (Let's say Firefox), then I'm already logged in on both. If I then log out in Firefox and refresh Chrome, I'm logged out on both.

When I was using Django I never noticed a problem like this... my global CONFIG variable is persisting across browsers. Weird!

Do I need to make my CONFIG into a class? Maybe this only happens when running through dev_appserver.py and won't happen in production? I'm still learning, so please be nice! Thanks :)

Note: Please ignore the blatant security issues with this example. I assure you this is not how I plan to use this code.

Answer

As davidism pointed out, I should not be storing variables that change per session as a global. I will instead decide on these variables after loading pertinent session variables like UID and Session ID.

Thanks.

Update: Athough, I find it interesting that Flask documentation would advocate setting globals via g (see: http://flask.pocoo.org/docs/0.11/api/)

Comments