I am building a web app with Python Flask with JavaScript. I am a beginner of Javascript.
The process I do now:
In Flask Python code,
1. I get data by scrapping the web (numeric data that updates every minute).
2. use the data and calculate something and get the final numbers.
3. make a list that contains the final numbers
4. feed the list to a page by adding the list to the page's definition of Flask
5. Now in HTML get the list by capturing it with {{ data|safe }} tag
6. use it with Javascript to make a chart.
The problem is: In the step 1, the data I get is being updated every minute. For example, on that web page now there are 15 data points. I parse the last 10 data points from that web page and then I put them in a list in my Python and then do the following steps and make a chart on my webpage. One minute later, in the data source web page, there will be 16 data point available, and I need to get the last 10 data points. In that case, I need to run the python code again to get the latest 10 data points to use them to make a chart on my web page.
So, I need to always run the whole python code which is the whole Flask app init.py file and re-render my webpage to see the updated chart. If I do not re-run the init.py file in my server, then even after 10 minutes or 2 hours, I will only see the data that I parsed for the first time forever.
How should I run the Flask and always get the updated data without always re-run the flask init.py every time.
I thought about using time.sleep(60) so that the flask app python file is run every 1 minutes. But this is really taking alot of time when my code gets much more thinks to calculate. And do not really work.
How should I solve this problem??
Should I use time.sleep ? or is threre better way?