-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
403 lines (322 loc) · 15.7 KB
/
app.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import streamlit as st
import pandas as pd
from pymongo import MongoClient
import plotly.express as px
from datetime import datetime
import plotly.graph_objects as go
import colorsys
import matplotlib.pyplot as plt
import seaborn as sns
# Initialize MongoDB connection
client = MongoClient('mongodb://localhost:27017/')
mongo_database = 'meteo_data'
def lighten_color(color, amount):
"""
Lightens a given color by a specified amount (0-1).
"""
try:
h, l, s = colorsys.rgb_to_hls(*color)
l = (l + amount) % 1
return colorsys.hls_to_rgb(h, l, s)
except (TypeError, ValueError):
# Handle potential errors with color input
return color
# Function to fetch Meteo data
def fetch_meteo_data(selected_date):
db = client[mongo_database]
collection = db['summary']
return collection.find_one({'summary_date': selected_date})
# Function to fetch Earthquake data
def fetch_earthquake_data():
# Assuming earthquake data is stored in a similar manner but in a different collection/database
db = client['earthquake_db'] # Update with your actual database name
collection = db['summaries'] # Update with your actual collection name
return pd.DataFrame(list(collection.find())) # Convert the MongoDB cursor to a DataFrame
# Function to display Meteo statistics and charts
def display_meteo_tab():
collection = client[mongo_database]['summary']
dates_list = [entry['summary_date'] for entry in collection.find({}, {'summary_date': 1})]
selected_date = st.sidebar.selectbox('Select a date', dates_list,index=0)
selected_data = fetch_meteo_data(selected_date)
# Utiliser une boîte de sélection pour choisir le type de visualisation
visualization_type = st.sidebar.selectbox("Choose Visualization Type", ["Histogram","Table", "Boxplot"] )
# Format the date string for better readability
if isinstance(selected_date, str):
formatted_date = datetime.strptime(selected_date, "%Y-%m-%dT%H:%M:%S.%fZ").strftime("%Y-%m-%d %H:%M:%S")
else:
formatted_date = selected_date.strftime("%Y-%m-%d %H:%M:%S")
st.header(f"Statistics for the date: {formatted_date}")
# Extracting additional information (25%, 50%, 75%) for each variable
data = {
"Temperature (°C)": {
"min": selected_data["min"]["t_2m:C"],
"max": selected_data["max"]["t_2m:C"],
"std": selected_data["std"]["t_2m:C"],
"mean": selected_data["mean"]["t_2m:C"],
"25%": selected_data["25%"]["t_2m:C"],
"50%": selected_data["50%"]["t_2m:C"],
"75%": selected_data["75%"]["t_2m:C"],
},
"Precipitations (mm)": {
"min": selected_data["min"]["precip_1h:mm"],
"max": selected_data["max"]["precip_1h:mm"],
"std": selected_data["std"]["precip_1h:mm"],
"mean": selected_data["mean"]["precip_1h:mm"],
"25%": selected_data["25%"]["precip_1h:mm"],
"50%": selected_data["50%"]["precip_1h:mm"],
"75%": selected_data["75%"]["precip_1h:mm"],
},
"Vitesse du vent (m/s)": {
"min": selected_data["min"]["wind_speed_10m:ms"],
"max": selected_data["max"]["wind_speed_10m:ms"],
"std": selected_data["std"]["wind_speed_10m:ms"],
"mean": selected_data["mean"]["wind_speed_10m:ms"],
"25%": selected_data["25%"]["wind_speed_10m:ms"],
"50%": selected_data["50%"]["wind_speed_10m:ms"],
"75%": selected_data["75%"]["wind_speed_10m:ms"],
},
"Pression au niveau de la mer (hPa)": {
"min": selected_data["min"]["msl_pressure:hPa"],
"max": selected_data["max"]["msl_pressure:hPa"],
"std": selected_data["std"]["msl_pressure:hPa"],
"mean": selected_data["mean"]["msl_pressure:hPa"],
"25%": selected_data["25%"]["msl_pressure:hPa"],
"50%": selected_data["50%"]["msl_pressure:hPa"],
"75%": selected_data["75%"]["msl_pressure:hPa"],
},
}
if visualization_type == "Boxplot":
for variable, values in data.items():
fig = px.box(y=list(values.values()), labels={"value": variable})
fig.update_layout(title=f"Statistics for {variable}", yaxis_title="Values")
st.plotly_chart(fig)
elif visualization_type == "Histogram":
bar_names = ['min', '25%', 'mean', '75%', 'max']
for variable, values in data.items():
if values:
data_values = [values[stat] for stat in bar_names]
# Create a figure with the specified colors
fig = go.Figure(layout_template='plotly_white') # Optional: Set a white background
for stat, value in zip(bar_names, data_values):
stat_label = f'{stat} (Zero)' if value == 0 else stat
color = "Blue" if value != 0 else "Red"
fig.add_trace(go.Bar(x=[stat_label], y=[value], name=f'{stat}', marker_color=color))
# Add spacing and layout
fig.update_layout(
title=f"Distribution of {variable}",
xaxis_title="Statistics",
yaxis_title="Values",
barmode='group',
bargap=0.2
)
st.plotly_chart(fig)
else:
st.warning(f"No data available for {variable}")
elif visualization_type == "Table":
for variable, values in data.items():
# Créer un tableau HTML pour chaque variable
html_table = """
<table>
<tr>
<th>Variable</th>
<th>Minimum</th>
<th>25%</th>
<th>Moyenne</th>
<th>75%</th>
<th>Maximum</th>
<th>Ecart-type</th>
</tr>
"""
html_table += f"""
<tr>
<td>{variable}</td>
<td>{values["min"]}</td>
<td>{values["25%"]}</td>
<td>{values["mean"]}</td>
<td>{values["75%"]}</td>
<td>{values["max"]}</td>
<td>{values["std"]}</td>
</tr>
"""
html_table += "</table>"
st.subheader(variable)
# Afficher le tableau HTML
st.write(html_table, unsafe_allow_html=True)
# Function to fetch Open Weather data
def fetch_open_weather_data():
db = client['open_weather_data']
collection = db['open_weather_summary']
return pd.DataFrame(list(collection.find()))
# Function to display Open Weather information
def display_open_weather():
data = fetch_open_weather_data()
selected_variable = st.sidebar.selectbox("Choose Variable To Display", ["temperature", "feels_like", "pression", "humidite"] )
# Display a table with the fetched data
st.write("Paris Open Weather Data Summary")
st.table(data[['temperature', 'feels_like', 'description', 'pression', 'humidite']])
# Create histograms using seaborn
st.write(selected_variable, "Histogram")
fig, ax = plt.subplots(figsize=(4, 2)) # Adjust the figsize as needed
sns.histplot(data[selected_variable], kde=True, ax=ax, binwidth=0.5)
st.pyplot(fig)
# Create box plot for temperature
st.write(selected_variable, "Box Plot")
fig, ax = plt.subplots(figsize=(4, 2))
sns.boxplot(x=data[selected_variable], ax=ax, width=0.5)
st.pyplot(fig)
# Helper function to convert timestamp to a Python datetime object
def parse_timestamp(timestamp_str):
return datetime.strptime(timestamp_str, "%Y-%m-%dT%H:%M:%S.%fZ")
# Function to fetch filtered Earthquake data
def fetch_filtered_earthquake_data(region=None, start_date=None, end_date=None):
db = client['earthquake_db'] # Update with your actual database name
collection = db['summaries'] # Update with your actual collection name
query = {}
if region:
query["adresse"] = {"$regex": region, "$options": "i"} # Case-insensitive match
if start_date and end_date:
query["timestamp"] = {"$gte": start_date.isoformat(), "$lte": end_date.isoformat()}
earthquakes = list(collection.find(query))
for eq in earthquakes:
#print(eq["timestamp"])
try:
eq["timestamp"] = parse_timestamp(eq["timestamp"])
except:
pass
return pd.DataFrame(earthquakes)
def get_unique_regions():
db = client['earthquake_db'] # Update with your actual database name
collection = db['summaries'] # Update with your actual collection name
# Get unique 'adresse' entries and extract the region part
addresses = collection.distinct('adresse')
regions = set(address.split(', ')[-1] for address in addresses if ', ' in address) # Extract region after the comma
return list(regions)
# Enhanced function to display Earthquake data on an interactive map
def display_earthquake_tab():
st.title("Earthquake Data Visualization")
# Get unique regions for the sidebar selection
unique_regions = get_unique_regions()
unique_regions.insert(0, 'All regions') # Add 'All regions' as the first option
selected_region = st.sidebar.selectbox("Select a region", unique_regions, index=0) # Default to 'All regions'
region =selected_region
start_date = st.sidebar.date_input("Start date", value=None)
end_date = st.sidebar.date_input("End date", value=None)
# If 'All regions' is not selected, filter by the selected region
if selected_region != 'All regions':
earthquake_data = fetch_filtered_earthquake_data(selected_region, start_date, end_date)
else:
earthquake_data = fetch_filtered_earthquake_data(None, start_date, end_date)
if not earthquake_data.empty:
if ((start_date==None) and (end_date==None)):
st.write(f"Displaying earthquakes for : {region}")
elif ((start_date!=None) and (end_date==None)):
st.write(f"Displaying earthquakes for region: {region} from {start_date}")
elif ((start_date==None) and (end_date!=None)):
passt.write(f"Displaying earthquakes for region: {region} until {end_date}")
else:
st.write(f"Displaying earthquakes for region: {region} from {start_date} to {end_date}")
# Ensure latitude and longitude are numeric for plotting
earthquake_data["latitude"] = pd.to_numeric(earthquake_data["latitude"])
earthquake_data["longitude"] = pd.to_numeric(earthquake_data["longitude"])
earthquake_data["depth"] = pd.to_numeric(earthquake_data["depth"])
earthquake_data["mag"] = pd.to_numeric(earthquake_data["mag"])
fig = px.scatter_mapbox(earthquake_data, lat="latitude", lon="longitude",
hover_name="adresse", hover_data=["mag", "depth"],
color="mag", size="mag",
color_continuous_scale=["green", "yellow", "red"],
size_max=15, height=600) # Adjusted zoom level for a broader view
# Update layout to use OpenStreetMap and remove margins
fig.update_layout(
mapbox_style="open-street-map",
width=1100,
margin={"r":0,"t":0,"l":0,"b":0},
mapbox=dict(
center=dict(lat=0, lon=0), # Center on the Equator and Prime Meridian
zoom=1.5 # Adjust zoom level as needed
)
)
# Display the map in Streamlit
st.plotly_chart(fig, use_container_width=True)
else:
st.write("No earthquake data available for the selected filters.")
# --------------------------------- Stock Data ---------------------------------
def fetch_stock_data():
db = client['donnees_bourse']
collection = db['resume_donnees_bourse']
df = pd.DataFrame(list(collection.find()))
df['timestamp'] = pd.to_datetime(df['timestamp'])
return df
def filtered_stock_data(stock_name=None,start_date=None, end_date=None):
db = client['donnees_bourse']
collection = db['resume_donnees_bourse']
unfiltered = pd.DataFrame(list(collection.find()))
unfiltered['timestamp'] = pd.to_datetime(unfiltered['timestamp'])
if stock_name:
unfiltered = unfiltered[unfiltered['stock_name'] == stock_name]
if start_date:
unfiltered = unfiltered[unfiltered['timestamp'] >= pd.to_datetime(start_date)]
if end_date:
unfiltered = unfiltered[unfiltered['timestamp'] <= pd.to_datetime(end_date)]
return unfiltered
def stock_names():
db = client['donnees_bourse']
collection = db['resume_donnees_bourse']
return collection.distinct('stock_name')
def display_stock_tab():
st.title("Stock Prices")
db = client['donnees_bourse']
collection = db['resume_donnees_bourse']
unique_sotck_names = stock_names()
unique_sotck_names.insert(0, 'All Stocks')
selected_stock = st.sidebar.selectbox("Select a stock", unique_sotck_names, index=0)
start_date = st.sidebar.date_input("Start date", value=None)
end_date = st.sidebar.date_input("End date", value=None)
visualization_type = st.sidebar.selectbox("Choose Visualization Type", ["line plot","candle plot"] )
if selected_stock != 'All Stocks':
data = filtered_stock_data(selected_stock, start_date, end_date)
else:
data = filtered_stock_data(None, start_date, end_date)
if not data.empty:
if ((start_date==None) and (end_date==None)):
st.write(f"Displaying Stock prices for : {selected_stock}")
elif ((start_date!=None) and (end_date==None)):
st.write(f"Displaying Stock prices for : {selected_stock} from {start_date}")
elif ((start_date==None) and (end_date!=None)):
st.write(f"Displaying Stock prices for : {selected_stock} until {end_date}")
else:
st.write(f"Displaying Stock prices for : {selected_stock} from {start_date} to {end_date}")
if visualization_type == "line plot":
data.set_index('timestamp', inplace=True)
fig = px.line(data, x=data.index, y='4. close', color='stock_name', labels={'4. close': 'Closing Price'})
fig.update_layout(title='Closing Prices of 3 Stocks Over Time', xaxis_title='Date', yaxis_title='Closing Price')
st.plotly_chart(fig)
elif visualization_type == "candle plot":
import plotly.graph_objects as go
if selected_stock == 'All Stocks':
st.write("Candle plot is not available for all stocks, please select one stock")
return
else:
fig = go.Figure(data=[go.Candlestick(x=data.index,
open=data['1. open'],
high=data['2. high'],
low=data['3. low'],
close=data['4. close'])])
fig.update_layout(xaxis_title='Date', yaxis_title='Price')
st.plotly_chart(fig)
# Main app structure
def main():
st.set_page_config(page_title="Real Time Dashboard", page_icon=':star', layout='wide')
st.sidebar.title("Data Selection")
# Ajouter 2 tabs de ilyes et mazigh
app_mode = st.sidebar.selectbox("Choose the data you want to view:", [ "Meteo","Earthquakes", "Stock", "Open_Weather"])
# ici aussi
if app_mode == "Meteo":
display_meteo_tab()
elif app_mode == "Earthquakes":
display_earthquake_tab()
elif app_mode == "Stock":
display_stock_tab()
elif app_mode == "Open_Weather":
display_open_weather()
if __name__ == "__main__":
main()