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))
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).
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)
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)
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 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.