import plotly.express as px


import plotly.graph_objects as go


def load_data():
    frame = px.data.gapminder()
    latest = frame[frame["year"] == 2007].nlargest(10, "gdpPercap")
    return [
        (str(country), str(continent), round(float(gdp), 0))
        for country, continent, gdp in latest[["country", "continent", "gdpPercap"]].itertuples(
            index=False, name=None
        )
    ]


def value_table(data):
    return {
        "columns": ["Country", "Continent", "GDP per capita (USD)"],
        "rows": [list(row) for row in data],
    }


def make_before(data) -> go.Figure:
    palette = [
        "#3366cc",
        "#dc3912",
        "#ff9900",
        "#109618",
        "#990099",
        "#0099c6",
        "#dd4477",
        "#66aa00",
        "#b82e2e",
        "#316395",
    ]
    countries, _, gdps = zip(*reversed(data), strict=True)
    fig = go.Figure(
        go.Bar(x=gdps, y=countries, orientation="h", marker={"color": palette})
    )
    fig.update_layout(
        template="simple_white",
        xaxis_title="GDP per capita (USD)",
        margin={"l": 110, "r": 24, "t": 24, "b": 48},
    )
    return fig


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