Spaces:
Sleeping
Sleeping
adding actual data to all places in the st app
Browse files
streamlit_src/controllers/user_controller.py
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
|
|
|
|
|
| 1 |
from models.air_quality_model import AirQualityModel
|
| 2 |
from views.user_view import UserView
|
| 3 |
-
import numpy as np
|
| 4 |
|
| 5 |
|
| 6 |
class UserController:
|
| 7 |
def __init__(self):
|
| 8 |
self.model = AirQualityModel()
|
| 9 |
self.view = UserView()
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def show_dashboard(self):
|
| 13 |
# Get today's data and predictions
|
|
@@ -22,11 +29,18 @@ class UserController:
|
|
| 22 |
|
| 23 |
# Display current data and predictions
|
| 24 |
self.view.show_current_data(today_data, who_guidelines)
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
self.view.raise_awareness_and_quiz(
|
| 28 |
-
today_data, who_guidelines, question_nr=question_choice
|
| 29 |
)
|
|
|
|
|
|
|
| 30 |
plot_type = self.view.view_option_selection()
|
| 31 |
if plot_type == "Line Plot":
|
| 32 |
self.view.display_predictions_lineplot(next_three_days, who_guidelines)
|
|
@@ -40,5 +54,3 @@ class UserController:
|
|
| 40 |
|
| 41 |
# Print sources
|
| 42 |
self.view.print_sources()
|
| 43 |
-
|
| 44 |
-
self.is_first_run = False
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
from models.air_quality_model import AirQualityModel
|
| 4 |
from views.user_view import UserView
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
class UserController:
|
| 8 |
def __init__(self):
|
| 9 |
self.model = AirQualityModel()
|
| 10 |
self.view = UserView()
|
| 11 |
+
|
| 12 |
+
# Ensure session state for first run and quiz question
|
| 13 |
+
if "is_first_run" not in st.session_state:
|
| 14 |
+
st.session_state.is_first_run = True
|
| 15 |
+
|
| 16 |
+
if "question_choice" not in st.session_state:
|
| 17 |
+
st.session_state.question_choice = np.random.randint(0, 5)
|
| 18 |
|
| 19 |
def show_dashboard(self):
|
| 20 |
# Get today's data and predictions
|
|
|
|
| 29 |
|
| 30 |
# Display current data and predictions
|
| 31 |
self.view.show_current_data(today_data, who_guidelines)
|
| 32 |
+
|
| 33 |
+
# Use session state to avoid resetting the quiz question after each interaction
|
| 34 |
+
if st.session_state.is_first_run:
|
| 35 |
+
st.session_state.question_choice = np.random.randint(0, 5)
|
| 36 |
+
st.session_state.is_first_run = False
|
| 37 |
+
|
| 38 |
+
# Raise awareness and display the quiz with the saved question number
|
| 39 |
self.view.raise_awareness_and_quiz(
|
| 40 |
+
today_data, who_guidelines, question_nr=st.session_state.question_choice
|
| 41 |
)
|
| 42 |
+
|
| 43 |
+
# Plot selection
|
| 44 |
plot_type = self.view.view_option_selection()
|
| 45 |
if plot_type == "Line Plot":
|
| 46 |
self.view.display_predictions_lineplot(next_three_days, who_guidelines)
|
|
|
|
| 54 |
|
| 55 |
# Print sources
|
| 56 |
self.view.print_sources()
|
|
|
|
|
|
streamlit_src/models/air_quality_model.py
CHANGED
|
@@ -1,18 +1,17 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
import os
|
| 4 |
|
| 5 |
PREDICTION_PATH = os.path.join(
|
| 6 |
os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
|
| 7 |
"data",
|
| 8 |
-
"
|
| 9 |
-
"
|
| 10 |
)
|
| 11 |
|
| 12 |
PAST_DATA_PATH = os.path.join(
|
| 13 |
os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
|
| 14 |
"data",
|
| 15 |
-
"
|
| 16 |
"last_three_days.csv",
|
| 17 |
)
|
| 18 |
|
|
@@ -22,13 +21,6 @@ class AirQualityModel:
|
|
| 22 |
self.WHO_NO2_LEVEL = 25
|
| 23 |
self.WHO_O3_LEVEL = 100
|
| 24 |
|
| 25 |
-
def get_today_data_fake(self):
|
| 26 |
-
today_data = {
|
| 27 |
-
"NO2 (µg/m³)": np.random.uniform(20, 60), # Simulated data
|
| 28 |
-
"O3 (µg/m³)": np.random.uniform(50, 120), # Simulated data
|
| 29 |
-
}
|
| 30 |
-
return today_data
|
| 31 |
-
|
| 32 |
def get_today_data(self):
|
| 33 |
"""
|
| 34 |
Returns the air quality data for today from the past 3 days data.
|
|
@@ -41,26 +33,29 @@ class AirQualityModel:
|
|
| 41 |
A pandas Series with the air quality data for today.
|
| 42 |
"""
|
| 43 |
last_three_days = self.get_last_three_days()
|
| 44 |
-
today = last_three_days.iloc[
|
| 45 |
return today
|
| 46 |
|
| 47 |
def next_three_day_predictions(self):
|
| 48 |
# Load data from the specified path (assuming it's a CSV file)
|
| 49 |
data = pd.read_csv(PREDICTION_PATH)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
# Create a DataFrame with the relevant information for the next three days
|
| 52 |
next_three_days = pd.DataFrame(
|
| 53 |
{
|
| 54 |
"Day": ["Day 1", "Day 2", "Day 3"],
|
| 55 |
"NO2 (µg/m³)": [
|
| 56 |
-
data["NO2"].values[0],
|
| 57 |
data["NO2 + day 1"].values[0],
|
| 58 |
data["NO2 + day 2"].values[0],
|
|
|
|
| 59 |
],
|
| 60 |
"O3 (µg/m³)": [
|
| 61 |
-
data["O3"].values[0],
|
| 62 |
data["O3 + day 1"].values[0],
|
| 63 |
data["O3 + day 2"].values[0],
|
|
|
|
| 64 |
],
|
| 65 |
}
|
| 66 |
)
|
|
|
|
| 1 |
import pandas as pd
|
|
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
PREDICTION_PATH = os.path.join(
|
| 5 |
os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
|
| 6 |
"data",
|
| 7 |
+
"model_predictions",
|
| 8 |
+
"prediction_data.csv",
|
| 9 |
)
|
| 10 |
|
| 11 |
PAST_DATA_PATH = os.path.join(
|
| 12 |
os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
|
| 13 |
"data",
|
| 14 |
+
"model_predictions",
|
| 15 |
"last_three_days.csv",
|
| 16 |
)
|
| 17 |
|
|
|
|
| 21 |
self.WHO_NO2_LEVEL = 25
|
| 22 |
self.WHO_O3_LEVEL = 100
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def get_today_data(self):
|
| 25 |
"""
|
| 26 |
Returns the air quality data for today from the past 3 days data.
|
|
|
|
| 33 |
A pandas Series with the air quality data for today.
|
| 34 |
"""
|
| 35 |
last_three_days = self.get_last_three_days()
|
| 36 |
+
today = last_three_days.iloc[0]
|
| 37 |
return today
|
| 38 |
|
| 39 |
def next_three_day_predictions(self):
|
| 40 |
# Load data from the specified path (assuming it's a CSV file)
|
| 41 |
data = pd.read_csv(PREDICTION_PATH)
|
| 42 |
|
| 43 |
+
# correct data is todays data row
|
| 44 |
+
data = data[data["date"] == pd.Timestamp.now().strftime("%Y-%m-%d")]
|
| 45 |
+
|
| 46 |
# Create a DataFrame with the relevant information for the next three days
|
| 47 |
next_three_days = pd.DataFrame(
|
| 48 |
{
|
| 49 |
"Day": ["Day 1", "Day 2", "Day 3"],
|
| 50 |
"NO2 (µg/m³)": [
|
|
|
|
| 51 |
data["NO2 + day 1"].values[0],
|
| 52 |
data["NO2 + day 2"].values[0],
|
| 53 |
+
data["NO2 + day 3"].values[0],
|
| 54 |
],
|
| 55 |
"O3 (µg/m³)": [
|
|
|
|
| 56 |
data["O3 + day 1"].values[0],
|
| 57 |
data["O3 + day 2"].values[0],
|
| 58 |
+
data["O3 + day 3"].values[0],
|
| 59 |
],
|
| 60 |
}
|
| 61 |
)
|