From 02a25c9391ef65ef352d0fae1f26f7a239ae5075 Mon Sep 17 00:00:00 2001 From: p1utoze <1ds21ai001@dsce.edu.in> Date: Tue, 2 Apr 2024 18:09:42 +0530 Subject: [PATCH] update: added color map reference section mapping to dataframe food name column --- app/pages/comparer.py | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/app/pages/comparer.py b/app/pages/comparer.py index a8d2029..bf18143 100644 --- a/app/pages/comparer.py +++ b/app/pages/comparer.py @@ -1,7 +1,7 @@ import pandas as pd import streamlit as st from src import prepare_unit_data -from src.config import PROJECT_ROOT +from src.config import COLOR_MAP, PROJECT_ROOT def run(): @@ -16,19 +16,26 @@ def run(): "g": "Grams", "kg": "Kilograms", "ug": "Micrograms", + "kJ": "Kilojoules", } - def get_food_code() -> list: + def highlight_category(val, df): + val = df.loc[df["name"] == val, "grup"].values[0].upper() + return f"background-color: {COLOR_MAP[val]}" + + def get_food_code(extra_cols=None) -> list: + if extra_cols is None: + extra_cols = [] ss = _sess_state["units_df"].loc[(_sess_state["units_df"]["name"].isin(_sess_state["nutrient"])), "code"] - return ss[~ss.str.endswith("_e")].tolist() + return ss[~ss.str.endswith("_e")].tolist() + extra_cols @st.cache_data(ttl=600) def get_nutrient_column(table: str, return_columns: str = "*", as_df: bool = True): return_columns = f"name, {return_columns}" - response = _sess_state["_conn"].table(table).select(return_columns).execute() + res = _sess_state["_conn"].table(table).select(return_columns).execute() if as_df: - return pd.DataFrame(response.data) - return response.data + return pd.DataFrame(res.data) + return res.data st.header("📊 Nutrient Comparison Table Chart") @@ -41,13 +48,13 @@ def get_nutrient_column(table: str, return_columns: str = "*", as_df: bool = Tru st.empty() if "nutrient" in _sess_state and _sess_state["nutrient"]: - cols = get_food_code() + cols = get_food_code(["grup"]) response = get_nutrient_column("food_ifct", return_columns=",".join(cols)) response.set_index("name", inplace=True) config = {} + cols.remove("grup") units = _sess_state["units_df"].loc[_sess_state["units_df"]["code"].isin(cols), "unit"].tolist() factors = _sess_state["units_df"].loc[_sess_state["units_df"]["code"].isin(cols), "factor"].tolist() - for i in range(len(cols)): response[cols[i]] = response[cols[i]] * factors[i] config[cols[i]] = st.column_config.ProgressColumn( @@ -58,4 +65,15 @@ def get_nutrient_column(table: str, return_columns: str = "*", as_df: bool = Tru max_value=float(response[cols[i]].max()), ) - st.dataframe(response, column_config=config, use_container_width=True, height=500) + response.reset_index(inplace=True) + color_df = response[["name", "grup"]] + response.drop("grup", axis=1, inplace=True) + styled_df = response.style.map(highlight_category, df=color_df, subset=["name"]) + st.dataframe(styled_df, column_config=config, use_container_width=True, height=500, hide_index=True) + + with st.expander("Color Map Reference"): + text = [] + for key, value in COLOR_MAP.items(): + text.append(f"{key}") + + st.markdown(" | ".join(text), unsafe_allow_html=True)