Pandas styler with custom style

Thanks a lot @willhuang ! it does work indeed :slight_smile:

Please let me take advantage to ask: do you know if this can be applied to the header? I do it with

import streamlit as st
import pandas as pd

data = {
    'A': [1, 2, 3, 4, 5],
    'B': [5, 4, 3, 2, 1],
    'C': [5, 4, 3, 2, 1],
}
df = pd.DataFrame(data)

def color_red_column(col):
    return ['color: red; font-weight: bold' for _ in col]

def color_backgroubd_red_column(col):

    return ['background-color: red' for _ in col]

header_styles = {
    'A': [{'selector': 'th', 'props': [('background-color', 'red')] }],
    'B': [{'selector': 'th', 'props': [('background-color', 'blue')] }],
}

styled_df = (df.style.apply(color_red_column, subset=['A'])
                    .apply(color_backgroubd_red_column, subset=['B'])
                    .set_table_styles(header_styles)
            )

st.table(styled_df)
st.dataframe(styled_df)

This locallly applies the style correctly with st.table, but not with st.dataframe. In snowflake it doesn’t work for st.table as expected.

The local screenshot:

And in snowflake:
image

If I manage to style the headers in st.dataframe, it would be a huge step.

1 Like