import pandas as pd


import plotly.express as px


import plotly.graph_objects as go


def load_data():
    frame = px.data.gapminder()
    selected = frame[frame["country"].isin(["Japan", "Korea, Rep."])]
    return [
        ("South Korea" if country == "Korea, Rep." else country, year, life)
        for country, year, life in selected[["country", "year", "lifeExp"]].itertuples(
            index=False, name=None
        )
    ]


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


def make_before(data) -> go.Figure:
    frame = pd.DataFrame(data, columns=["Country", "Year", "Life expectancy (years)"])
    fig = px.line(
        frame, x="Year", y="Life expectancy (years)", facet_col="Country", template="simple_white"
    )
    fig.update_yaxes(matches=None)
    return fig


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