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=countries, y=values))
    for country, value in data:
        fig.add_annotation(x=country, y=value, text=f"{value:.1f}", showarrow=False, yshift=6)
    fig.update_layout(
        template="simple_white",
        width=365,
        height=360,
        yaxis_title="Life expectancy (years)",
    )
    return fig


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