1

I am building a simple Flask website. I read CSV file through pd.read_csv, analyze it and output the results to a Flask template.

The CSV file is updated everyday. However the results on a website are not changing, until I restart the server.

To give you a little overview,

I am reading CSV file as follows:

data = pd.read_csv('Data.csv', 
                   parse_dates=True, 
                   infer_datetime_format=True, 
                   index_col='Date')

I assigned values and functions to template variable, for example:

    # Variable 1
    mean = data['BTC'].mean() 
    # Function 1
    def std():
        return data['ETH'].std()

        # Passing variables to template
        return render_template('page.html', mean = mean, std = std())

And I have lots of variables that I pass to a template.

My app_run code snippet is as follows:

if __name__ == '__main__':
    host = os.getenv('IP', '0.0.0.0')
    port = int(os.getenv('PORT', 5000))
    app.debug = True
    app.secret_key = 'longlistofweirdcharacters'
    app.run(host = host, port = port)

What should I do, if I want changes in Data.csv to reflect on a template without restarting the server?

  • You question is a bit vague, are you looking to repeat the function call every 24 hours? Or on an endpoint call ? My guess is you are looking for a cron job similar. You could use celery or just refer to a simpler implementation on this question. https://stackoverflow.com/questions/21214270/flask-run-function-every-hour – Ilhicas Sep 06 '17 at 15:42

1 Answers1

0

It's not clear where that call to read_csv is. But it should be within the view function that renders the template.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Well, that's exactly my point. If you want it to reload on every request, it needs to be in the view itself. – Daniel Roseman Sep 06 '17 at 17:38
  • Tried it with a sample variable. It worked. Unfortunately, I have tons of operations / functions over that dataframe. Looks like there is no way other than cluttering the View – user2968773 Sep 06 '17 at 18:18
  • @DanielRoseman This seems to be the ultimate solution. But if the files are huge, the load times are high and the memory on the server keeps getting occupied if we have multiple users using trying to achieve the same result. Any suggestions on performance using this approach ? – Vijay Sep 18 '18 at 09:47