Hi,
I am trying to use on_change callback function within data_editor. I can’t seem to get my table updated when I edit the editable fields on the table. My expectation is as soon as I edit the data the table should update in place. Here is my code that I could not get working correctly for me. Currently, when I change data, nothing happens (i.e the prices do not get updated).
if final_dfs: st.session_state.final_df = pd.concat(final_dfs, ignore_index=True) # Add default values and extra columns if "Markup, %" not in st.session_state.final_df.columns: st.session_state.final_df["Markup, %"] = 30 if "External margin, %" not in st.session_state.final_df.columns: st.session_state.final_df["External margin, %"] = 50 if "Select" not in st.session_state.final_df.columns: st.session_state.final_df["Select"] = False # Define a callback function to recalculate the prices def recalculate_prices(): st.session_state.final_df = calculate_price_with_markup(st.session_state.final_df) st.session_state.final_df = calculate_total_price(st.session_state.final_df) st.session_state.final_df = calculate_final_base_margin(st.session_state.final_df) # Rearrange the column sequence desired_order = [ "part_id", "diameter_in", "Total Cost (USD)", "Markup, %", "Price with markup (USD)", "External margin, %", "Total price (USD)", "Final base margin, %", "Select" ] st.session_state.final_df = st.session_state.final_df[desired_order] # Initial calculation of prices recalculate_prices() # Define the column configuration column_config = { "part_id": st.column_config.TextColumn( "Part ID", disabled=True, ), "diameter_in": st.column_config.NumberColumn( "Diameter", disabled=True, ), "Total Cost (USD)": st.column_config.NumberColumn( "Total Cost (USD)", disabled=True, ), "Markup, %": st.column_config.NumberColumn( "Markup, %", ), "External margin, %": st.column_config.NumberColumn( "External margin, %", ), "Price with markup (USD)": st.column_config.NumberColumn( "Price with markup (USD)", disabled=True, ), "Total price (USD)": st.column_config.NumberColumn( "Total price (USD)", disabled=True, ), "Final base margin, %": st.column_config.NumberColumn( "Final base margin, %", disabled=True, ), "Select": st.column_config.CheckboxColumn("Select"), } # Display the data editor edited_df = st.data_editor( st.session_state.final_df, key="data_editor", hide_index=True, column_config=column_config, on_change=recalculate_prices, )