import plotly.express as px


import plotly.graph_objects as go


def load_data():
    frame = px.data.gapminder()
    selected = frame[frame["continent"].isin(["Asia", "Europe"])]
    return list(
        selected[["continent", "country", "year", "lifeExp"]].itertuples(index=False, name=None)
    )


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


def make_before(data) -> go.Figure:
    import pandas as pd

    frame = pd.DataFrame(data, columns=["Continent", "Country", "Year", "Life expectancy (years)"])
    means = frame.groupby(["Continent", "Year"], as_index=False)["Life expectancy (years)"].mean()
    return px.line(
        means,
        x="Year",
        y="Life expectancy (years)",
        color="Continent",
        line_dash="Continent",
        template="simple_white",
    )


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