Spaces:
Sleeping
Sleeping
Add country filter to central viz
Browse files- src/streamlit_app.py +27 -8
src/streamlit_app.py
CHANGED
|
@@ -117,24 +117,43 @@ st.markdown("""
|
|
| 117 |
Watch how the bubbles (countries) suddenly drop en masse in 2009. This synchronized collapse is the visual signature of a global recession.
|
| 118 |
""")
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
fig_main = px.scatter(
|
| 122 |
-
|
| 123 |
x="Trade_GDP",
|
| 124 |
y="GDP_Growth",
|
| 125 |
animation_frame="Year",
|
| 126 |
animation_group="Country Name",
|
| 127 |
size="Trade_GDP",
|
| 128 |
-
|
| 129 |
-
#
|
| 130 |
-
|
| 131 |
color_continuous_scale=[
|
| 132 |
[0, "red"],
|
| 133 |
[0.5, "red"],
|
| 134 |
[0.5, "blue"],
|
| 135 |
[1, "blue"]
|
| 136 |
],
|
| 137 |
-
color_continuous_midpoint=0,
|
|
|
|
| 138 |
hover_name="Country Name",
|
| 139 |
range_x=[0, 180],
|
| 140 |
range_y=[-15, 15],
|
|
@@ -148,10 +167,10 @@ if not df.empty and 'Trade_GDP' in df.columns and 'GDP_Growth' in df.columns:
|
|
| 148 |
)
|
| 149 |
|
| 150 |
# Reference line for Recession
|
| 151 |
-
fig_main.add_hline(y=0, line_dash="dash", line_color="
|
| 152 |
|
| 153 |
st.plotly_chart(fig_main, use_container_width=True)
|
| 154 |
-
st.caption("Source: World Bank World Development Indicators (WDI).
|
| 155 |
else:
|
| 156 |
st.warning("Data could not be loaded for the main visualization.")
|
| 157 |
|
|
|
|
| 117 |
Watch how the bubbles (countries) suddenly drop en masse in 2009. This synchronized collapse is the visual signature of a global recession.
|
| 118 |
""")
|
| 119 |
|
| 120 |
+
# ---COUNTRY FILTER ---
|
| 121 |
+
# Get a list of all unique countries
|
| 122 |
+
all_countries_list = sorted(df['Country Name'].unique().tolist())
|
| 123 |
+
|
| 124 |
+
# Create the Multiselect Widget
|
| 125 |
+
selected_countries_main = st.multiselect(
|
| 126 |
+
"🔍 **Filter by Country:** (Leave empty to see the whole world, or select countries to isolate them)",
|
| 127 |
+
options=all_countries_list,
|
| 128 |
+
default=[], # Default is empty (show all)
|
| 129 |
+
placeholder="Choose countries (e.g., United States, China, Germany)..."
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# Logic: If countries are selected, filter the dataframe. If not, use the full dataframe.
|
| 133 |
+
if selected_countries_main:
|
| 134 |
+
df_viz = df[df['Country Name'].isin(selected_countries_main)]
|
| 135 |
+
else:
|
| 136 |
+
df_viz = df
|
| 137 |
+
|
| 138 |
+
if not df_viz.empty and 'Trade_GDP' in df_viz.columns and 'GDP_Growth' in df_viz.columns:
|
| 139 |
fig_main = px.scatter(
|
| 140 |
+
df_viz, # Use the filtered dataframe here
|
| 141 |
x="Trade_GDP",
|
| 142 |
y="GDP_Growth",
|
| 143 |
animation_frame="Year",
|
| 144 |
animation_group="Country Name",
|
| 145 |
size="Trade_GDP",
|
| 146 |
+
|
| 147 |
+
# STRICT RED/BLUE SCALE
|
| 148 |
+
color="GDP_Growth",
|
| 149 |
color_continuous_scale=[
|
| 150 |
[0, "red"],
|
| 151 |
[0.5, "red"],
|
| 152 |
[0.5, "blue"],
|
| 153 |
[1, "blue"]
|
| 154 |
],
|
| 155 |
+
color_continuous_midpoint=0,
|
| 156 |
+
|
| 157 |
hover_name="Country Name",
|
| 158 |
range_x=[0, 180],
|
| 159 |
range_y=[-15, 15],
|
|
|
|
| 167 |
)
|
| 168 |
|
| 169 |
# Reference line for Recession
|
| 170 |
+
fig_main.add_hline(y=0, line_dash="dash", line_color="black", annotation_text="Recession Threshold (0%)")
|
| 171 |
|
| 172 |
st.plotly_chart(fig_main, use_container_width=True)
|
| 173 |
+
st.caption("Source: World Bank World Development Indicators (WDI). Red = Recession, Blue = Growth.")
|
| 174 |
else:
|
| 175 |
st.warning("Data could not be loaded for the main visualization.")
|
| 176 |
|