Spaces:
Sleeping
Sleeping
Delete sr/1.streamlitapp.py
Browse files- src/1.streamlitapp.py +0 -188
src/1.streamlitapp.py
DELETED
|
@@ -1,188 +0,0 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import matplotlib.pyplot as plt
|
| 3 |
-
import bqplot as bq
|
| 4 |
-
import ipywidgets as widgets
|
| 5 |
-
import numpy as np
|
| 6 |
-
import requests
|
| 7 |
-
import os
|
| 8 |
-
import seaborn as sns
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Creating buttons for downloading and refreshing plots
|
| 12 |
-
download_button = widgets.Button(description="Download Dataset")
|
| 13 |
-
refresh_button = widgets.Button(description="Refresh Plots")
|
| 14 |
-
|
| 15 |
-
def fetch_and_save_data(_):
|
| 16 |
-
api_url = "https://api.open-meteo.com/v1/forecast?latitude=41.88&longitude=-87.63&hourly=temperature_2m,relative_humidity_2m,precipitation,cloudcover,windspeed_10m&timezone=auto"
|
| 17 |
-
|
| 18 |
-
try:
|
| 19 |
-
response = requests.get(api_url)
|
| 20 |
-
response.raise_for_status()
|
| 21 |
-
data = response.json() # we need to Parse JSON
|
| 22 |
-
|
| 23 |
-
# and then transform to csv
|
| 24 |
-
hourly = data['hourly']
|
| 25 |
-
df = pd.DataFrame(hourly)
|
| 26 |
-
df.to_csv("chicago_weather_data.csv", index=False)
|
| 27 |
-
print("Weather data Refreshed and downloaded")
|
| 28 |
-
|
| 29 |
-
except Exception as e:
|
| 30 |
-
pass
|
| 31 |
-
|
| 32 |
-
def refresh_plots(_):
|
| 33 |
-
try:
|
| 34 |
-
# Read from the saved CSV file
|
| 35 |
-
df_raw = pd.read_csv("chicago_weather_data.csv")
|
| 36 |
-
|
| 37 |
-
df_raw['time'] = pd.to_datetime(df_raw['time'], errors='coerce')
|
| 38 |
-
for col in ['temperature_2m', 'relative_humidity_2m', 'precipitation', 'cloudcover', 'windspeed_10m']:
|
| 39 |
-
df_raw[col] = pd.to_numeric(df_raw[col], errors='coerce')
|
| 40 |
-
global df
|
| 41 |
-
df = df_raw
|
| 42 |
-
|
| 43 |
-
print("Plots refreshed")
|
| 44 |
-
|
| 45 |
-
except Exception as e:
|
| 46 |
-
pass
|
| 47 |
-
|
| 48 |
-
fetch_and_save_data(None)
|
| 49 |
-
refresh_plots(None)
|
| 50 |
-
|
| 51 |
-
#buttons
|
| 52 |
-
print('The data and visuals has already been refreshed, but adding buttons for clarity')
|
| 53 |
-
download_button.on_click(fetch_and_save_data)
|
| 54 |
-
refresh_button.on_click(refresh_plots)
|
| 55 |
-
widgets.HBox([download_button, refresh_button])
|
| 56 |
-
|
| 57 |
-
#Plot3
|
| 58 |
-
|
| 59 |
-
variable_labels = {
|
| 60 |
-
'temperature_2m': 'Temperature',
|
| 61 |
-
'relative_humidity_2m': 'Humidity',
|
| 62 |
-
'precipitation': 'Precipitation',
|
| 63 |
-
'cloudcover': 'Cloud Cover'
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
dropdown = widgets.Dropdown(
|
| 67 |
-
options=[(label, key) for key, label in variable_labels.items()],
|
| 68 |
-
value='temperature_2m',
|
| 69 |
-
description='Select Variable:',
|
| 70 |
-
style={'description_width': 'initial'}
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
x_sc = bq.DateScale()
|
| 74 |
-
y_sc = bq.LinearScale()
|
| 75 |
-
|
| 76 |
-
line = bq.Lines(
|
| 77 |
-
x=df['time'],
|
| 78 |
-
y=df['temperature_2m'],
|
| 79 |
-
scales={'x': x_sc, 'y': y_sc},
|
| 80 |
-
display_legend=False,
|
| 81 |
-
stroke_width=2
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
x_ax = bq.Axis(label='Time', scale=x_sc)
|
| 86 |
-
y_ax = bq.Axis(label='Value', scale=y_sc, orientation='vertical')
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
fig = bq.Figure(
|
| 90 |
-
title='Chicago Temperature Over Time',
|
| 91 |
-
marks=[line],
|
| 92 |
-
axes=[x_ax, y_ax],
|
| 93 |
-
legend_location='top-left'
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
def update_plot(change):
|
| 98 |
-
var = dropdown.value
|
| 99 |
-
line.y = df[var]
|
| 100 |
-
line.x = df['time']
|
| 101 |
-
fig.title = f"{variable_labels[var]} Over Time"
|
| 102 |
-
|
| 103 |
-
dropdown.observe(update_plot, names='value')
|
| 104 |
-
|
| 105 |
-
# Show widgets
|
| 106 |
-
widgets.VBox([dropdown, fig])
|
| 107 |
-
|
| 108 |
-
import matplotlib.pyplot as plt
|
| 109 |
-
from mpl_toolkits.mplot3d import Axes3D
|
| 110 |
-
|
| 111 |
-
fig = plt.figure(figsize=(12, 6))
|
| 112 |
-
ax = fig.add_subplot(111, projection='3d')
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
scatter = ax.scatter(df['temperature_2m'], df['precipitation'], df['windspeed_10m'],
|
| 116 |
-
c=df['temperature_2m'], cmap='coolwarm', s=20, marker='o', alpha=0.7, edgecolors='black')
|
| 117 |
-
|
| 118 |
-
ax.set_xlabel('Temperature (°C)')
|
| 119 |
-
ax.set_ylabel('Precipitation')
|
| 120 |
-
ax.set_zlabel('Windspeed (km/h)')
|
| 121 |
-
plt.title('3D Scatterplot of Temperature, Precipitation, and Windspeed')
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
cbar = plt.colorbar(scatter, ax=ax, label='Temperature (°C)')
|
| 125 |
-
|
| 126 |
-
plt.show()
|
| 127 |
-
|
| 128 |
-
df['time'] = pd.to_datetime(df['time'], errors='coerce')
|
| 129 |
-
df['temperature_2m'] = pd.to_numeric(df['temperature_2m'], errors='coerce')
|
| 130 |
-
|
| 131 |
-
df['date_only'] = df['time'].dt.date
|
| 132 |
-
|
| 133 |
-
grouped = df.groupby('date_only')['temperature_2m'].agg(['min', 'mean', 'max']).reset_index()
|
| 134 |
-
|
| 135 |
-
slider = widgets.IntSlider(
|
| 136 |
-
value=0,
|
| 137 |
-
min=0,
|
| 138 |
-
max=len(grouped) - 1,
|
| 139 |
-
step=1,
|
| 140 |
-
description='Day Index:',
|
| 141 |
-
continuous_update=False
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
x_scale = bq.OrdinalScale()
|
| 145 |
-
y_scale = bq.LinearScale()
|
| 146 |
-
|
| 147 |
-
bar = bq.Bars(
|
| 148 |
-
x=['Min', 'Mean', 'Max'],
|
| 149 |
-
y=grouped.iloc[slider.value, 1:].tolist(),
|
| 150 |
-
scales={'x': x_scale, 'y': y_scale},
|
| 151 |
-
colors=['#76b5c5'],
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
-
x_axis = bq.Axis(scale=x_scale, label='Temperature Summary')
|
| 155 |
-
y_axis = bq.Axis(scale=y_scale, orientation='vertical', label='Temperature (°C)')
|
| 156 |
-
|
| 157 |
-
fig = bq.Figure(
|
| 158 |
-
marks=[bar],
|
| 159 |
-
axes=[x_axis, y_axis],
|
| 160 |
-
title=f"Temperature Summary for {grouped['date_only'].iloc[slider.value]}"
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
def update_bar(change):
|
| 164 |
-
i = slider.value
|
| 165 |
-
bar.y = grouped.iloc[i, 1:].tolist()
|
| 166 |
-
fig.title = f"Temperature Summary for {grouped['date_only'].iloc[i]}"
|
| 167 |
-
|
| 168 |
-
slider.observe(update_bar, names='value')
|
| 169 |
-
|
| 170 |
-
widgets.VBox([slider, fig])
|
| 171 |
-
|
| 172 |
-
def plot_wind_boxplot(df):
|
| 173 |
-
df['date_only'] = df['time'].dt.date
|
| 174 |
-
|
| 175 |
-
plt.figure(figsize=(12, 6))
|
| 176 |
-
sns.boxplot(data=df, x='date_only', y='windspeed_10m', palette='Set3')
|
| 177 |
-
|
| 178 |
-
plt.title('Daily Wind Speed Distribution in Chicago (km/h)', fontsize=15)
|
| 179 |
-
plt.xlabel('Date', fontsize=12)
|
| 180 |
-
plt.ylabel('Wind Speed (km/h)', fontsize=12)
|
| 181 |
-
plt.x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