-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
59 lines (50 loc) · 2.67 KB
/
streamlit_app.py
File metadata and controls
59 lines (50 loc) · 2.67 KB
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
#create a streamlit app where you can choose multiple models, scenarios and regions to plot
#with matplotlib
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
uploaded_file = st.file_uploader("Upload a file", type=["csv", "xlsx", "xls"])
if uploaded_file is not None:
file_extension = uploaded_file.name.split(".")[-1].lower()
#Check for csv
if file_extension == "csv":
df = pd.read_csv(uploaded_file)
#Check for xlsx or xls
else:
df = pd.read_excel(uploaded_file)
#lower case
df = df.applymap(lambda x: x.lower() if isinstance(x, str) else x)
model_values = st.multiselect("Select Models:", df["Model"].unique())
scenario_values = st.multiselect("Select Scenarios:", df["Scenario"].unique())
region_values = st.multiselect("Select Regions:", df["Region"].unique())
variable_value = st.selectbox("Select a Variable:", df["Variable"].unique())
if model_values and scenario_values and region_values and variable_value:
filtered_df = df[(df["Model"].isin(model_values)) &
(df["Scenario"].isin(scenario_values))&
(df["Region"].isin(region_values))&
(df["Variable"]== variable_value)]
if not filtered_df.empty:
plt.figure(figsize=(10, 6))
for model in model_values:
for scenario in scenario_values:
for region in region_values:
subset = filtered_df[(filtered_df["Model"] == model) &
(filtered_df["Scenario"] == scenario) &
(filtered_df["Region"] == region)]
if not subset.empty:
row_location = subset.index[0]
row_values = subset.loc[row_location]
values_to_plot = row_values[[col for col in df.columns if str(col).isdigit()]].astype(float)
label = f"{model}, {scenario}, {region}"
plt.plot(values_to_plot.index, values_to_plot.values, marker='o', linestyle='-', label=label)
plt.title(f"Line Plot for {variable_value}")
plt.xlabel("Years")
plt.ylabel(df.loc[row_location, "Unit"])
plt.grid(True)
plt.legend()
st.pyplot(plt)
else:
st.warning('No data available for the selected choices.')
st.write(filtered_df)
else:
st.warning('Please select at least one option for each category.')