-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
87 lines (77 loc) · 2.35 KB
/
app.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import psycopg2
import pandas as pd
import plotly.express as px
from dash import Dash, dcc, html, Input, Output
# Connect to the PostgreSQL database
def get_data(sensor_id=None, start_date=None, end_date=None):
conn = psycopg2.connect(
host=os.getenv("DB_HOST"),
database=os.getenv("DB_NAME"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PASSWORD"),
)
query = "SELECT * FROM sensor_data WHERE 1=1"
params = []
if sensor_id:
query += " AND sensor_id = %s"
params.append(sensor_id)
if start_date:
query += " AND time >= %s"
params.append(start_date)
if end_date:
query += " AND time <= %s"
params.append(end_date)
df = pd.read_sql(query, conn, params=params)
df["temperature_fahrenheit"] = df["temperature"] * 9 / 5 + 32
df["time_tz"] = df["time"].dt.tz_convert("US/Central")
conn.close()
return df
app = Dash(__name__)
app.layout = html.Div(
[
html.H1("Sensor Data Dashboard"),
dcc.Dropdown(
id="sensor_id",
options=[
{"label": "Sensor 1", "value": "1"},
{"label": "Sensor 2", "value": "2"},
],
placeholder="Select a sensor",
),
dcc.DatePickerRange(
id="date_range",
start_date_placeholder_text="Start Date",
end_date_placeholder_text="End Date",
),
dcc.Graph(id="temperature_graph"),
dcc.Graph(id="humidity_graph"),
]
)
@app.callback(
[Output("temperature_graph", "figure"), Output("humidity_graph", "figure")],
[
Input("sensor_id", "value"),
Input("date_range", "start_date"),
Input("date_range", "end_date"),
],
)
def update_graph(sensor_id, start_date, end_date):
df = get_data(sensor_id, start_date, end_date)
temp_fig = px.line(
df, x="time_tz", y="temperature_fahrenheit", title="Temperature Over Time"
)
humidity_fig = px.line(df, x="time_tz", y="humidity", title="Humidity Over Time")
return temp_fig, humidity_fig
debug = False
dev_tools_hot_reload = False
if os.environ.get("ENV") == "dev":
debug = True
dev_tools_hot_reload = True
if __name__ == "__main__":
app.run(
host="0.0.0.0",
port=8050,
debug=debug,
dev_tools_hot_reload=dev_tools_hot_reload,
)