import plotly.express as px


import plotly.graph_objects as go


def load_data():
    frame = px.data.gapminder()
    subset = frame[
        (frame["year"] == 2007)
        & frame["country"].isin(
            ["Bosnia and Herzegovina", "Czech Republic", "Slovak Republic", "United Kingdom"]
        )
    ]
    return list(subset[["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)
    fig = go.Figure(
        go.Bar(
            x=values,
            y=countries,
            orientation="h",
            text=["2007"] * len(values),
            textposition="inside",
        )
    )
    fig.update_layout(
        template="simple_white", yaxis_automargin=True, xaxis_title="Life expectancy (years)"
    )
    return fig


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