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!
+1 for this feature (something equivalent to CheckboxColumn but only one cell can be selected at any given time. Many useful use cases for this , and the current workaround is very very messy (I’m using st.data_editor’s onchange, having to reset all the oother checked columns, and then rebuilding the dataframe. Which has a side effect that it loses any columnst he user may have sorted on).
Essential for me – multiselect is a very different problem being solved, single select is essential for many (or at least my) applications. Stuck using a dodgey aggrid workaround until this is solved.
Radio, checkbox, or double-click row select (if highlighted) would be fine as long as it’s single-select.
Vote +1!
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.