st.dataframe selection problem

this code generates the warning st.warning(“Selection object has no ‘rows’ attribute.”) and no check boxes are shown on rows

import streamlit as st
import pandas as pd

st.title(“Dataframe Selection Test”)

df = pd.DataFrame({“col1”: [1, 2, 3], “col2”: [“A”, “B”, “C”]})

selected_data = st.dataframe(df, selection_mode=“multiple-rows”, hide_index=True)

if hasattr(selected_data, ‘selection’) and selected_data.selection is not None:
if hasattr(selected_data.selection, ‘rows’) and selected_data.selection.rows is not None:
if selected_data.selection.rows:
st.write(“Selected rows indices:”, selected_data.selection.rows)
else:
st.write(“No rows selected.”)
else:
st.warning(“Selection object has no ‘rows’ attribute.”)
else:
st.warning(“No selection object found.”)

Hi @Roger3, welcome to the forum!

Can you edit your post so that it’s a code block, so that we can see the indentation? You can do this by putting ```in a line before your code, and after your code.

Problem was solved by using the following code. Apparently the original did not structure the selected_data properly, and the event code was needed. Thanks for your reply.

Blockquote

sttest.py

import streamlit as st
import pandas as pd

st.title(“Dataframe Selection Test”)

if “df” not in st.session_state:
st.session_state.df = pd.DataFrame({“col1”: [1, 2, 3], “col2”: [“A”, “B”, “C”]})

event = st.dataframe(
st.session_state.df,
use_container_width=True,
key = “data”,
on_select = “rerun”,
selection_mode=[“multi-row”],
)

if event is not None:
st.write(“Selected rows:”, event.selection.rows)
st.write(“Selected columns:”, event.selection.columns)

Blockquote

‘’’

sttest.py

import streamlit as st
import pandas as pd

st.title(“Dataframe Selection Test”)

if “df” not in st.session_state:
st.session_state.df = pd.DataFrame({“col1”: [1, 2, 3], “col2”: [“A”, “B”, “C”]})

event = st.dataframe(
st.session_state.df,
use_container_width=True,
key = “data”,
on_select = “rerun”,
selection_mode=[“multi-row”],
)

if event is not None:
st.write(“Selected rows:”, event.selection.rows)
st.write(“Selected columns:”, event.selection.columns)

‘’’

Sorry, having trouble formatting code