File size: 4,999 Bytes
30a5cd1 8497557 675bb54 8497557 30a5cd1 675bb54 30a5cd1 675bb54 30a5cd1 675bb54 30a5cd1 675bb54 30a5cd1 675bb54 30a5cd1 675bb54 30a5cd1 675bb54 30a5cd1 |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
import time
import altair as alt
import joblib
import numpy as np
import pandas as pd
import streamlit as st
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import plotly.graph_objects as go
from helper_functions import custom_metric_box, pollution_box
import plotly.graph_objects as go
import streamlit as st
import pandas as pd
import numpy as np
st.set_page_config(
page_title="Utrecht Pollution Dashboard",
page_icon="🏂��🌱",
layout="wide",
initial_sidebar_state="expanded")
alt.themes.enable("dark")
# App Title
st.title("Utrecht Pollution Dashboard 🌱")
@st.cache_resource(ttl=6*300) # Reruns every 6 hours
def run_model():
# Load or train your model (pretrained model in this case)
model = joblib.load("linear_regression_model.pkl")
# Static input values
input_data = pd.DataFrame({
'Temperature': [20.0],
'Wind Speed': [10.0],
'Humidity': [50.0]
})
# Run the model with static input
prediction = model.predict(input_data)
return prediction
col1, col2 = st.columns((1,1))
# Create a 3-column layout
with col1:
st.subheader('Current Weather')
col1, col2, col3 = st.columns(3)
# First column
with col1:
custom_metric_box(label="Temperature", value="2 °C", delta="-3 °C")
custom_metric_box(label="Humidity", value="60 %", delta="-1 %")
# Second column
with col2:
custom_metric_box(label="Pressure", value="1010 hPa", delta="+2 hPa")
custom_metric_box(label="Precipitation", value="5 mm", delta="-1 mm")
# Third column
with col3:
custom_metric_box(label="Solar Radiation", value="200 W/m²", delta="-20 W/m²")
custom_metric_box(label="Wind Speed", value="15 km/h", delta="-2 km/h")
st.subheader('Current Pollution Levels')
col1, col2 = st.columns((1,1))
# Display the prediction
#st.write(f'Predicted Pollution Level: {prediction[0]:.2f}')
with col1:
pollution_box(label="O<sub>3</sub>", value="37 µg/m³", delta="+2 µg/m³")
with col2:
pollution_box(label="NO<sub>2</sub>", value="28 µg/m³", delta="+3 µg/m³")
prediction = run_model() # Assuming you have a function run_model()
# Sample data (replace with your actual data)
dates_past = pd.date_range(end=pd.Timestamp.today(), periods=7).to_list()
dates_future = pd.date_range(start=pd.Timestamp.today() + pd.Timedelta(days=1), periods=3).to_list()
# O3 and NO2 values for the past 7 days
o3_past_values = [30, 32, 34, 33, 31, 35, 36]
no2_past_values = [20, 22, 21, 23, 22, 24, 25]
# Predicted O3 and NO2 values for the next 3 days
o3_future_values = [37, 38, 40]
no2_future_values = [26, 27, 28]
# Combine dates and values
dates = dates_past + dates_future
o3_values = o3_past_values + o3_future_values
no2_values = no2_past_values + no2_future_values
# Create a DataFrame
df = pd.DataFrame({
'Date': dates,
'O3': o3_values,
'NO2': no2_values
})
st.subheader('O3 and NO2 Prediction')
# Create two columns for two separate graphs
subcol1, subcol2 = st.columns(2)
# Plot O3 in the first subcolumn
with subcol1:
fig_o3 = go.Figure()
fig_o3.add_trace(go.Scatter(x=df['Date'], y=df['O3'],
mode='lines+markers',
name='O3',
line=dict(color='rgb(0, 191, 255)', width=4))) # Bright blue
# Add a vertical line for predictions (today's date)
fig_o3.add_shape(
dict(
type="line",
x0=pd.Timestamp.today(), x1=pd.Timestamp.today(),
y0=min(o3_values), y1=max(o3_values),
line=dict(color="White", width=3, dash="dash"),
)
)
fig_o3.update_layout(
plot_bgcolor='rgba(0, 0, 0, 0)', # Transparent background
paper_bgcolor='rgba(0, 0, 0, 0)', # Transparent paper background
yaxis_title="O3 Concentration (µg/m³)",
font=dict(size=14),
hovermode="x unified"
)
st.plotly_chart(fig_o3)
# Plot NO2 in the second subcolumn
with subcol2:
fig_no2 = go.Figure()
fig_no2.add_trace(go.Scatter(x=df['Date'], y=df['NO2'],
mode='lines+markers',
name='NO2',
line=dict(color='rgb(255, 20, 147)', width=4))) # Bright pink
# Add a vertical line for predictions (today's date)
fig_no2.add_shape(
dict(
type="line",
x0=pd.Timestamp.today(), x1=pd.Timestamp.today(),
y0=min(no2_values), y1=max(no2_values),
line=dict(color="White", width=3, dash="dash"),
)
)
fig_no2.update_layout(
plot_bgcolor='rgba(0, 0, 0, 0)', # Transparent background
paper_bgcolor='rgba(0, 0, 0, 0)', # Transparent paper background
yaxis_title="NO2 Concentration (µg/m³)",
font=dict(size=14),
hovermode="x unified"
)
st.plotly_chart(fig_no2) |