Would love to enable checkbox for single selection while using st.data_editor() with column_config={"Select": st.column_config.CheckboxColumn()}
Code snippet: (from documentation )
def dataframe_with_selections(df):
df_with_selections = df.copy()
df_with_selections.insert(0, "Select", False)
# Get dataframe row-selections from user with st.data_editor
edited_df = st.data_editor(
df_with_selections,
hide_index=True,
column_config={"Select": st.column_config.CheckboxColumn(required=True)},
disabled=df.columns,
num_rows="dynamic",
)
# Filter the dataframe using the temporary column, then drop the column
selected_rows = edited_df[edited_df.Select]
return selected_rows.drop('Select', axis=1)
selection = dataframe_with_selections(df)
st.write("Your selection:")
st.write(selection)
What I’m aiming for :
Single checkbox selection is enabled only.
How the above code is working:
With the above code, end-user can opt for multiple selections.
Probably I’m missing something very obvious. Any help would be appreciated!
Thanks for the question, could you elaborate a bit more on “single checkbox selection is enabled only”, not quite sure what the intended checkbox should be like.
In addition to default, st.column_config.CheckboxColumn() also has disabled and default parameters that may be explored upon.
Hi,
I think I have the same issue. Is there a possibility to implement the option for single selection next to multiple selection in st.column_config.CheckboxColumn() or data_editor? Or maybe it was done and I don’t see the corresponding parameter. As far as I know, *it is only possible to check multiple check boxes. However, I want the user to only check one box. It is not possible via session_state to figure out which was the last selection due to the row index sorting. Otherwise, I could force to put all to false except the last selection. My workaround at the moment: If the user checks a second box and the index of the selection therefore exceeds 1, i force a page reload to uncheck (put false) all the boxes. This is rather ugly solution but the only one I have found.
# Get dataframe row-selections from user with st.data_editor
edited_df = st.data_editor(
df_with_selections,
key="data_editor",
hide_index=True,
column_config={"Select": st.column_config.CheckboxColumn(required=True)},
disabled=df_tools.columns,
)
# Filter the dataframe using the temporary column, then drop the column
selected_rows = edited_df[edited_df.Select]
return selected_rows.drop('Select', axis=1)
if len(selected_rows.index) > 1:
streamlit_js_eval(js_expressions="parent.window.location.reload()")
return selected_rows
Hey Avra, sorry totally forgot to answer on this. We already chatted that there’s probably no good way to do this today. I thought about it a bit today but I also couldn’t find any workaround (other than manually checking how many rows were selected and showing an error). We’ll definitely support this though once we implement row selections as a built-in feature!