2

I programmed a Flask route (let's say '/post_data') to receive data from a remote sensor through a POST request.

I'd like to show that data with Dash with some dropdown to customize the graph responsively and that updates automatically when new data arrives at the Flask route.

import dash
import flask


app = dash.Dash(__name__)
app.layout = [
   # ... some  dash_core_components ...
   dcc.Graph(id='mygraph'),
]
server = app.server

@server.route('/post_data', methods=['GET', 'POST'])
def post_data():
    if request.method == 'POST':
        data = eval(request.data.decode('utf8'))


@app.callback(
    [Output('mygraph', 'figure')],
    [Input('mydropdown1', 'value'), ...],
)
def update_mygraph(mydropdown1_value, ...):
    # QUESTION: how to get data from post_data?
    # some elaboration on data based on dropdown values
    fig = px.scatter(data, x="x", y="y")
    return fig


if __name__ == '__main__':
    app.run_server()

What I don't understand is: how to share the data from the Flask route with the Dash callback?

Should I store the dataset in the Flask session? Is then possible to fetch the dataset from the session in the dash callback? How?

user11696358
  • 356
  • 1
  • 15
  • The "duplicate" question is not a duplicate: I don't want to share data between Flask routes but between a Flask route and a Dash callback and they are not the same (at least not obviously and if so an explaining answer should be provided linking to the "duplicate" question). – user11696358 Sep 13 '21 at 10:00
  • Some usefull informations about this question can be found here https://community.plotly.com/t/update-plot-in-dash-from-rest-api/10612 – user11696358 Sep 14 '21 at 12:13

1 Answers1

0

Only approaches that come into my mind are:

  1. Write all data into a file and then access the file from the callback.
  2. Save all data into a database and then access the db from the callback.
  3. Maybe you could pass the data as url params. On the callback you could access the flask session, maybe. This one is just a conjecture.

An alternative solution could be just to insert the data on the same dashboard... Upload Component

Antjes
  • 70
  • 1
  • 8
  • I tried to access the flask session from the dash callback but I got "RuntimeError: Working outside of request context. This typically means that you attempted to use functionality that needed an active HTTP request." – user11696358 Sep 13 '21 at 10:03
  • I don't understand your last suggestion: what do you mean by "insert the data on the same dashboard"? thank you – user11696358 Sep 13 '21 at 10:04