Styler not working when using st.data_editor to add a dataframe check column

This is my first post here, so apologies if it’s missing any info needed.

I am showing a few dataframes with stocks earnings data I previously capture during the day. After querying my DB I need some of this data colour formatted so that I visually identify interesting names. I did this with a pandas styler and applied it to the dataframe, calling the styler with st.write(dataframe). Next step was to add a tick column so that i could select the interesting names and sort the dataframe by this column. For this, the best way I found in the documentation was to create a ‘check’ column in the dataframe and then call the styler with st.data_editor like this:

 st.data_editor(best_earnings_styler,
            column_config={'check': st.column_config.CheckboxColumn(
                                                                    'Select',
                                                                    help='Select stocks',
                                                                    default=False,
            )}
        )

Now, my issue is that st.data_editor is not processing styler colour configuration, when st.write or the st.dataframe where behaving correctly.

Debugging data:
I am running the app locally, there is no error message, it simply doesn’t accept the colors style, and this is my full code:

 # Code to format best stock earnings  
    if st.checkbox('Show best earnings'):

        # Convert columns to numeric type before applying formatting
        best_earnings_df['EPS_chg'] = pd.to_numeric(best_earnings_df['EPS_chg'], errors='coerce') 
        best_earnings_df['Rev_chg'] = pd.to_numeric(best_earnings_df['Rev_chg'], errors='coerce')
        best_earnings_df['EPS_surprise'] = pd.to_numeric(best_earnings_df['EPS_surprise'], errors='coerce')
        best_earnings_df['Rev_surprise'] = pd.to_numeric(best_earnings_df['Rev_surprise'], errors='coerce')
        best_earnings_df['Avg_volume'] = pd.to_numeric(best_earnings_df['Avg_volume'], errors='coerce')
        best_earnings_df['check'] = False
        best_earnings_styler = best_earnings_df.style.applymap(color_negative_red, subset=['EPS_chg','EPS_surprise', 'Rev_chg', 'Rev_surprise'])\
            .apply  (background_gradient, 
                    subset=['EPS_chg'],
                    m=best_earnings_df['EPS_chg'].min(),
                    M=best_earnings_df['EPS_chg'].max(),
                    cmap=custom_cmap)\
            .apply  (background_gradient, 
                    subset=['EPS_surprise'],
                    m=best_earnings_df['EPS_surprise'].min(),
                    M=best_earnings_df['EPS_surprise'].max(),
                    cmap=custom_cmap)\
            .apply  (background_gradient, 
                    subset=['Rev_chg'],
                    m=best_earnings_df['Rev_chg'].min(),
                    M=best_earnings_df['Rev_chg'].max(),
                    cmap=custom_cmap)\
            .apply  (background_gradient, 
                    subset=['Rev_surprise'],
                    m=best_earnings_df['Rev_surprise'].min(),
                    M=best_earnings_df['Rev_surprise'].max(),
                    cmap=custom_cmap)\
            .format("{:.2f}", subset=['EPS_chg', 'EPS_surprise','Rev_chg','Rev_surprise'])\
            .format("{:,.0f}", subset=['Avg_volume'])


      
        st.data_editor(
            best_earnings_styler,
            column_config={'check': st.column_config.CheckboxColumn(
                                                                    'Select',
                                                                    help='Select stocks',
                                                                    default=False,
            )}
        )

Hello @spanisharlem, and welcome to our community! It’s great to have you here! :hugs:

Regarding your issue with st.data_editor, it might not fully support integration with the Pandas Styler. I’ll need to confirm this with a few colleagues, but this could be the root of the problem.

If you absolutely need cell formatting similar to what Pandas Styler provides, have you considered switching to Streamlit AG Grid? It might offer the functionality you’re looking for.

Best,
Charly

thanks, @Charly_Wargnier , I read in a threat that it was partially compatible. What that means, it’s not specified in the post. I am using the styler as it is the only way I have seen I can introduce gradient colours to the number columns, that works great with st.dataframe but not with st.data_editor. But then I need a checkbox column in order to do a manual selection but this functionality is not clickable if called with st.dataframe, and works well with st.dataframe. I wonder if I need to choose between one or the other functionality. Thanks!

Thank you for your feedback, @spanisharlem.

@lukasmasuch, who took part in developing st.data_editor, may be able to provide more insights on this.

As of 1.34.0 the limitation of the data editor with regards to the styler is specified in the note of the data parameter.

Note

  • Styles from pandas.Styler will only be applied to non-editable columns.
  • Mixing data types within a column can make the column uneditable.
  • Additionally, the following data types are not yet supported for editing: complex, list, tuple, bytes, bytearray, memoryview, dict, set, frozenset, fractions.Fraction, pandas.Interval, and pandas.Period.
  • To prevent overflow in JavaScript, columns containing datetime.timedelta and pandas.Timedelta values will default to uneditable but this can be changed through column configuration.
1 Like

Many thanks for pointing it out @ferdy , I read the doc but did not fully understand it. Now I just added to data_editor the argument disabled=() with the column names I wanted to format, which make this columns non editable and it is now running perfectly.

Thanks for the heads up, @ferdy! :pray:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.