Dash by Plot.ly で live data update.

dash_by_plotly シリーズ。要はそれなりに苦労してるってことやな。

ラジオボタンでデータ切り替え

dash live-update application.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/local/bin/python3

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, Input
import plotly.graph_objs as go

app = dash.Dash(__name__)

app.layout = html.Div([
  html.Div([
    dcc.Graph(id='scatter'),
    dcc.RadioItems(id='radio',
      options=[{'label':'A', 'value':'A'}, {'label':'B', 'value':'B'}]),
  ]),
])

@app.callback(
  Output('scatter', 'figure'),
  [Input('radio', 'value')])
def update_scatter(value):

  traces = list()

  if value == 'A':
    traces.append(go.Scatter(x=[1,2,3,4,5], y=[10,12,15,12,14], name=value))
  elif value == 'B':
    traces.append(go.Scatter(x=[1,2,3,4,5], y=[8,6,12,3,9], name=value))
  else:
    pass

  return {'data': traces}

if __name__ == '__main__':
  app.run_server(debug=True, host='127.0.0.1', port='8050')

備考

2018/Oct/17頃書いた。