Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,29 +11,40 @@ data = {
|
|
11 |
df = pd.DataFrame(data)
|
12 |
df.set_index("Country", inplace=True)
|
13 |
|
|
|
14 |
def generate_heatmap(selected_countries):
|
15 |
filtered_df = df.loc[selected_countries]
|
16 |
-
|
17 |
-
# Normalize for better visualization
|
18 |
normalized = filtered_df.copy()
|
19 |
normalized["Population"] = normalized["Population"] / 1e6 # Convert to millions
|
20 |
|
21 |
-
plt.figure(figsize=(
|
22 |
sns.heatmap(normalized.T, annot=True, fmt=".1f", cmap="YlOrRd", cbar_kws={"label": "Population (in millions)"})
|
23 |
plt.title("π Population Heatmap")
|
24 |
plt.yticks(rotation=0)
|
25 |
plt.tight_layout()
|
26 |
return plt
|
27 |
|
28 |
-
#
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Gradio App
|
32 |
with gr.Blocks() as demo:
|
33 |
-
gr.Markdown("##
|
34 |
-
selected = gr.CheckboxGroup(label="Select Countries", choices=
|
35 |
-
|
|
|
|
|
|
|
36 |
|
37 |
-
|
|
|
|
|
38 |
|
39 |
demo.launch()
|
|
|
11 |
df = pd.DataFrame(data)
|
12 |
df.set_index("Country", inplace=True)
|
13 |
|
14 |
+
# Heatmap function
|
15 |
def generate_heatmap(selected_countries):
|
16 |
filtered_df = df.loc[selected_countries]
|
|
|
|
|
17 |
normalized = filtered_df.copy()
|
18 |
normalized["Population"] = normalized["Population"] / 1e6 # Convert to millions
|
19 |
|
20 |
+
plt.figure(figsize=(6, 3))
|
21 |
sns.heatmap(normalized.T, annot=True, fmt=".1f", cmap="YlOrRd", cbar_kws={"label": "Population (in millions)"})
|
22 |
plt.title("π Population Heatmap")
|
23 |
plt.yticks(rotation=0)
|
24 |
plt.tight_layout()
|
25 |
return plt
|
26 |
|
27 |
+
# Pie chart function
|
28 |
+
def generate_pie_chart(selected_countries):
|
29 |
+
filtered_df = df.loc[selected_countries]
|
30 |
+
|
31 |
+
plt.figure(figsize=(5, 5))
|
32 |
+
plt.pie(filtered_df["Population"], labels=filtered_df.index, autopct="%1.1f%%", startangle=90)
|
33 |
+
plt.title("π§© Population Distribution")
|
34 |
+
plt.tight_layout()
|
35 |
+
return plt
|
36 |
|
37 |
# Gradio App
|
38 |
with gr.Blocks() as demo:
|
39 |
+
gr.Markdown("## π World Population Visualization")
|
40 |
+
selected = gr.CheckboxGroup(label="Select Countries", choices=df.index.tolist(), value=["India", "China", "USA"])
|
41 |
+
|
42 |
+
with gr.Row():
|
43 |
+
heatmap_output = gr.Plot(label="Heatmap")
|
44 |
+
piechart_output = gr.Plot(label="Pie Chart")
|
45 |
|
46 |
+
# Link input to both outputs
|
47 |
+
selected.change(fn=generate_heatmap, inputs=selected, outputs=heatmap_output)
|
48 |
+
selected.change(fn=generate_pie_chart, inputs=selected, outputs=piechart_output)
|
49 |
|
50 |
demo.launch()
|