title: Flask - Store values in memory between requests
body: <p>I have a Single page application - Angularjs on the front and flask on the backend which lets the user upload a file (xlsx, csv...) and then interactively analyze/query the file</p>
  
  <p>Essentially the user loads the file into memory on first upload and then subsequent ajax calls will tap into this file in memory. Im not sure how to keep the file in memory between subsequent requests (ajax).</p>
  
  <p>The <code>g</code> variable is erased after each request and if I understand right used to access values across a request (set usually by <code>before_request</code> and available all through the <code>views</code></p>
  
  <p>The request context is local to the request. I did manage to set the value on the current_app and then was able to access this in my subsequent ajax calls</p>
  
  <pre><code># On my first file upload, i load the file into memory
  and set it to a variable on current_app:
  
  from flask import current_app
  @app.route('/upload', methods =['POST'])
  def upload():
     ...
     upload file into memory
     ...
     current_app.file = file_in_memory
  
  
  
  @app.route('/subsequent_call')
  def subsequent():
      # i'm able to access the file in memory through 
      the current_app.file which i set earlier
  
      return current_app.file.number_of_lines()
  </code></pre>
  
  <p>This method of storing the file in memory on the current_app just doesnt seem right and feels too dirty/hackish. Would this scale at all ? </p>
  
  <p>I could pickle the file after every request and pull it back up on each request. But Storing/pickling and refetching the file every time into memory when a user is interactively querying the data seems too heavy/inefficient</p>
  
  <p>Is there any other elegant/right way to do this, app_context, werkzeug locals etc ? Or am i thinking about it all wrong ?</p>