From 9c29ab825b7089da618a38e512aa0c92ebccb24d Mon Sep 17 00:00:00 2001 From: Konstantin Koslowski Date: Sun, 3 Nov 2019 16:38:01 +0100 Subject: [PATCH] homecontrol-dash: first attempt at visualization --- .gitignore | 4 +++ Pipfile | 14 +++++++++ homecontrol-dash.py | 76 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 .gitignore create mode 100644 Pipfile create mode 100755 homecontrol-dash.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61c9711 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/* +Pipfile.lock +dash +dcc diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..01dfe71 --- /dev/null +++ b/Pipfile @@ -0,0 +1,14 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +dash = "*" +dash-daq = "*" +requests = "*" + +[requires] +python_version = "3.7" diff --git a/homecontrol-dash.py b/homecontrol-dash.py new file mode 100755 index 0000000..2929941 --- /dev/null +++ b/homecontrol-dash.py @@ -0,0 +1,76 @@ +#!/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 + +x = [1] +y = [2] + +external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] + +app = dash.Dash(__name__, external_stylesheets=external_stylesheets) + +available_sensors = ["refresh first"] + +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='Available Sensors' + ), + 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 = "http://localhost:5000/sensors/get" + res = requests.get(url) + content = res.json() + return [{'label': content[i]["type"], 'value': i} for i in content] + + +@app.callback( + Output('example-graph', 'figure'), + [Input('refresh-button', 'n_clicks'), Input('select-sensor', 'value')]) +def update_graph(n_clicks, sensor): + url = "http://localhost:5000/sensors/get_values/%s" % sensor + res = requests.get(url) + content = res.json() + x = [time.strftime("%Y%m%d-%H%M%S", time.localtime(float(i))) for i in content] + y = [content[i] for i in content] + + return { + 'data': [ + {'x': x, 'y': y, 'mode': 'markers', 'name': sensor} + ], + 'layout': { + 'title': 'Data for sensor: %s' % sensor + } + + } + +if __name__ == '__main__': + + app.run_server(debug=True) \ No newline at end of file