import plotly.express as px


import plotly.graph_objects as go


def load_data():
    frame = px.data.gapminder()
    nordics = ["Denmark", "Finland", "Iceland", "Norway", "Sweden"]
    latest = frame[(frame["year"] == 2007) & (frame["country"].isin(nordics))].sort_values(
        "lifeExp"
    )
    return [
        (str(country), round(float(life), 2))
        for country, life in latest[["country", "lifeExp"]].itertuples(index=False, name=None)
    ]


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(*data, strict=True)
    palette = ["#0072B2", "#E69F00", "#009E73", "#CC79A7", "#D55E00"]
    fig = go.Figure(go.Bar(x=values, y=countries, orientation="h", marker={"color": palette}))
    fig.update_layout(template="simple_white", xaxis_title="Life expectancy (years)")
    return fig


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