#!/usr/bin/env python3 import dash import dash_core_components as dcc import dash_html_components as html from dash.dependencies import Input, Output import time import requests URL_BASE="http://innocence:5000" x = [1] y = [2] external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=external_stylesheets) available_sensors = ["undefined"] app.layout = html.Div(children=[ html.H1(children='homecontrol-dash'), html.Div(children=''' homecontrol-dash: visualize data '''), dcc.Graph( id='example-graph', figure={ 'data': [ {'x': x, 'y': y, 'mode': 'markers', 'name': 'SF'} ], 'layout': { 'title': 'initial values' } } ), dcc.Dropdown( id='select-sensor', options=[{'label': i, 'value': i} for i in available_sensors], value="undefined" ), html.Button(id='refresh-button', n_clicks=0, children='refresh') ]) @app.callback( Output('select-sensor', 'options'), [Input('refresh-button', 'n_clicks')]) def update_list(n_clicks): url = "%s/sensor/get" % URL_BASE print(url) res = requests.get(url) content = res.json() options = [{'label': content[i]["sensorType"], 'value': i} for i in content] return options @app.callback( Output('example-graph', 'figure'), [Input('refresh-button', 'n_clicks'), Input('select-sensor', 'value')]) def update_graph(n_clicks, sensorId): if not sensorId == "undefined": # url = "%s/sensor/get_values/%s?limit=10" % (URL_BASE, sensorId) url = "%s/sensor/get_values/%s" % (URL_BASE, sensorId) res = requests.get(url) content = res.json() v = content[sensorId]["values"] sensorType = content[sensorId]["sensorType"] x = [time.strftime("%m%d-%H%M%S", time.localtime(float(v[i]["ts"]))) for i in range(len(v))] y = [v[i]["value"] for i in range(len(v))] else: sensorType = sensorId x = [1] y = [1] return { 'data': [ {'x': x, 'y': y, 'mode': 'markers', 'name': "%s (%s)" % (sensorType, sensorId)} ], 'layout': { 'title': 'Data for sensor: %s (%s)' % (sensorType, sensorId) } } if __name__ == '__main__': app.run_server(debug=True)