Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -2,87 +2,66 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
-
import matplotlib.ticker as ticker
|
| 6 |
-
|
| 7 |
-
# Set font for CJK characters
|
| 8 |
-
plt.rcParams['font.sans-serif'] = ['Noto Sans CJK SC', 'DejaVu Sans']
|
| 9 |
-
plt.rcParams['axes.unicode_minus'] = False
|
| 10 |
|
| 11 |
# Load the data
|
| 12 |
df = pd.read_csv("data.csv")
|
| 13 |
|
| 14 |
-
# Get unique entities and years
|
| 15 |
entities = sorted(df["Entity"].unique().tolist())
|
| 16 |
earliest_year = int(df["Year"].min())
|
| 17 |
latest_year = int(df["Year"].max())
|
| 18 |
|
| 19 |
-
def plot_data(selected_entities,
|
| 20 |
if not selected_entities:
|
| 21 |
-
return
|
| 22 |
-
|
| 23 |
-
min_year, max_year = year_range
|
| 24 |
|
| 25 |
filtered_df = df[df["Entity"].isin(selected_entities)].copy()
|
| 26 |
-
filtered_df = filtered_df[(filtered_df["Year"] >=
|
| 27 |
|
| 28 |
# Create the plot
|
| 29 |
-
fig, ax = plt.subplots(figsize=(
|
| 30 |
for entity in selected_entities:
|
| 31 |
entity_data = filtered_df[filtered_df["Entity"] == entity]
|
| 32 |
if not entity_data.empty:
|
| 33 |
-
ax.plot(entity_data["Year"], entity_data["Percent"], label=entity, marker="
|
| 34 |
-
|
| 35 |
-
ax.set_xlabel("Year", fontsize=12)
|
| 36 |
-
ax.set_ylabel("Income Share (%)", fontsize=12)
|
| 37 |
-
ax.set_title("Top 5% Income Share Trend", fontsize=16, fontweight="bold")
|
| 38 |
-
ax.legend(title="Country/Region", bbox_to_anchor=(1.05, 1), loc="upper left")
|
| 39 |
-
ax.grid(True, linestyle=":", alpha=0.7)
|
| 40 |
-
ax.set_facecolor("#f5f5f5") # Light grey background
|
| 41 |
-
|
| 42 |
-
# Improve x-axis ticks
|
| 43 |
-
ax.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
|
| 44 |
-
plt.xticks(rotation=45, ha="right")
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
# Return raw data if requested
|
| 49 |
if show_raw_data:
|
| 50 |
return fig, filtered_df
|
| 51 |
else:
|
| 52 |
return fig, pd.DataFrame()
|
| 53 |
|
| 54 |
# Gradio Interface
|
| 55 |
-
with gr.Blocks(
|
| 56 |
gr.Markdown("# 📊 Top 5% Income Share Visualization")
|
| 57 |
-
|
| 58 |
-
|
| 59 |
with gr.Row():
|
| 60 |
-
with gr.Column(
|
| 61 |
entity_selector = gr.CheckboxGroup(
|
| 62 |
choices=entities,
|
| 63 |
-
label="Select Countries
|
| 64 |
value=["Australia", "China", "Germany", "Japan", "United States"]
|
| 65 |
)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
)
|
| 73 |
-
show_raw_data_checkbox = gr.Checkbox(label="Show Raw Data Table", value=False)
|
| 74 |
-
submit_button = gr.Button("🚀 Update Visualization", variant="primary")
|
| 75 |
-
|
| 76 |
-
with gr.Column(scale=2):
|
| 77 |
-
output_plot = gr.Plot(label="Income Share Trend")
|
| 78 |
-
output_dataframe = gr.DataFrame(label="Filtered Data Table")
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
submit_button.click(
|
| 84 |
fn=plot_data,
|
| 85 |
-
inputs=[entity_selector,
|
| 86 |
outputs=[output_plot, output_dataframe]
|
| 87 |
)
|
| 88 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load the data
|
| 7 |
df = pd.read_csv("data.csv")
|
| 8 |
|
| 9 |
+
# Get unique entities and years
|
| 10 |
entities = sorted(df["Entity"].unique().tolist())
|
| 11 |
earliest_year = int(df["Year"].min())
|
| 12 |
latest_year = int(df["Year"].max())
|
| 13 |
|
| 14 |
+
def plot_data(selected_entities, start_year, end_year, show_raw_data):
|
| 15 |
if not selected_entities:
|
| 16 |
+
return None, pd.DataFrame()
|
|
|
|
|
|
|
| 17 |
|
| 18 |
filtered_df = df[df["Entity"].isin(selected_entities)].copy()
|
| 19 |
+
filtered_df = filtered_df[(filtered_df["Year"] >= start_year) & (filtered_df["Year"] <= end_year)]
|
| 20 |
|
| 21 |
# Create the plot
|
| 22 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 23 |
for entity in selected_entities:
|
| 24 |
entity_data = filtered_df[filtered_df["Entity"] == entity]
|
| 25 |
if not entity_data.empty:
|
| 26 |
+
ax.plot(entity_data["Year"], entity_data["Percent"], label=entity, marker="o", markersize=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
ax.set_xlabel("Year")
|
| 29 |
+
ax.set_ylabel("Income Share (%)")
|
| 30 |
+
ax.set_title("Top 5% Income Share Trend")
|
| 31 |
+
ax.legend(title="Country", bbox_to_anchor=(1.05, 1), loc="upper left")
|
| 32 |
+
ax.grid(True, linestyle="--", alpha=0.6)
|
| 33 |
+
plt.tight_layout()
|
| 34 |
|
|
|
|
| 35 |
if show_raw_data:
|
| 36 |
return fig, filtered_df
|
| 37 |
else:
|
| 38 |
return fig, pd.DataFrame()
|
| 39 |
|
| 40 |
# Gradio Interface
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
gr.Markdown("# 📊 Top 5% Income Share Visualization")
|
| 43 |
+
|
|
|
|
| 44 |
with gr.Row():
|
| 45 |
+
with gr.Column():
|
| 46 |
entity_selector = gr.CheckboxGroup(
|
| 47 |
choices=entities,
|
| 48 |
+
label="Select Countries",
|
| 49 |
value=["Australia", "China", "Germany", "Japan", "United States"]
|
| 50 |
)
|
| 51 |
+
with gr.Row():
|
| 52 |
+
start_year_input = gr.Number(value=earliest_year, label="Start Year", precision=0)
|
| 53 |
+
end_year_input = gr.Number(value=latest_year, label="End Year", precision=0)
|
| 54 |
+
|
| 55 |
+
show_raw_data_checkbox = gr.Checkbox(label="Show Data Table", value=False)
|
| 56 |
+
submit_button = gr.Button("Update Chart", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
with gr.Column():
|
| 59 |
+
output_plot = gr.Plot()
|
| 60 |
+
output_dataframe = gr.DataFrame()
|
| 61 |
|
| 62 |
submit_button.click(
|
| 63 |
fn=plot_data,
|
| 64 |
+
inputs=[entity_selector, start_year_input, end_year_input, show_raw_data_checkbox],
|
| 65 |
outputs=[output_plot, output_dataframe]
|
| 66 |
)
|
| 67 |
|