create an app that contains a button and a displayed data frame, upon clicking the button, some of the rows must be highlighted (odd), upon clicking another button, the others (even), you can use pandas style for this
Drop files here
or click to upload
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)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?