import plotly.express as px


import plotly.graph_objects as go


def load_data():
    frame = px.data.gapminder()
    europe = frame[(frame["year"] == 2007) & (frame["continent"] == "Europe")].nlargest(12, "lifeExp")
    rows = [
        (str(country), round(float(life), 2))
        for country, life in europe[["country", "lifeExp"]].itertuples(index=False, name=None)
    ]
    rows.sort(key=lambda item: item[1])
    return rows


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


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


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