import numpy as np


import plotly.express as px


import plotly.graph_objects as go


def load_data():
    frame = px.data.gapminder()
    latest = frame[frame["year"] == 2007]
    rows = []
    for continent, group in latest.groupby("continent"):
        rows.append((str(continent), round(float(np.mean(group["lifeExp"])), 1)))
    rows.sort(key=lambda item: item[1])
    return rows


def value_table(data):
    return {
        "columns": ["Continent", "Mean life expectancy in 2007 (years)"],
        "rows": [list(row) for row in reversed(data)],
    }


def make_before(data) -> go.Figure:
    continents = [row[0] for row in data]
    values = [row[1] for row in data]
    fig = go.Figure(
        go.Bar(
            x=values,
            y=continents,
            orientation="h",
            marker={"color": "rgba(255, 255, 255, 0)", "line": {"color": "#1f2937", "width": 1.5}},
        )
    )
    fig.update_layout(
        template="simple_white",
        xaxis_title="Mean life expectancy (years)",
        xaxis_range=[0, 90],
        margin={"l": 92, "r": 24, "t": 24, "b": 48},
    )
    return fig


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