from math import sqrt


import plotly.express as px


import plotly.graph_objects as go


def load_data():
    land_area = {"Australia": 7682300, "Japan": 364500, "Bangladesh": 130170}
    frame = px.data.gapminder()
    selected = frame[(frame["year"] == 2007) & frame["country"].isin(land_area)]
    return sorted(
        [
            (country, int(population), land_area[country])
            for country, population in selected[["country", "pop"]].itertuples(
                index=False, name=None
            )
        ],
        key=lambda row: row[1],
    )


def value_table(data):
    return {
        "columns": ["Country", "Population in 2007", "Land area (km²)"],
        "rows": [list(row) for row in data],
    }


def make_after(data) -> go.Figure:
    countries, population, _ = zip(*data, strict=True)
    fig = go.Figure(
        go.Bar(x=[value / 1000000 for value in population], y=countries, orientation="h")
    )
    fig.update_layout(
        template="simple_white", xaxis_title="Population (millions)", yaxis_automargin=True
    )
    fig.update_layout(
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        colorway=["#3b82c4", "#d97706", "#14967f", "#8b5cf6", "#dc4c3e", "#78716c"],
    )
    return fig


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