import plotly.express as px


import plotly.graph_objects as go


def load_data():
    frame = px.data.gapminder()
    us = frame[frame["country"] == "United States"].sort_values("year")
    return [
        (int(year), round(float(life), 2))
        for year, life in us[["year", "lifeExp"]].itertuples(index=False, name=None)
    ]


def value_table(data):
    return {
        "columns": ["Year", "US life expectancy (years)"],
        "rows": [list(row) for row in data],
    }


def make_before(data) -> go.Figure:
    years = [row[0] for row in data]
    values = [row[1] for row in data]
    fig = go.Figure(
        go.Scatter(
            x=years,
            y=values,
            mode="lines+markers",
            line={"color": "#0072B2", "width": 2},
            marker={"size": 6, "color": "#0072B2"},
        )
    )
    fig.update_layout(
        template="simple_white",
        title="Life expectancy",
        xaxis_title="Year",
        yaxis_title="Years",
        yaxis_range=[66, 80],
        margin={"l": 56, "r": 24, "t": 44, "b": 48},
    )
    return fig


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