88import plotly .graph_objs as go
99from plotly .subplots import make_subplots
1010from werkzeug .middleware .profiler import ProfilerMiddleware
11+ from dotenv import load_dotenv
12+
13+ load_dotenv () # Load environment variables from .env file
1114
1215app = Flask (__name__ )
1316
1417# Determine the absolute path to the database file
1518BASE_DIR = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
1619DB_NAME = os .path .join (BASE_DIR , 'sensor_data.db' )
20+ DB_PATH = os .getenv ('DB_PATH' )
1721
1822def get_latest_data ():
23+ """Fetches the latest sensor data from the database."""
1924 try :
20- conn = sqlite3 .connect (DB_NAME )
25+ conn = sqlite3 .connect (DB_PATH )
2126 cursor = conn .cursor ()
2227 cursor .execute (
23- "SELECT date, co2, temperature, humidity FROM last_day_sensor_data ORDER BY id DESC LIMIT 1"
28+ """
29+ SELECT date, co2, temperature, humidity
30+ FROM last_day_sensor_data
31+ ORDER BY id DESC LIMIT 1
32+ """
2433 )
2534 row = cursor .fetchone ()
2635 if row :
@@ -38,8 +47,9 @@ def get_latest_data():
3847 conn .close ()
3948
4049def fetch_last_day ():
50+ """Fetches the last day's sensor data from the database."""
4151 try :
42- conn = sqlite3 .connect (DB_NAME )
52+ conn = sqlite3 .connect (DB_PATH )
4353 query = "SELECT date, co2, temperature, humidity FROM last_day_sensor_data;"
4454 df = pd .read_sql_query (query , conn )
4555 df ['date' ] = pd .to_datetime (df ['date' ])
@@ -70,28 +80,41 @@ def resample_data(df, rule='1T'):
7080
7181@app .route ('/' )
7282def index ():
83+ """Renders the index page with sensor data plots."""
7384 df_orig = fetch_last_day ()
7485 if df_orig .empty :
7586 return render_template ('index.html' , graph_html = "No data available." )
76-
7787 # Resample data to one-minute intervals to reduce the number of points
7888 df = resample_data (df_orig , rule = '1T' )
7989
8090 fig = make_subplots (
8191 rows = 3 ,
8292 cols = 1 ,
8393 shared_xaxes = True ,
84- subplot_titles = ("CO₂ Levels Over Time" , "Temperature Over Time" , "Humidity Over Time" )
94+ subplot_titles = (
95+ "CO₂ Levels Over Time" ,
96+ "Temperature Over Time" ,
97+ "Humidity Over Time" ,
98+ ),
8599 )
86- fig .add_trace (go .Scatter (x = df ['date' ], y = df ['co2' ], mode = 'lines' , name = 'CO₂ over time' ), row = 1 , col = 1 )
87- fig .add_trace (go .Scatter (x = df ['date' ], y = df ['temperature' ], mode = 'lines' , name = 'Temperature (°C)' ), row = 2 , col = 1 )
88- fig .add_trace (go .Scatter (x = df ['date' ], y = df ['humidity' ], mode = 'lines' , name = 'Humidity (%)' ), row = 3 , col = 1 )
100+ fig .add_trace (
101+ go .Scatter (x = df ['date' ], y = df ['co2' ], mode = 'lines' , name = 'CO₂ over time' ), row = 1 , col = 1
102+ )
103+ fig .add_trace (
104+ go .Scatter (x = df ['date' ], y = df ['temperature' ], mode = 'lines' , name = 'Temperature (°C)' ),
105+ row = 2 ,
106+ col = 1
107+ )
108+ fig .add_trace (
109+ go .Scatter (x = df ['date' ], y = df ['humidity' ], mode = 'lines' , name = 'Humidity (%)' ), row = 3 , col = 1
110+ )
89111 fig .update_layout (title = 'Sensor Data Over Time' , xaxis_title = 'Time' , height = 800 )
90112 graph_html = fig .to_html (full_html = False )
91113 return render_template ('index.html' , graph_html = graph_html )
92114
93115@app .route ('/current' )
94116def current ():
117+ """Renders the current data page."""
95118 current_data = get_latest_data ()
96119 return render_template ('current.html' , current_data = current_data )
97120
0 commit comments