[Tutor] Flask File Uploads and Sessions
Alan Gauld
alan.gauld at yahoo.co.uk
Fri May 8 18:24:48 EDT 2020
On 08/05/2020 17:22, Doran, Harold C. wrote:
... 3) User chooses a variable from the drop down menu and the mean of
that column is computed.
>
> I am successful with both (1) and (2) but have now spent more than 4 days trying to solve (3)
> When running this, I can read in a file and my drop down is populated as expected....
> I believe the problem is that my file orig_df is not stored in a session.
Indeed. It dies as soon as the function terminates because that's what
local variables in functions do. If you want something to persist
between calls you will need to store it somewhere, either as a local
file or in a database or possibly in an application level global -
although if you have multiple users that's almost certainly a bad
idea!
> So, what I want is for the data orig_df to be read in from the UI and
> reusable for things I would want to do with it
The normal approach would be to store it in a database somehow.
Maybe SQL if the data collumns are standard or maybe a document centric
database like Mongo if the data is more open ended. It may come down to
what your web server supports.
If that fails just store the original file and then re-read it for each
request but that becomes slow if you have big files.
> ...global variable or even stored on the server,
If its not on the server how can the server see it? It would need
to download it each time. I don't know how your R code is doing it but
it must be storing it somehow, either in memory or in a file/database.
>the file should be available to the user only during their browser session
> and then it would vaporize when they close their browser session.
It might be possible to stor it(or the filename?) as a cookie and then
upload the file each time they connect. But tat will require some smart
client side JavaScript as well as server side code to support it.
All in all the simplest approach is to store the data you need on the
server along with some kind of key to identify the associated user and
session.(which may well be in a cookie)
> def data_tools_upload():
> if request.method == 'POST':
> orig_df = pd.read_csv(request.files.get('file'))
This is a local in-memory variable on the server.
> var2use = request.form.get("vars2use")
> vars = list(orig_df.columns)
> mean = orig_df[vars[4]].mean()
> dims = orig_df.shape
> message = 'You have data! There are %s rows and %s columns and the variable %s has mean %s' % (dims[0],dims[1],vars[4],round(mean,3))
> table = orig_df.head(10).to_html(classes='data', header = "true")
> return render_template('upload2.html', tables = [table], message = message, vars = vars, showVar=var2use)
> return render_template('upload2.html')
And it goes out of scope here and will be deleted.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list