Pandas styler with custom style

Hello,

I want to show below dataframe styler using st.dataframe but it is not showing the custom style as per given in make_bar_style, Your inputs will be valuable:

import pandas as pd
import streamlit as st

# example multi-index dataframe
data = {'level_0': [1, 2, 3],
        'level_1': ['A', 'B', 'C'],
        '01/23/2023': ['20%', '30%', '40%'],
        '01/24/2023': ['10%', '20%', '30%'],
        '01/25/2023': ['15%', '25%', '35%'],
        '01/26/2023': ['5%', '10%', '15%'],
        '01/27/2023': ['1%', '2%', '3%']}

df = pd.DataFrame(data)
df = df.set_index(['level_0', 'level_1'])

def make_bar_style(x):
    if '%' in str(x):
        x = float(x.strip('%'))
        return f"background: linear-gradient(90deg,#5fba7d {x}%, transparent {x}%); width: 10em"  
    return ''


st.dataframe(df.style.applymap(make_bar_style))

Thanks,
Nilesh

1 Like

Hi @Nilesh_Dalvi ,

This should work with st.table but not with st.dataframe. st.dataframe supports pandas styler but only background color, font color & display value. The reason is because st.dataframe uses HTML canvas and thus won’t likely be able to support this (won’t have access to the html dom with HTML canvas).

1 Like

Hi @willhuang - Thanks for this, st.table is working.

Hello @willhuang , does st.dataframe support background color, font color & display value for all streamlit versions? I’m in 1.22 because it’s the one available in Snowflake and it seems background does not work, and it would be quite nice. Here an example:

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' for _ in col]

def color_backgroubd_red_column(col):

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

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

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

And this is the result:


The background is ignored in st.dataframe (in st.table works fine).

Hi @Ricardo_Picatoste ,

thanks for asking your question. Setting background colors in st.dataframe should work. I just changed background: red' to background-color: red` and that seemed to fix it.

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' for _ in col]

def color_backgroubd_red_column(col):

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

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

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

Screenshot 2023-10-27 at 1.54.50 PM

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

Hi @Ricardo_Picatoste , I don’t think it’s quite possible right now to style headers:

:frowning:

You would likely need some custom css / hacky way of doing it if I had to guess.

Thank you for the heads up. I hope that now that streamlit is part of, and will run often in Snowflake, a solution that works in there will be prioritized.

I am pushing to have streamlit dashboards at the company, but not having the possibility of having properly formatted tables is a show-stopper.

Hi @Ricardo_Picatoste , I hear you and totally understand that not having the possibility of having properly formatted tables is a show-stopper on your side. I can let our product team know and see if we can get it prioritized.

If you could, can you upvote and comment on this issue:

It helps our team understand where to put resources into and help prioritize.

Thanks @willhuang , and done, voted!

Hello Everyone,

I want to color my list-like valued column but output is strange:


when you click to the value it shows the colored value, otherwise not.

Here is my coloring code block:

            st.dataframe(
                df_reviews_filtered.style.apply(
                    lambda row: ["color: green" * len(row) for _ in row],
                    axis=1,
                    subset=["positive_topics"],
                ).apply(
                    lambda row: ["color: red" * len(row) for _ in row],
                    axis=1,
                    subset=["negative_topics"],
                )
            )