1

I am receiving data from server (with library socket) periodically, lets say every second. And I want to make multiple live web graphs from these data, just with Python. I have this code right now

count_of_data = 100
X = deque(maxlen = count_of_data)
X.append(1)
  
Y = deque(maxlen = count_of_data)
Y.append(1)

app = dash.Dash(__name__)
  
app.layout = html.Div(
    [
        dcc.Graph(id = 'live-graph', animate = True),
        dcc.Interval(
            id = 'graph-update',
            interval = 1000,
            n_intervals = 0
        ),
    ]
)
 
 
@app.callback(
    Output('live-graph', 'figure'),
    [ Input('graph-update', 'n_intervals') ]
)


def update_graph_scatter(n):
    X.append(X[-1]+1)
    Y.append(random.randint(0, 100)) #this should take data from server
    data = plotly.graph_objs.Scatter(
            x=list(X),
            y=list(Y),
            name='Scatter',
            mode= 'lines+markers'
    )
    if max(X) < count_of_data:
            return {'data': [data],
            'layout' : go.Layout(xaxis=dict(range=[0, 100]),yaxis = dict(range = [0, 100]),)}   
    else:
        return {'data': [data],
            'layout' : go.Layout(xaxis=dict(range=[min(X), max(X)]),yaxis = dict(range = [0, 100]),)}


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

How can I transform it into function which updates graph on website every time I call it? This function I would like to use multiple times - create more graphs on one website - just for sure that it will be possible.

Simon
  • 11
  • 1

0 Answers0