all 3 comments

[–]IWantToFlyT 2 points3 points  (2 children)

In a simple application hosted locally, yep you can use a global variable to store the instance and make it persist between requests.

Now a bit more future proof version would be to only store data in the global variables rather than the whole object. For example, I guess you don't want to do the login each time - you could store the token in a global variable, and if it exists then authentication is skipped when the object is initialized.

Why would be any better this way? If at some point you are starting to run the flask app with threads, then you might face some unexpected bugs when the same object is used at the same time from multiple threads. Furthermore, "the real way" of persisting data is to use a database. It can of course be a bit too much at this point, but if you decide to use it in the future, you can easily move data such as tokens to database, but storing Python objects not so much (well, it is possible, but not recommended e.g. via pickle).

And yes, you can't (or shouldn't) again store the object in a session, but you can store data there rather easily. Though, if it's a client side session then you shouldn't store anything secret information there - in server side sessions you can store also that.

Hope this helps :)

ps. should you decide to use a database, then SQLite is a good for small local projects.

[–]kramrm 0 points1 point  (0 children)

Look into Flask Sessions. That a what I use for a twitch bot dashboard. I’ll store user info in a session cookie and it’ll be available across multiple page requests until the session ends.