Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -104,31 +104,8 @@ def load_shocks_from_zip(zip_file):
|
|
| 104 |
except Exception as e:
|
| 105 |
return None
|
| 106 |
|
| 107 |
-
# def show_shock_value_only(df: pd.DataFrame, label: str):
|
| 108 |
-
# st.markdown(f"### Policy Shock Variables ({label})")
|
| 109 |
-
# show_option = st.radio(
|
| 110 |
-
# f"View Option for {label}",
|
| 111 |
-
# ["Only show changed (non-neutral) values", "Show all variables"],
|
| 112 |
-
# index=0,
|
| 113 |
-
# horizontal=True,
|
| 114 |
-
# key=f"shock_radio_{label}"
|
| 115 |
-
# )
|
| 116 |
-
|
| 117 |
-
# if show_option == "Only show changed (non-neutral) values":
|
| 118 |
-
# if "value" in df.columns:
|
| 119 |
-
# filtered_df = df[df["value"] != 1]
|
| 120 |
-
# if filtered_df.empty:
|
| 121 |
-
# st.info(f"✅ No changed values found in {label} (all value = 1).")
|
| 122 |
-
# else:
|
| 123 |
-
# st.dataframe(filtered_df, use_container_width=True)
|
| 124 |
-
# else:
|
| 125 |
-
# st.warning(f"⚠️ No 'value' column found in {label}.")
|
| 126 |
-
# else:
|
| 127 |
-
# st.dataframe(df, use_container_width=True)
|
| 128 |
-
# ✅ 新版函数:不包含 st.expander,增加 region 和 commodity 筛选
|
| 129 |
def show_shock_value_only(df: pd.DataFrame, label: str):
|
| 130 |
-
st.
|
| 131 |
-
|
| 132 |
show_option = st.radio(
|
| 133 |
f"View Option for {label}",
|
| 134 |
["Only show changed (non-neutral) values", "Show all variables"],
|
|
@@ -137,46 +114,18 @@ def show_shock_value_only(df: pd.DataFrame, label: str):
|
|
| 137 |
key=f"shock_radio_{label}"
|
| 138 |
)
|
| 139 |
|
| 140 |
-
# 初步过滤
|
| 141 |
if show_option == "Only show changed (non-neutral) values":
|
| 142 |
if "value" in df.columns:
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
else:
|
| 145 |
st.warning(f"⚠️ No 'value' column found in {label}.")
|
| 146 |
-
|
| 147 |
-
# 多条件筛选器
|
| 148 |
-
if "region" in df.columns:
|
| 149 |
-
regions = sorted(df["region"].dropna().unique())
|
| 150 |
-
selected_regions = st.multiselect(f"{label}: Filter by Region", regions, default=regions, key=f"region_{label}")
|
| 151 |
-
df = df[df["region"].isin(selected_regions)]
|
| 152 |
-
|
| 153 |
-
if "commodity" in df.columns:
|
| 154 |
-
commodities = sorted(df["commodity"].dropna().unique())
|
| 155 |
-
selected_commodities = st.multiselect(f"{label}: Filter by Commodity", commodities, default=commodities, key=f"commodity_{label}")
|
| 156 |
-
df = df[df["commodity"].isin(selected_commodities)]
|
| 157 |
-
|
| 158 |
-
if "attribute" in df.columns:
|
| 159 |
-
attrs = sorted(df["attribute"].dropna().unique())
|
| 160 |
-
selected_attrs = st.multiselect(f"{label}: Filter by Attribute", attrs, default=attrs, key=f"attribute_{label}")
|
| 161 |
-
df = df[df["attribute"].isin(selected_attrs)]
|
| 162 |
-
|
| 163 |
-
if "value" in df.columns and not df["value"].isnull().all():
|
| 164 |
-
val_min = float(df["value"].min())
|
| 165 |
-
val_max = float(df["value"].max())
|
| 166 |
-
value_range = st.slider(
|
| 167 |
-
f"{label}: Filter by Value Range",
|
| 168 |
-
min_value=val_min,
|
| 169 |
-
max_value=val_max,
|
| 170 |
-
value=(val_min, val_max),
|
| 171 |
-
key=f"value_range_{label}"
|
| 172 |
-
)
|
| 173 |
-
df = df[df["value"].between(value_range[0], value_range[1])]
|
| 174 |
-
|
| 175 |
-
# 展示表格
|
| 176 |
-
if df.empty:
|
| 177 |
-
st.info(f"No data available after filtering for {label}.")
|
| 178 |
else:
|
| 179 |
st.dataframe(df, use_container_width=True)
|
|
|
|
| 180 |
shock_df_1 = load_shocks_from_zip(zip_file_1)
|
| 181 |
shock_df_2 = load_shocks_from_zip(zip_file_2)
|
| 182 |
|
|
@@ -186,7 +135,6 @@ with st.expander("Policy Shock Variables", expanded=False):
|
|
| 186 |
if zip_file_2 is not None and shock_df_2 is not None:
|
| 187 |
show_shock_value_only(shock_df_2, "Shock 2")
|
| 188 |
|
| 189 |
-
|
| 190 |
@st.cache_data
|
| 191 |
def load_and_process_zip_pair(zip1, zip2):
|
| 192 |
def load_zip(zip_obj):
|
|
@@ -233,7 +181,6 @@ def load_and_process_zip_pair(zip1, zip2):
|
|
| 233 |
|
| 234 |
return df_baseline, df_scenario, shock_name
|
| 235 |
|
| 236 |
-
|
| 237 |
df_base1, df_shock1, shock1_name = load_zip(zip1)
|
| 238 |
|
| 239 |
df = pd.merge(df_base1, df_shock1, on=["region", "commodity", "year", "attribute"], how="left")
|
|
@@ -320,7 +267,6 @@ else:
|
|
| 320 |
chart = build_topn_bar_chart(topn_df)
|
| 321 |
st.altair_chart(chart, use_container_width=True)
|
| 322 |
|
| 323 |
-
|
| 324 |
# ----------------- LINE CHART -----------------
|
| 325 |
@st.cache_data
|
| 326 |
def filter_data(df, region_code, commodity, attribute):
|
|
|
|
| 104 |
except Exception as e:
|
| 105 |
return None
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
def show_shock_value_only(df: pd.DataFrame, label: str):
|
| 108 |
+
st.markdown(f"### Policy Shock Variables ({label})")
|
|
|
|
| 109 |
show_option = st.radio(
|
| 110 |
f"View Option for {label}",
|
| 111 |
["Only show changed (non-neutral) values", "Show all variables"],
|
|
|
|
| 114 |
key=f"shock_radio_{label}"
|
| 115 |
)
|
| 116 |
|
|
|
|
| 117 |
if show_option == "Only show changed (non-neutral) values":
|
| 118 |
if "value" in df.columns:
|
| 119 |
+
filtered_df = df[df["value"] != 1]
|
| 120 |
+
if filtered_df.empty:
|
| 121 |
+
st.info(f"✅ No changed values found in {label} (all value = 1).")
|
| 122 |
+
else:
|
| 123 |
+
st.dataframe(filtered_df, use_container_width=True)
|
| 124 |
else:
|
| 125 |
st.warning(f"⚠️ No 'value' column found in {label}.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
else:
|
| 127 |
st.dataframe(df, use_container_width=True)
|
| 128 |
+
|
| 129 |
shock_df_1 = load_shocks_from_zip(zip_file_1)
|
| 130 |
shock_df_2 = load_shocks_from_zip(zip_file_2)
|
| 131 |
|
|
|
|
| 135 |
if zip_file_2 is not None and shock_df_2 is not None:
|
| 136 |
show_shock_value_only(shock_df_2, "Shock 2")
|
| 137 |
|
|
|
|
| 138 |
@st.cache_data
|
| 139 |
def load_and_process_zip_pair(zip1, zip2):
|
| 140 |
def load_zip(zip_obj):
|
|
|
|
| 181 |
|
| 182 |
return df_baseline, df_scenario, shock_name
|
| 183 |
|
|
|
|
| 184 |
df_base1, df_shock1, shock1_name = load_zip(zip1)
|
| 185 |
|
| 186 |
df = pd.merge(df_base1, df_shock1, on=["region", "commodity", "year", "attribute"], how="left")
|
|
|
|
| 267 |
chart = build_topn_bar_chart(topn_df)
|
| 268 |
st.altair_chart(chart, use_container_width=True)
|
| 269 |
|
|
|
|
| 270 |
# ----------------- LINE CHART -----------------
|
| 271 |
@st.cache_data
|
| 272 |
def filter_data(df, region_code, commodity, attribute):
|