Skip to content

Commit

Permalink
update: added color map reference section mapping to dataframe food n…
Browse files Browse the repository at this point in the history
…ame column
  • Loading branch information
p1utoze committed Apr 2, 2024
1 parent 682169e commit 02a25c9
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions app/pages/comparer.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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")

Expand All @@ -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(
Expand All @@ -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"<span style='color:{value}'>{key}</span>")

st.markdown(" | ".join(text), unsafe_allow_html=True)

0 comments on commit 02a25c9

Please sign in to comment.