Py.Cafe

Sindhup24/

solara-river-flow-forecast

Interactive River Flow Forecast Visualization

DocsPricing
  • app.py
  • merged_output.csv
  • requirements.txt
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
import pandas as pd
from pathlib import Path
import solara
import matplotlib.pyplot as plt
import folium
import folium.plugins

# Load the uploaded CSV file
file_path = 'merged_output.csv'
df_forecast = pd.read_csv(file_path)

df_forecast['Date'] = df_forecast['Date'].astype(str)

# Function to filter data based on RiverNumber and Date
def forecast_filter(df, river_values, date_values):
    df_river = df.loc[df['RiverNumber'].isin(river_values)]
    df_date = df_river.loc[df_river['Date'].isin(date_values)]
    return df_date

# Example filtered data
dff_forecast = forecast_filter(df_forecast, [110123714], ['2024040100'])

# Function to plot charts
def forecast_charts(df):
    ensemble_means = df.iloc[:, :52].mean()
    ensemble_means = ensemble_means.reset_index()

    fig, ax = plt.subplots(figsize=(10, 5))
    ax.bar(ensemble_means['index'], ensemble_means[0])
    ax.set_title('Ensemble Forecast Means')
    display(fig)
    plt.close(fig)

forecast_charts(dff_forecast)

# Function to create map
def forecast_map(df):
    latitude = df['XCoordinate'].mean()
    longitude = df['YCoordinate'].mean()

    forecast_map = folium.Map(location=[latitude, longitude], zoom_start=12)

    incidents = folium.plugins.MarkerCluster().add_to(forecast_map)

    for lat, lng, label in zip(df.YCoordinate, df.XCoordinate, df.Date):
        folium.Marker(
            location=[lat, lng],
            icon=None,
            popup=label,
        ).add_to(incidents)

    display(forecast_map)

forecast_map(dff_forecast)

import solara
river_numbers = solara.reactive([110123714],)
dates = solara.reactive(['2024040100'])
limit = solara.reactive(2)

@solara.component
def View():
    dff = forecast_filter(df_forecast, river_numbers.value, dates.value)
    row_count = len(dff)
    if row_count > limit.value:
        solara.Warning(f"Only showing the first {limit.value} of {row_count:,} forecasts on map")
    forecast_map(dff.iloc[:limit.value])
    if row_count > 0:
        forecast_charts(dff)
    else:
        solara.Warning("You filtered out all the data, no charts shown")
View()

limit.value = 70
river_numbers.value = [110123714]

solara.SelectMultiple('RiverNumber', all_values=[str(k) for k in df_forecast['RiverNumber'].unique().tolist()], values=river_numbers)

@solara.component
def Controls():
    solara.SelectMultiple('RiverNumber', all_values=[str(k) for k in df_forecast['RiverNumber'].unique().tolist()], values=river_numbers)
    solara.SelectMultiple('Date', all_values=[str(k) for k in df_forecast['Date'].unique().tolist()], values=dates)
    solara.Text("Maximum number of rows to show on map")
    solara.SliderInt('', value=limit, min=1, max=1000)
Controls()

categories.value = [*categories.value, 'Warrants']

@solara.component
def Page():
    with solara.Sidebar():
        Controls()
    View()
Page()