Spaces:
Sleeping
Sleeping
nice viz
Browse files- app_streamlit.py +90 -12
- requirements.txt +1 -0
app_streamlit.py
CHANGED
@@ -1,23 +1,101 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
-
|
5 |
-
st.title('Lahore Air Quality!')
|
6 |
-
st.subheader('Particle matter, diameter < 2.5 micrometers (PM2.5)')
|
7 |
-
|
8 |
-
### Load data
|
9 |
-
|
10 |
import datetime
|
11 |
import hopsworks
|
12 |
from functions import util
|
13 |
import os
|
14 |
import pickle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import datetime
|
5 |
import hopsworks
|
6 |
from functions import util
|
7 |
import os
|
8 |
import pickle
|
9 |
+
import plotly.express as px
|
10 |
+
|
11 |
+
|
12 |
+
st.set_page_config(
|
13 |
+
page_title="Ex-stream-ly Cool App",
|
14 |
+
page_icon="🧊",
|
15 |
+
layout="wide",
|
16 |
+
initial_sidebar_state="expanded",
|
17 |
+
menu_items={
|
18 |
+
'Get Help': 'https://www.extremelycoolapp.com/help',
|
19 |
+
'Report a bug': "https://www.extremelycoolapp.com/bug",
|
20 |
+
'About': "# This is a header. This is an *extremely* cool app!"
|
21 |
+
}
|
22 |
+
)
|
23 |
+
|
24 |
+
st.title('Lahore Air Quality!')
|
25 |
+
st.subheader('Particle matter, diameter < 2.5 micrometers (PM2.5)')
|
26 |
+
|
27 |
+
pickle_file_path = 'air_quality_df.pkl'
|
28 |
+
|
29 |
+
with open(pickle_file_path, 'rb') as file:
|
30 |
+
st.session_state.df = pickle.load(file).sort_values(by="date")
|
31 |
+
|
32 |
+
# st.line_chart(st.session_state.df,x='date',y='pm25',width=2000,height=800,use_container_width=False)
|
33 |
+
|
34 |
+
# Define the number of last elements to show
|
35 |
+
n = 10
|
36 |
+
|
37 |
+
# Extract the x-axis values (dates) from the dataframe
|
38 |
+
x_values = st.session_state.df["date"]
|
39 |
+
|
40 |
+
# Define your colors, labels, and ranges
|
41 |
+
colors = ['green', 'yellow', 'orange', 'red', 'purple', 'darkred']
|
42 |
+
labels = ['Good', 'Moderate', 'Unhealthy for Some', 'Unhealthy', 'Very Unhealthy', 'Hazardous']
|
43 |
+
ranges = [(1, 49), (50, 99), (100, 149), (150, 199), (200, 299), (300, 500)] # Avoid 0 for log scale
|
44 |
+
|
45 |
+
# Create the Plotly Express line chart
|
46 |
+
fig = px.line(
|
47 |
+
st.session_state.df,
|
48 |
+
x="date",
|
49 |
+
y="pm25",
|
50 |
+
markers=True
|
51 |
+
)
|
52 |
+
|
53 |
+
# Add background color rectangles using `shapes`
|
54 |
+
shapes = []
|
55 |
+
for i, (start, end) in enumerate(ranges):
|
56 |
+
shapes.append(
|
57 |
+
dict(
|
58 |
+
type="rect", # Add a rectangle
|
59 |
+
xref="paper", # Extend the rectangle across the entire x-axis
|
60 |
+
yref="y", # Anchor the rectangle to the y-axis
|
61 |
+
x0=0, # Start from the left (x0 in paper coordinates)
|
62 |
+
x1=1, # End at the right (x1 in paper coordinates)
|
63 |
+
y0=start, # Start of the y-range
|
64 |
+
y1=end, # End of the y-range
|
65 |
+
fillcolor=colors[i], # Background color
|
66 |
+
opacity=0.2, # Transparency level
|
67 |
+
layer="below", # Place behind the data
|
68 |
+
line_width=0 # No border
|
69 |
+
)
|
70 |
+
)
|
71 |
|
72 |
+
# Update the layout and traces for customization
|
73 |
+
fig.update_traces(
|
74 |
+
marker=dict(size=10), # Increase marker size
|
75 |
+
line=dict(width=3) # Make the line thicker
|
76 |
+
)
|
77 |
|
78 |
+
fig.update_layout(
|
79 |
+
shapes=shapes, # Add the background rectangles
|
80 |
+
xaxis=dict(
|
81 |
+
range=[x_values.iloc[-n], x_values.iloc[-1]], # Dynamically set the range
|
82 |
+
title=dict(
|
83 |
+
text="Date", # Set x-axis label
|
84 |
+
font=dict(size=20) # Increase font size for the x-axis label
|
85 |
+
),
|
86 |
+
),
|
87 |
+
yaxis=dict(
|
88 |
+
title=dict(
|
89 |
+
text="PM2.5 Concentration", # Set y-axis label
|
90 |
+
font=dict(size=20) # Increase font size for the y-axis label
|
91 |
+
),
|
92 |
+
type="log", # Set y-axis to logarithmic scale
|
93 |
+
fixedrange=True # Disable vertical panning/zooming
|
94 |
+
),
|
95 |
+
autosize=True,
|
96 |
+
width=2100,
|
97 |
+
height=900,
|
98 |
+
)
|
99 |
|
100 |
+
# Render the chart in Streamlit
|
101 |
+
st.plotly_chart(fig)
|
requirements.txt
CHANGED
@@ -23,3 +23,4 @@ hopsworks
|
|
23 |
pandas
|
24 |
#modal
|
25 |
streamlit
|
|
|
|
23 |
pandas
|
24 |
#modal
|
25 |
streamlit
|
26 |
+
plotly
|