Hello, i have a locally run app where i used this technique to create a dtaframe with row selections. My question is if it is possible to make the dataframe work in a way that only one row is selectable, e.g. if one selected more then one row then the previous row would get unselected.
import streamlit as st
import pandas as pd
# Sample DataFrame
@st.cache_data
def load_data():
data = {
'A': range(10),
'B': range(10, 20),
'C': range(20, 30)
}
return pd.DataFrame(data)
df = load_data()
st.title('Highlight DataFrame Rows')
# Function to highlight odd/even rows
def highlight_odd_even(df, odd=True):
styles = []
for i in range(len(df)):
if (i % 2 == 0 and not odd) or (i % 2 == 1 and odd):
styles.append('background-color: yellow')
else:
styles.append('')
return ['background-color: yellow' if s else '' for s in styles]
highlight_odd = st.button('Highlight Odd Rows')
highlight_even = st.button('Highlight Even Rows')
if highlight_odd:
styled_df = df.style.apply(highlight_odd_even, odd=True)
st.dataframe(styled_df)
elif highlight_even:
styled_df = df.style.apply(highlight_odd_even, odd=False)
st.dataframe(styled_df)
else:
st.dataframe(df)
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.