St_aggrid does not update when user inputs editable property

@PablocFonseca

Thanks a lot for the component.

See code below. I would like to have the user select whether he wants to modify a column (“editable”) or not (“not editable”). However, the user selection has no effect on how the component behaves. In the case below, at the first execution, “editable” returns true and the AgGrid component continues to allow to edit data in the column no matter what the user select successively.

Is there a way to make this work?

Thanks!

import streamlit as st
from st_aggrid import AgGrid, GridOptionsBuilder
import pandas as pd

df = pd.DataFrame([['1','2',''],['d','e',''],['','','']], columns=['first','second','modified_data'])

options = ('editable', 'not editable')

selected_option = st.selectbox('Do you want allow edits?', options)

edit_data = selected_option == 'editable'

gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_column('modified_data', editable=edit_data)
go = gb.build()

response = AgGrid(
        df, 
        gridOptions=go,
        reload_data=False, 
        key='Ag_1'
)

Hi @gaetanoe , Welcome to the Streamlit Community Forum!

Please, try this code below, it seems to work.

import streamlit as st
from st_aggrid import AgGrid, GridOptionsBuilder , GridUpdateMode
import pandas as pd

df = pd.DataFrame([['1','2',''],['d','e',''],['','','']], columns=['first','second','modified_data'])
options = ("True", "False")
selected_option = st.selectbox('Do you want allow edits?', options)

#gb.configure_column('modified_data', editable = selected_option)  # Doesnot work
gb = GridOptionsBuilder.from_dataframe(df)
if selected_option == 'True':
     gb.configure_column('modified_data', editable = True)
else:
     gb.configure_column('modified_data', editable = False)    
      
go = gb.build()
#st.write(go) # Check the Grid Options
response = AgGrid(
        df, 
        gridOptions=go,
        reload_data=False, 
        update_mode=GridUpdateMode.MODEL_CHANGED
)

streamlit-test-2021-12-02-11-12-40

Best
Avra

1 Like

First, thanks for the response. I thought I sent a reproducible error but I ended up sharing a small test full of bugs that you actually identified …

Your code works. Below I appended a piece of the code from my production dashboard. I probably incorrectly believe it does exactly what your code does, but still I am not able to edit. Note that the logic of making the column editable is dependent on whether I let the dashboard reload the data or not. “When the data is reloaded, then don’t make the column editable, otherwise make it editable”. What am I doing wrong?

import streamlit as st
from st_aggrid import AgGrid, GridOptionsBuilder , GridUpdateMode
import pandas as pd

df = pd.DataFrame([['1','2',''],['d','e',''],['','','']], columns=['first','second','modified_data'])

#########
# WORKS
#########
options = ("True", "False")
selected_option = st.selectbox('Do you want allow edits?', options)

#gb.configure_column('modified_data', editable = selected_option)  # Doesnot work
gb = GridOptionsBuilder.from_dataframe(df)
if selected_option == 'True':
     gb.configure_column('modified_data', editable = True)
else:
     gb.configure_column('modified_data', editable = False)    
      
go = gb.build()
st.write(go) # Check the Grid Options
response = AgGrid(
        df, 
        gridOptions=go,
        reload_data=False, 
        update_mode=GridUpdateMode.MODEL_CHANGED
)

#################
# DOES NOT WORK
#################
edit_options = ('refresh','hold')
reload_si = True
reload_si_text = st.radio("Scenario edit options:", edit_options)
if reload_si_text == edit_options[0]:
    reload_si = True
    edit_si = False
elif reload_si_text == edit_options[1]:
    reload_si = False
    edit_si = True

gb_sc = GridOptionsBuilder.from_dataframe(df)

if edit_si:
    gb_sc.configure_column('modified_data', editable = True)
else:
    gb_sc.configure_column('modified_data', editable = False)

go_sc = gb_sc.build()

st.write(f"editable {edit_si}")
st.write(go_sc)

response_si = AgGrid(
    df, 
    gridOptions=go_sc,
    resizable=True,
    update_mode=GridUpdateMode.MANUAL,
    reload_data=reload_si,
    key='Ag_si')

Hey, thanks for your response.

I’m glad that it worked for you :slight_smile:

I’m not certain about it, because based on your code, I see, you have a

This line perhaps be the issue in your case (my assumption, I may be wrong) .I’m bit ignorant about this use case . I need to test this later and check your code. Apologies!
How about checking it in the other open posts related to Ag-Grid ? here’s a link

Goodluck,
Avra

I found a problem here with Aggrid I am trying to store the value of filters used in aggrid for other purpose and I am not able to find wher does it store the information for example here I selected a filter age<20 and returned a table and now I want to use this filter to query a data from other table using same columns and I am not able to find a place where this age<20 would be stored to reuse