Summary
In the following example, I have 4 checkboxes with keys A, B, C, D. The goal is to let the user select a checkbox and, based on which key he enables, it displays a data editor with a dataframe with that key and two columns of values “Low”/“High”.
Problem
The thing is, for some reason, let’s say I enable A and change the “High” value to 37, ithen ok. But when I change it to 98, it goes back to 37. It is only if I try to change again to, whatever value let’s say 51, that it changes to 51. How can I fix that?
Code snippet
import pandas as pd
import streamlit as st
if 'df' not in st.session_state:
st.session_state['df'] = pd.DataFrame(
index=['A', 'B', 'C', 'D'],
columns=['Low', 'High', 'Active'],
data=[[.0, 50., False], [.0, 500., False], [.1, 1.5, False], [.0, 50., False]])
st.session_state['df']['Active'].loc['A'] = st.checkbox('A')
st.session_state['df']['Active'].loc['B'] = st.checkbox('B')
st.session_state['df']['Active'].loc['C'] = st.checkbox('C')
st.session_state['df']['Active'].loc['D'] = st.checkbox('D')
active_df = st.session_state['df'][st.session_state['df']['Active'] == True].copy()
edited_df = st.data_editor(active_df[['Low', 'High']])
if edited_df is not None:
for idx, row in edited_df.iterrows():
st.session_state['df'].loc[idx, ['Low', 'High']] = row['Low'], row['High']
Random Idea
Well I’m struggling to find a logic to fix that. I thought that maybe if I call a function on the “on_change” parameter of “st.data_editor” it could work, but didn’t I need to pass “edited_df” as an argument? But this doesn’t work, so I’m completely lost lol Would appreciate any help! Thanks