Recalculate columns values in a st.data_editor only works with first row

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.

Hello

may not be what you are looking for, but it is a different approach.

> import pandas as pd
> import streamlit as st
> 
> df = pd.DataFrame({
>     "solution": [ ],
>     "total_amount": [],
>     "service_amount": [],
>     "hours_amount": [],
>     "num_horas_S": [],
>     "num_horas_DS": [],
> })
> 
> def calc_hours(input_df):
>     output_df = input_df.copy()
>     output_df["num_horas"] = input_df["num_horas_S"] + input_df["num_horas_DS"]
>     return output_df
> 
> st.title("Ejemplo")
> 
> # Crear el editor
> editable_df_new = st.data_editor(df,
>                                   key="edit",
>                                   hide_index=True,
>                                   num_rows="dynamic",
>                                   disabled=['num_horas']
>                                   )
> 
> if st.button("Actualizar DataFrame"):
>     new = calc_hours(editable_df_new)
>     st.write(new)