import plotly.express as px


import plotly.graph_objects as go


def load_data():
    frame = px.data.gapminder()
    picks = [
        "Iceland",
        "Norway",
        "Sweden",
        "Spain",
        "Germany",
        "Japan",
        "United States",
        "China",
    ]
    latest = frame[(frame["year"] == 2007) & (frame["country"].isin(picks))]
    rows = [
        (str(country), int(pop))
        for country, pop in latest[["country", "pop"]].itertuples(index=False, name=None)
    ]
    rows.sort(key=lambda item: item[1])
    return rows


def value_table(data):
    return {
        "columns": ["Country", "Population (2007)"],
        "rows": [[country, f"{pop:,}"] for country, pop in data],
    }


def make_before(data) -> go.Figure:
    countries = [row[0] for row in data]
    pops = [row[1] for row in data]
    fig = go.Figure(
        go.Bar(x=pops, y=countries, orientation="h", marker={"color": "#0072B2"})
    )
    fig.update_layout(
        template="simple_white",
        xaxis_type="log",
        xaxis_title="Population (log scale)",
        margin={"l": 110, "r": 24, "t": 28, "b": 48},
    )
    return fig


if __name__ == "__main__":
    print(make_before(load_data()).to_json())
