I am recalculating a column value based on the other values inside a row. It works for the first record but does not work for the rest of the records. Also, if I have more than two rows, I delete the rest of the documents when I edit the first row and recalculate the value.
def calc_hours(new_df: pd.DataFrame | None = None):
if new_df is not None:
if new_df.equals(st.session_state["df"]):
return
st.session_state["df"] = new_df
df = st.session_state["df"]
df["num_horas"] = df["num_horas_S"] + df["num_horas_DS"]
st.session_state["df"] = df
st.rerun()
if "df" not in st.session_state:
st.session_state.df = pd.DataFrame(columns=['solution',
'total_amount',
'service_amount',
'hours_amount',
'num_horas',
'num_horas_S',
'num_horas_DS'])
calc_hours()
editable_df = st.data_editor(st.session_state["df"],
key="data",
column_config={
"solution": st.column_config.SelectboxColumn(
"solucion",
help="The category of the app",
width="medium",
options=[
"Hola",
"Adios",
"Nos vemos",
],
required=True,
),
"total_amount": st.column_config.NumberColumn(
"Price (in USD)",
help="The price of the product in USD",
min_value=0,
max_value=100000000,
step=1,
format="%d",
default=0,
),
"service_amount": st.column_config.NumberColumn(
"Price (in USD)",
help="The price of the product in USD",
min_value=0,
max_value=100000000,
step=1,
format="%d",
default=0,
),
"hours_amount": st.column_config.NumberColumn(
"Price (in USD)",
help="The price of the product in USD",
min_value=0,
max_value=1000,
step=1,
format="%d",
default=0,
),
"num_horas": st.column_config.NumberColumn(
"Horas",
help="The price of the product in USD",
min_value=0,
max_value=1000,
step=1,
format="%d",
default=0,
required=True,
disabled=True,
),
"num_horas_S": st.column_config.NumberColumn(
"HS",
help="Num. horas Socio",
min_value=0,
max_value=1000,
step=1,
format="%d",
default=0,
),
"num_horas_DS": st.column_config.NumberColumn(
"HDS",
help="Num. horas Socio",
min_value=0,
max_value=1000,
step=1,
format="%d",
default=0,
),
},
hide_index=True,
num_rows="dynamic",
disabled=['num_horas',],
)
calc_hours(editable_df)
Thanks a lot.