File size: 6,275 Bytes
35e0c07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import folium
from folium import LinearColormap
import requests
from datetime import datetime, timedelta
from streamlit_folium import st_folium

# Set page layout to wide
st.set_page_config(layout="wide", page_title="Real-Time Relative Humidity Data Dashboard")

# Function to load data
@st.cache_data(ttl=300)  # Cache data to avoid reloading every time
def load_data():
    with st.spinner("Loading data..."):
        response = requests.get("https://csdi.vercel.app/weather/rhum")
        data = response.json()
        features = data['features']
        df = pd.json_normalize(features)
        df.rename(columns={
            'properties.Relative Humidity(percent)': 'Relative Humidity (%)',
            'properties.Automatic Weather Station': 'Station Name',
            'geometry.coordinates': 'Coordinates'
        }, inplace=True)
        return df

# Check if the data has been loaded before
if 'last_run' not in st.session_state or (datetime.now() - st.session_state.last_run) > timedelta(minutes=5):
    st.session_state.df = load_data()
    st.session_state.last_run = datetime.now()

# Data
df = st.session_state.df

# Compute statistics
humidity_data = df['Relative Humidity (%)']
avg_humidity = humidity_data.mean()
max_humidity = humidity_data.max()
min_humidity = humidity_data.min()
std_humidity = humidity_data.std()

# Create three columns
col1, col2, col3 = st.columns([1.65, 2, 1.15])

# Column 1: Histogram and statistics
with col1:
    # Define colors for gradient
    color_scale = ['#58a0db', '#0033cc']

    # Create histogram
    fig = px.histogram(df, x='Relative Humidity (%)', nbins=20,
                       labels={'Relative Humidity (%)': 'Relative Humidity (%)'},
                       title='Relative Humidity Histogram',
                       color_discrete_sequence=color_scale)

    # Add average line
    fig.add_shape(
        go.layout.Shape(
            type="line",
            x0=avg_humidity,
            y0=0,
            x1=avg_humidity,
            y1=df['Relative Humidity (%)'].value_counts().max(),
            line=dict(color="red", width=2, dash="dash"),
        )
    )

    # Update layout
    fig.update_layout(
        xaxis_title='Relative Humidity (%)',
        yaxis_title='Count',
        title='Relative Humidity Distribution',
        bargap=0.2,
        title_font_size=20,
        xaxis_title_font_size=14,
        yaxis_title_font_size=14,
        height=350,
        shapes=[{
            'type': 'rect',
            'x0': min_humidity,
            'x1': max_humidity,
            'y0': 0,
            'y1': df['Relative Humidity (%)'].value_counts().max(),
            'fillcolor': 'rgba(0, 100, 255, 0.2)',
            'line': {
                'color': 'rgba(0, 100, 255, 0.2)',
                'width': 0
            },
            'opacity': 0.1
        }]
    )

    # Add annotations
    fig.add_annotation(
        x=avg_humidity,
        y=df['Relative Humidity (%)'].value_counts().max() * 0.9,
        text=f"Average: {avg_humidity:.2f}%",
        showarrow=True,
        arrowhead=1
    )

    st.plotly_chart(fig, use_container_width=True)

    # Display statistics
    col_1, col_2 = st.columns([1, 1])
    with col_1:
        st.metric(label="Average R.Humidity (%)", value=f"{avg_humidity:.2f}")
        st.metric(label="Minimum R.Humidity (%)", value=f"{min_humidity:.2f}")
    with col_2:
        st.metric(label="Maximum R.Humidity (%)", value=f"{max_humidity:.2f}")
        st.metric(label="Std. Dev (%)", value=f"{std_humidity:.2f}")

# Function to convert humidity to color based on gradient
def humidity_to_color(humidity, min_humidity, max_humidity):
    norm_humidity = (humidity - min_humidity) / (max_humidity - min_humidity)
    if norm_humidity < 0.5:
        r = int(173 + (0 - 173) * (2 * norm_humidity))
        g = int(216 + (0 - 216) * (2 * norm_humidity))
        b = int(230 + (255 - 230) * (2 * norm_humidity))
    else:
        r = int(0 + (0 - 0) * (2 * (norm_humidity - 0.5)))
        g = int(0 + (0 - 0) * (2 * (norm_humidity - 0.5)))
        b = int(255 + (0 - 255) * (2 * (norm_humidity - 0.5)))

    return f'rgb({r}, {g}, {b})'

# Column 2: Map
with col2:
    with st.spinner("Loading map..."):
        m = folium.Map(location=[22.3547, 114.1483], zoom_start=11, tiles='CartoDB positron')
        min_humidity = df['Relative Humidity (%)'].min()
        max_humidity = df['Relative Humidity (%)'].max()

        colormap = LinearColormap(
            colors=['#58a0db', 'blue'],
            index=[min_humidity, max_humidity],
            vmin=min_humidity,
            vmax=max_humidity,
            caption='Relative Humidity (%)'
        )
        colormap.add_to(m)

        for _, row in df.iterrows():
            humidity = row['Relative Humidity (%)']
            color = humidity_to_color(humidity, min_humidity, max_humidity)

            folium.Marker(
                location=[row['Coordinates'][1], row['Coordinates'][0]],
                popup=f"<p style='font-size: 12px; background-color: white; padding: 5px; border-radius: 5px;'>{row['Station Name']}: {humidity:.1f}%</p>",
                icon=folium.DivIcon(
                    html=f'<div style="font-size: 10pt; color: {color}; padding: 2px; border-radius: 5px;">'
                         f'<strong>{humidity:.1f}%</strong></div>'
                )
            ).add_to(m)

        st_folium(m, width=500, height=600)

# Column 3: Data Table
with col3:
    st.markdown(
        """
        <style>
        .dataframe-container {
            height: 600px;
            overflow-y: auto;
        }
        .dataframe th, .dataframe td {
            text-align: left;
            padding: 8px;
        }
        </style>
        """,
        unsafe_allow_html=True
    )

    # Rename column for display
    df_display = df[['Station Name', 'Relative Humidity (%)']].rename(columns={'Relative Humidity (%)': 'R.Humidity'})
    st.dataframe(df_display, height=600)

# Refresh Button
if st.button("Refresh Data"):
    with st.spinner("Refreshing data..."):
        st.session_state.df = load_data()
        st.session_state.last_run = datetime.now()
        st.experimental_rerun()