How to update the DF(st.data_editor) being shown?


App: Local
Python: 3.11.9
Streamlit: 1.29.0

Hi, I’m trying to make a simple dashboard using streamlit.

When the data is inputted from the sidebar area, the main page will show two data frames due to the input result. I already splited it by st.columns.

I’ll say the left area is the recom_area, and the right area is the result_area.

What I want to do, modify and show the DF right area (reseult_area)

Sidebar (input area)

here, I make a data and stored it in input_data. like this,

with st.sidebar:
    # sudo: make some inputs (radio, text, num ...)
    st.session_state.btn_search = st.button("Search") # searching button

if "input_data" not in st.session_state:
    st.session_state.input_data = [# above inputs ...]

Main Pages (display area)

recom_area, result_area = st.columns(2)
with recom_area:
    sim_pjt_info = Func(get some DF)
    sim_pjt_cost = Func(get some DF)
    # just have showing them
    st.dataframe(sim_pjt_info.astype(str), height=320, width=700)
    st.dataframe(sim_pjt_cost.astype(str), height=320, width=700)
with result_area:
    # also just showing
    input_info = Func(get some DF)
    st.dataframe(input_info.astype(str).fillna(" "), height=320, width=700)
    # !!! TO DO !!! - If I click the button, below DF should be change
    recom_result = Func(get some DF, using sim_pjt_cost)
    edit_df = st.data_editor(recom_result, ...)
    reset_btn = st.button("Reset")
    adj_btn = st.button("Adj")
   # I edit the DF (first showing one),
   if reset_btn:
     # When I press reset button,
     # the fix disappears and I have to go back to the beginning (recom_result)
    if adj_btn:
     # When I press adjust button,
     # Create a new data frame using the modified data,
     # and the existing data is updated with the newly created data, like this
     new_edit_df = Func(edit_df) # modified
     edit_df = new_edit_df # Same position as previously shown,  edit_df = st.data_editor(recom_result, ...)

Is there a way to create a page in the above format?
I’ve tried this and that, but I couldn’t find a solution, I’d really appreciate your help.

this helped me in a similar case editable dataframe

Hi @Befotaka

It seems that you’re nesting buttons and that may cause the app to refresh when the nested button is clicked (and in doing so the data in st.data_editor may not update properly. Thus, if you can use session state for the buttons via callback function. There are some code snippet examples in the Docs:

1 Like

Thanks to you, I got an idea.
but, still some problems …

if "edit_df" not in st.session_state:
    st.session_state.edit_df = Func(get some init DF)
def reset_df():
    st.session_state.edit_df = Func(get some init DF) # get same init DF
def update_df():
    # update shown DF
    st.session_state.edit_df = # update DF
def store_df():
    # store some DF
with st.container():
    btn_cols = st.columns(3)
    with btn_cols[0]: btn_reset = st.button("Reset", on_click=reset_df)
    with btn_cols[1]: btn_update = st.button("Update", on_click=update_df)
    with btn_cols[2]: btn_store = st.button("Store", on_click=sttore_df)
    edit_df = st.data_editor(st.session_state.edit_df, ...)

I edited my code right as above,

and what I want to do is, If I press the button, I’ll do something that fits the button.

btn_reset should initializes the value, and btn_update should updates the value.

If I use print in it, it changesd, but it doesn’t change what’s been shown (first setted edit_df), it’s not reset too …

Is there a way to do this?

Hi @nuhyc

Glad to hear that you’ve got some ideas out of the suggestion and yes you’re headed in the right direction with callbacks. By using print nothing is saved, so you may have to assign/append values to the appropriate variables so that they are saved/updated.