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”.
Share the Streamlit and Python versions.
Streamlit, version 1.34.0
Python 3.9.19
I tried to include both
st.session_state.df_with_selections[“Select”] = False
st.session_state.edited_df[“Select”] = False
in the buttons
but only st.session_state.df_with_selections[“Select”] = False
is needed. I can only select unselect in one direction.
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”.
Share the Streamlit and Python versions.
Streamlit, version 1.34.0
Python 3.9.19
I tried to include both
st.session_state.df_with_selections[“Select”] = False
st.session_state.edited_df[“Select”] = False
in the buttons
but only st.session_state.df_with_selections[“Select”] = False
is needed. I can only select unselect in one direction.
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.rerun()
# Select All Cards
if st.button("Select All Cards"):
st.session_state.df_with_selections["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)`
Felix Scope is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.