Hi.
I’m using streamlit-aggrid custom component to display dataframes. I’d like to color an entire row based on a specific column’s value. I’m not sure how to do it as there are no examples provided for this. Please help.
Best wishes,
Dinesh
Hi.
I’m using streamlit-aggrid custom component to display dataframes. I’d like to color an entire row based on a specific column’s value. I’m not sure how to do it as there are no examples provided for this. Please help.
Best wishes,
Dinesh
I figured out how to do it, thanks to code to enable other functionality not directly supported that a user posted on the Aggrid thread in Show the Community channel. The following snippet should help (I’m assuming the dataframe has a column called state, and you know about using Aggrid otherwise including imports):
jscode = JsCode("""
function(params) {
if (params.data.state === 'failed') {
return {
'color': 'white',
'backgroundColor': 'red'
}
}
};
""")
gridOptions = gb.build()
gridOptions['getRowStyle'] = jscode
grid_response = AgGrid(
df,
gridOptions=gridOptions,
allow_unsafe_jscode=True,
)
Hope this helps others searching for a similar answer,
Dinesh
hi there,
what if I want to color a row, based on a condition between other fields/columns in my dataframe?
How do the JS function will look like? how do i send over the params?
suppose I have a dataframe with the following fields:
ColumnA, ColumnB, ColumnC
and I want to apply a certain format for the entire row, if the following condition is true: ColumnA != ColumnB+ColumnC.
please help! Thanks
hi,
very helpful, it works. What if the column’s name has a blank space (example: “city state”)? In this case it does not work.
You’ll need to configure the column with both a header_name and a field.
gob.configure_column(field="city_state", header_name="city, state")
jscode = JsCode("""
function(params) {
if (params.data.city_state === 'Corvallis, Oregon') {
return {
'color': 'black',
'backgroundColor': 'orange'
}
}
if (params.data.city_state === 'Eugene, Oregon') {
return {
'color': 'yellow',
'backgroundColor': 'green'
}
}
};
""")
I didn’t check the syntax, but you may get the gist of the solution.
Just set the if statement to include the conditiion you’re trying to meet: e.g.,
jscode = JsCode("""
function(params) {
if (params.data.ColumnA != (params.data.ColumnB + params.data.ColumnC)) { return ....
Hi, I’ve tried this and it sort of works. I can change the background color, make the text bold, anything I want, except change the text color itself. Do you know why? Thanks
Hi, this code with changing background color for a row doesn’t work for me when I create AgGrid with ‘key’ parameter. Any idea please? Thank you!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
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.
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.
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.
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.