Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions transmonee_dashboard/src/transmonee_dashboard/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,57 @@ def make_brand(**kwargs):
)


@component
def make_header_nav(**kwargs):
return html.Header(
id="header",
className="header",
children=[
html.Div(
className="container-fluid",
children=[
html.Div(
className="row",
children=[
html.Nav(
className="col-12",
id=server.config["NAVBAR_CONTAINER_ID"],
)
],
)
],
),
],
**kwargs,
)


@component
def make_header_nav_old(**kwargs):
return html.Header(
id="header",
className="header",
children=[

html.Div(
className="header__bottom",
children=[
html.Div(
className="header__inner",
children=[
html.Nav(
className="header__navigation",
id=server.config["NAVBAR_CONTAINER_ID"],
)
],
)
],
),
],
**kwargs,
)


@component
def make_header(**kwargs):
return html.Header(
Expand Down
11 changes: 10 additions & 1 deletion transmonee_dashboard/src/transmonee_dashboard/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .utils import DashRouter, DashNavBar
from .pages import (
child_education,
health,
# child_protection,
# child_health,
# child_poverty,
Expand Down Expand Up @@ -31,6 +32,7 @@
# ("", home.get_layout),
# ("overview", overview.get_layout),
("", child_education.get_layout),
("health", health.get_layout),
# ("child-education", child_education.get_layout),

# ("child-protection", child_protection.get_layout),
Expand All @@ -53,7 +55,7 @@
# is a string corresponding to path of the route (will be prefixed with
# 'routes_pathname_prefix') and 'display' is a valid value for the `children`
# keyword argument for a Dash component (ie a Dash Component or a string).
nav_items = (
nav_items_old = (
("", html.Div([fa("fas fa-home"), "Home"]), []),
("overview", html.Div([fa("fas fa-info-circle"), "Overview"]), []),
(
Expand Down Expand Up @@ -108,6 +110,13 @@
# ("resources", html.Div([fa("fas fa-database"), "Resources"]), []),
)

nav_items = (

("", html.Div([fa("fas fa-info-circle"), "Education"]), []),
("health", html.Div([fa("fas fa-medkit"), "Health"]), []),
# ("protection", html.Div([fa("fas fa-bullseye"), "Protection"]), []),
)

nav_items_full_names = {
"education": "Education",
"child-protection": "Family environment and protection from violence and harmful practices",
Expand Down
4 changes: 2 additions & 2 deletions transmonee_dashboard/src/transmonee_dashboard/layouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import dash_html_components as html
import dash_bootstrap_components as dbc

from .components import make_footer, make_header, make_sidebar
from .components import make_footer, make_header, make_sidebar, make_header_nav
from .components import fa


Expand Down Expand Up @@ -48,7 +48,7 @@ def main_default_layout():

return html.Div(
[
# make_header(),
make_header_nav(),
html.Br(),
dcc.Store(id="store"),
dbc.Container(
Expand Down
33 changes: 21 additions & 12 deletions transmonee_dashboard/src/transmonee_dashboard/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_endpoint(endpoint_id="UNICEF"):
def get_search_countries(countries_cl):
all_countries = {"label": "All", "value": "All"}
countries_list = []
#print(countries_cl)
# print(countries_cl)
# countries_list = [
# {
# "label": key,
Expand All @@ -56,27 +56,26 @@ def get_search_countries(countries_cl):
return countries_list


def _add_tree_level(tree_node, parent_code, codes):
for c in codes:
if "parent" in c and c["parent"] == parent_code:
if "children" not in tree_node:
tree_node["children"] = []
tree_node["children"].append({"key": c["id"], "title": c["name"]})
_add_tree_level(tree_node["children"][-1], c["id"], codes)


def get_codelist(agency, id, version="latest"):
cl_id = f"{agency}|{id}|{version}"
if cl_id in codelists:
#print("CL " + id + " already downloaded")
# print("CL " + id + " already downloaded")
return codelists[cl_id]
#print("CL " + id + " new downloaded")
# print("CL " + id + " new downloaded")

cl = Codelist.Codelist()
cl.download_codelist(get_endpoint(), agency, id, version=version)
codelists[cl_id] = cl.get_codes()
return cl.get_codes()

def _add_tree_level(tree_node, parent_code, codes):
for c in codes:
if "parent" in c and c["parent"] == parent_code:
if "children" not in tree_node:
tree_node["children"] = []
tree_node["children"].append({"key": c["id"], "title": c["name"]})
_add_tree_level(tree_node["children"][-1], c["id"], codes)

def get_selection_tree(ref_area_cl):
codes = get_codelist(ref_area_cl["agency"], ref_area_cl["id"])

Expand Down Expand Up @@ -120,6 +119,16 @@ def get_dataset(cfg_data, years=None, countries=[], recent_data=False):
return df


def get_col_unique(df: Dataflow, col_id: str) -> list:
if not col_id in df:
return []
#Unique and remove nans
ret = df[col_id].unique()
ret = ret[~pd.isnull(ret)]
return ret



# def get_dataflow_struct(cfg_data):
# df_id = f'{get_endpoint()} | {cfg_data["agency"]} | {cfg_data["id"]} | {cfg_data["version"]}'
# if df_id in sdmx_dataflows_structs:
Expand Down
46 changes: 37 additions & 9 deletions transmonee_dashboard/src/transmonee_dashboard/pages/base_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@
get_dataset,
get_search_countries,
# get_indicator_name,
get_codelist
get_codelist,
get_col_unique
)

# set defaults
pio.templates.default = "plotly_white"
px.defaults.color_continuous_scale = px.colors.sequential.BuGn
px.defaults.color_discrete_sequence = px.colors.qualitative.Dark24

#language shouold be read from a param if forced or from the browser
lang = "en"
# move this elsewhere
translations = {
"en": {"REF_AREA": "Geographic areas"},
"pt": {"REF_AREA": "Geographic areas [PT]"}
}

colours = [
"primary",
"success",
Expand Down Expand Up @@ -144,7 +153,7 @@ def make_area(area_name):
},
switch=True,
# style=exclude_outliers_style,
style={"display":"none"}
style={"display": "none"}

),
html.Br(),
Expand Down Expand Up @@ -180,6 +189,7 @@ def get_base_layout(**kwargs):
cfg = kwargs.get("cfg")
main_title = cfg["main_title"]
cl_indicators = get_codelist("BRAZIL_CO", "CL_BRAZILCO_INDICATORS")
cl_units = get_codelist("UNICEF", "CL_UNIT_MEASURE")
selection_tree = get_selection_tree(cfg["ddl_ref_areas_cl"])

'''
Expand Down Expand Up @@ -286,7 +296,7 @@ def get_base_layout(**kwargs):
# style=country_dropdown_style,
# ),
dbc.DropdownMenu(
label=f"Countries: {'len(countries)'}",
label=f"{translations[lang]['REF_AREA']}: {'len(countries)'}",
id="collapse-countries-button",
className="m-2",
color="info",
Expand All @@ -300,7 +310,8 @@ def get_base_layout(**kwargs):
# checked=["0"],
checked=selection_tree["checked"],
# selected=[],
expanded=["0"],
# expanded=["0", "BR"],
expanded=selection_tree["checked"],
# data=[]
data=selection_tree["data"],
),
Expand Down Expand Up @@ -511,7 +522,6 @@ def apply_filters(
# ctx = dash.callback_context
# selected = ctx.triggered[0]["prop_id"].split(".")[0]


# selections = dict(
# theme=current_theme,
# indicators_dict=indicators,
Expand Down Expand Up @@ -540,7 +550,7 @@ def apply_filters(
f"Years: {selected_years[0]} - {selected_years[-1]}",
# "Countries: {}".format(country_text),
# "Countries: {}".format("UPDATED"),
f"Countries: {str(len(sel_country_codes))} selected",
f"{translations[lang]['REF_AREA']}: {str(len(sel_country_codes))} selected",
)


Expand Down Expand Up @@ -588,7 +598,6 @@ def indicator_card(
)
# def show_cards(selections, current_cards, indicators_dict):
def show_cards(selections, current_cards, page_cfg):

cards = [
indicator_card(
selections,
Expand Down Expand Up @@ -730,6 +739,7 @@ def main_figure(indicator, show_historical_data, selections, cfg):
ref_areas = selections["countries"]

cl_countries = get_codelist("BRAZIL_CO", "CL_BRAZIL_REF_AREAS")
cl_units = get_codelist("UNICEF", "CL_UNIT_MEASURE")

if latest_data:
data = get_dataset(series, recent_data=True, countries=ref_areas)
Expand All @@ -741,8 +751,12 @@ def main_figure(indicator, show_historical_data, selections, cfg):
return EMPTY_CHART, ""

df_countries = pd.DataFrame(columns=["name", "id"], data=cl_countries)
df_units = pd.DataFrame(columns=["name", "id"], data=cl_units)

data = data.merge(df_countries, how="left", left_on="REF_AREA", right_on="id")
# data = data.drop(columns=["id"])
# data = data.rename(columns={"name":"REF_AREA_NAME"})
# data = data.merge(cl_units, how="left", left_on="UNIT_MEASURE", right_on="id")

DEFAULT_LABELS = {
"REF_AREA": "Geographic area",
Expand All @@ -763,6 +777,13 @@ def main_figure(indicator, show_historical_data, selections, cfg):
# if len(data[data["CODE"] == indicator]["Unit_name"].astype(str).unique()) > 0
# else ""
# )

# TODO this code seems to be duplicated in area_figure, merge the code
source = ""
sources = get_col_unique(data, "DATA_SOURCE")
if len(sources) > 0:
source = ", ".join(sources)

# df_indicator_sources = df_sources[df_sources["Code"] == indicator]
# unique_indicator_sources = df_indicator_sources["Source_Full"].unique()
# source = (
Expand Down Expand Up @@ -800,7 +821,6 @@ def main_figure(indicator, show_historical_data, selections, cfg):
# layout=main_figure.layout,
# )

source = ""
source_link = ""
return main_figure, html.A(html.P(source), href=source_link, target="_blank")

Expand Down Expand Up @@ -861,6 +881,7 @@ def area_figure(

cl_countries = get_codelist("BRAZIL_CO", "CL_BRAZIL_REF_AREAS")
df_countries = pd.DataFrame(columns=["name", "id"], data=cl_countries)
df_countries = df_countries.rename(columns={"name":"REF_AREA_l"})

data = data.merge(df_countries, how="left", left_on="REF_AREA", right_on="id")

Expand Down Expand Up @@ -895,6 +916,12 @@ def area_figure(
# if len(data[data["CODE"] == indicator]["Unit_name"].astype(str).unique()) > 0
# else ""
# )

# TODO this code seems to be duplicated in main_figure, merge the code
source = ""
sources = get_col_unique(data, "DATA_SOURCE")
if len(sources) > 0:
source = ", ".join(sources)
# df_indicator_sources = df_sources[df_sources["Code"] == indicator]
# unique_indicator_sources = df_indicator_sources["Source_Full"].unique()
# source = (
Expand All @@ -908,7 +935,6 @@ def area_figure(
# else ""
# )

source = ""
source_link = ""

# options["labels"] = DEFAULT_LABELS.copy()
Expand All @@ -920,6 +946,7 @@ def area_figure(
"AGE": "Current age",
"EDUCATION_LEVEL": "Education level",
"TIME_PERIOD": "Time period",
"REF_AREA_l":"Geographic area"
}

options["labels"] = DEFAULT_LABELS.copy()
Expand All @@ -933,6 +960,7 @@ def area_figure(
ind = list(data["INDICATOR"].unique())[0]
indicator_name = next(item for item in cl_indicators if item["id"] == ind)
indicator_name = indicator_name["name"]

# cl_countries = get_codelist("BRAZIL_CO", "CL_BRAZIL_REF_AREAS")
# set the chart title, wrap the text when the indicator name is too long
chart_title = textwrap.wrap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"graphs": {
"bar": {
"options": dict(
x="name",
x="REF_AREA_l",
y="OBS_VALUE",
barmode="group",
# text="TIME_PERIOD",
Expand Down Expand Up @@ -174,7 +174,7 @@
"graphs": {
"bar": {
"options": dict(
x="name",
x="REF_AREA_l",
y="OBS_VALUE",
barmode="group",
# text="TIME_PERIOD",
Expand Down Expand Up @@ -290,7 +290,7 @@
"graphs": {
"bar": {
"options": dict(
x="name",
x="REF_AREA_l",
y="OBS_VALUE",
barmode="group",
# text="TIME_PERIOD",
Expand Down Expand Up @@ -367,7 +367,7 @@
"graphs": {
"bar": {
"options": dict(
x="name",
x="REF_AREA_l",
y="OBS_VALUE",
barmode="group",
# text="TIME_PERIOD",
Expand Down
Loading