import plotly.express as px


import plotly.graph_objects as go


def load_data():
    frame = px.data.gapminder()
    selected = frame[
        (frame["year"] == 2007)
        & frame["country"].isin(["Austria", "Belgium", "Denmark", "Finland", "France"])
    ]
    return sorted(
        selected[["country", "lifeExp"]].itertuples(index=False, name=None),
        key=lambda row: row[1],
        reverse=True,
    )


def value_table(data):
    return {"columns": ["Country", "Life expectancy (years)"], "rows": [list(row) for row in data]}


def make_before(data) -> go.Figure:
    countries, values = zip(*sorted(data), strict=True)
    fig = go.Figure(go.Bar(x=countries, y=values))
    fig.update_layout(template="simple_white", yaxis_title="Life expectancy (years)")
    return fig


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