Assuming you have already installed flask (pip install flask
), you may achieve what you want with a simple flask server app, here referenced as server.py
:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/data")
def data_route():
collected_data = [] # Your data
return jsonify(results=collected_data)
if __name__ == "__main__":
app.run()
You should load your data into collected_data
(I've used an empty list []
just as an example), by reading a file where you stored the results or by doing whatever computation you need.
Also, consider having your 'data collection script' running separately from the flask server, specially if it is computationally expensive, slow, or if the resulting data does change all the time:
- If your collection script returns the same results over and over, consider caching it. You may store it in many ways, but depending on your needs dumping the results to a simple file every time it runs, and serving the file contents through flask may be enough.
- If the resulting data is very big, you should take a look at how to stream content