1

I have a python server using the flask framework.

My application is a chatbot that talks to the user and performs some calculations in a dataset provided by the user.

I am already able to upload this data to the server, but when I try to use the data it is not loaded anymore. So I need to use this data until the user disconnect from my server.

Reading the Flask Documentation, I found some information that can lead me to what I need. The Application Context

Am I in the right direction?

This is how I am uploading the file in the server:

@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
     # check if the post request has the file part
    if 'file' not in request.files:
        flash('No file part')
        return redirect(request.url)
    file = request.files['file']
    # if user does not select file, browser also
    # submit a empty part without filename
    if file.filename == '':
        flash('No selected file')
        return redirect(request.url)
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None)
        csv_input = pd.read_csv(stream,sep=None,engine='python')
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        session['receive_count'] = session.get('receive_count', 0) + 1
        return render_template('index.html', async_mode=socketio.async_mode)

return render_template('index.html', async_mode=socketio.async_mode)   
Pedro Silva
  • 137
  • 1
  • 13
  • Is it a file upload? Where are you storing the file? Are you reading the file and then storing the data in a variable? Make a minimal code example (removing the things that are not specific to the problem you're having) – qff Dec 07 '17 at 12:35
  • Ok, I've just putted my uploading code part. I am storing the file in a variable that I created before the function. – Pedro Silva Dec 07 '17 at 12:55

1 Answers1

1

There are multiple solutions to your problem.

First thing is that the file variable here is local to the index()-function. If you want to use a variable you defined outside of the function (and it's not imported like request and session) you need to start the function with the line global file.

I would store the filename in session, then read the file again when you need the data. That way it will be local to the user, and you're not storing large amounts data in memory. Plus, if you use session.permanent = True it will be the stored for month for that "user" regardless of server and browser restarts.

Edit: This answer originally stated that you can use the Flask g-object to store the global. However, the g-object is local to a request even though it lives in the application context. (i.e. data stored on the g-object is not accessible across requests, it's only accessible within the same request)

qff
  • 5,524
  • 3
  • 37
  • 62
  • So, if I understood right, my options are: 1 - I should declare a variable as global if I intent to use it outside my index function. @qff suggestion. 2 - to use a data source outside of Flask to hold global data. A database, memcached, or redis. What do you suggest guys? – Pedro Silva Dec 08 '17 at 11:13
  • 1) Yes. 2) If it's a personal/hobby project I would stick to the filename in the session and then load the file when needed. If it's a more serious project, or it takes a long time to load the file, store the fully loaded data in Redis (or another datastore you feel comfortable with). – qff Dec 08 '17 at 11:49