Hey, I want to select and unselect rows of a dataframe i load.
here is the code
but i am unable to use “Unselect cards” and “Select Cards” if i already clicked on the select box in the app. “unselect cards” works only after i clicked “Select Cards” and all cards are selected. Then I am able to unselect some by hand and all via “Unselect Cards”.
import streamlit as st
import numpy as np
import pandas as pd
Initialize session state variables
if ‘data’ not in st.session_state:
st.session_state.data = pd.DataFrame(
[
{“command”: “st.selectbox”, “rating”: 4, “is_widget”: True},
{“command”: “st.balloons”, “rating”: 5, “is_widget”: False},
{“command”: “st.time_input”, “rating”: 3, “is_widget”: True},
]
)
st.session_state.data.insert(0, “Select”, False)
if ‘selected_rows’ not in st.session_state:
st.session_state.selected_rows =
Get dataframe row-selections from user with st.data_editor
if ‘edited_df’ not in st.session_state:
st.session_state.df_with_selections = st.session_state.data.copy()
st.session_state.edited_df = st.data_editor(
st.session_state.df_with_selections,
hide_index=True,
column_config={“Select”: st.column_config.CheckboxColumn(required=True)},
#disabled= st.session_state.df_with_selections.columns,
)
Unselect All Cards
if st.button(“Unselect All Cards”):
st.session_state.df_with_selections[“Select”] = False
st.session_state.edited_df[“Select”] = False
st.rerun()
Select All Cards
if st.button(“Select All Cards”):
st.session_state.df_with_selections[“Select”] = True
st.session_state.edited_df[“Select”] = True
st.rerun()
Filter the dataframe using the temporary column, then drop the column
st.session_state.selected_rows = st.session_state.edited_df[st.session_state.edited_df[“Select”]]
Update the session state data with any changes made in the data editor
st.session_state.selected_rows.update(st.session_state.df_with_selections)
st.write(“Your selection:”)
st.write(st.session_state.selected_rows)
If you’re creating a debugging post, please include the following info:
- Are you running your app locally or is it deployed?
locally
. Share the full text of the error message (not a screenshot).
no error message
- Share the Streamlit and Python versions.
Streamlit, version 1.34.0
Python 3.9.19