import plotly.express as px


import plotly.graph_objects as go


def load_data():
    frame = px.data.gapminder()
    picks = ["Japan", "Switzerland", "Sweden", "Canada", "Germany", "United States"]
    latest = frame[(frame["year"] == 2007) & (frame["country"].isin(picks))]
    rows = [
        (str(country), round(float(life), 1))
        for country, life in latest[["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": {"text": "Life expectancy (years)", "font": {"size": 7}},
            "tickfont": {"size": 7},
            "range": [0, 90],
        },
        yaxis={"tickfont": {"size": 7}},
        margin={"l": 72, "r": 24, "t": 24, "b": 40},
    )
    return fig


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