File size: 2,365 Bytes
986c965
 
 
 
e966a32
986c965
6b878b7
986c965
997251b
6b878b7
 
 
986c965
997251b
986c965
997251b
6b878b7
 
997251b
6b878b7
 
997251b
986c965
6b878b7
e966a32
997251b
6b878b7
997251b
 
 
 
 
 
986c965
6b878b7
 
 
 
986c965
 
997251b
e966a32
997251b
6b878b7
997251b
6b878b7
 
997251b
6b878b7
 
997251b
 
 
 
 
 
6b878b7
997251b
 
 
6b878b7
 
 
997251b
6b878b7
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70

import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt

# Load the data
df = pd.read_csv("data.csv")

# Get unique entities and years
entities = sorted(df["Entity"].unique().tolist())
earliest_year = int(df["Year"].min())
latest_year = int(df["Year"].max())

def plot_data(selected_entities, start_year, end_year, show_raw_data):
    if not selected_entities:
        return None, pd.DataFrame()

    filtered_df = df[df["Entity"].isin(selected_entities)].copy()
    filtered_df = filtered_df[(filtered_df["Year"] >= start_year) & (filtered_df["Year"] <= end_year)]

    # Create the plot
    fig, ax = plt.subplots(figsize=(10, 6))
    for entity in selected_entities:
        entity_data = filtered_df[filtered_df["Entity"] == entity]
        if not entity_data.empty:
            ax.plot(entity_data["Year"], entity_data["Percent"], label=entity, marker="o", markersize=4)

    ax.set_xlabel("Year")
    ax.set_ylabel("Income Share (%)")
    ax.set_title("Top 5% Income Share Trend")
    ax.legend(title="Country", bbox_to_anchor=(1.05, 1), loc="upper left")
    ax.grid(True, linestyle="--", alpha=0.6)
    plt.tight_layout()

    if show_raw_data:
        return fig, filtered_df
    else:
        return fig, pd.DataFrame()

# Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown("# 📊 Top 5% Income Share Visualization")
    
    with gr.Row():
        with gr.Column():
            entity_selector = gr.CheckboxGroup(
                choices=entities,
                label="Select Countries",
                value=["Australia", "China", "Germany", "Japan", "United States"]
            )
            with gr.Row():
                start_year_input = gr.Number(value=earliest_year, label="Start Year", precision=0)
                end_year_input = gr.Number(value=latest_year, label="End Year", precision=0)
            
            show_raw_data_checkbox = gr.Checkbox(label="Show Data Table", value=False)
            submit_button = gr.Button("Update Chart", variant="primary")

        with gr.Column():
            output_plot = gr.Plot()
            output_dataframe = gr.DataFrame()

    submit_button.click(
        fn=plot_data,
        inputs=[entity_selector, start_year_input, end_year_input, show_raw_data_checkbox],
        outputs=[output_plot, output_dataframe]
    )

if __name__ == "__main__":
    demo.launch()