Using st.data_editor on dataframes that have styled columns

Summary

iam using st.data_editor to create a interactive dataframe, but in my dataframe cells in “Status” column has background color (green or red) based on the value of that cell.
so when u try to update any other columns of my dataframe using st.data_editor the updated value is not visible in the dataframe, i even tried disabling edit option on the “Status” column

Steps to reproduce

Code snippet:

styled = filtered_df.style.applymap(highlight_status, subset=pd.IndexSlice[:, ['Status']])
edited_df=st.data_editor(styled,use_container_width=True,hide_index=True,disabled=["Host IP","Component","Validation_Command","Status"])

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

To display the updated value in the UI

Actual behavior:
its not displaying or updating the dataframe with the updated value
Screenshot 2023-06-07 150935

in the above given screenshot u can see that status column has a background color and its not editable but notification column is editable but when i try to edit the notification its not getting updated

Debug info

  • Streamlit version: 1.23.1
  • Python version:
  • Using PyEnv - Pycharm
  • OS version: Windows 11
  • Browser version:

Hey, I’m having the same issue - seems like data_editor doesn’t really work with styled dataframes which is a bit of a shame as you have to choose one or the other. Hopefully this is something that can be fixed in the next release?

1 Like

The current version of st.data_editor should be partly compatible with Pandas styler as input, but there are restrictions:

  1. Only works for non-editable / disabled columns.
  2. Currently, it returns the edited data as a dataframe and not as Pandas Styler. So you have to apply the styling logic again on the edited df if you want to show that.

But your issue sounds like something that also might be a bug. Maybe what could help is a video that showcases the editing issue.

1 Like

I encountered the same problem and created a small demo.

import streamlit as st
import pandas as pd

df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [1, 2, 3, 4]})

col1, col2 = st.columns(2)
with col1:
    st.subheader("DataFrame")
    st.data_editor(
        df,
        key="display1",
        disabled=("B"),
    )

with col2:
    st.subheader("Styler")
    styled_df = df.style.format(
        {
            "A": "{:.1f}%",
            "B": "{:.2f}€",
        }
    )
    st.data_editor(
        styled_df,
        key="display2",
        disabled=("B"),
    )

st.write("Streamlit version: ", st.__version__)

On the left, the input is simply the dataframe. On the right, some styling is applied and a pandas styler is handed over to st.data_editor. Editing works perfectly well in the left, but does not work on the right.

Streamlit version: 1.23.1
OS: Windows 11
Python: 3.10
Browser: Chrome 114.0.5735.134

I tried recording a screencast, but the .webm format is not allowed for upload :confused:

1 Like

Thanks so much for the short, executable example! I was able to make a recording to share so everyone can see what we’re talking about here:
4E029CF1-82BA-453D-8B6E-92633973F570

I also removed the styling from column A, but it still failed to commit edits. If I write session state above and below the editor, I can see the edit display in session state before the widget, but it resets when it re-renders the data editor.

I believe this issue distills down to how Streamlit determines equivalency of pandas styler objects. As a workaround, put your styler object into session state and pass it into the data editor from session state. If you don’t redefine your styler with each rerun, it seems to avoid the issue. I’ve modified your code here to demonstrate:

import streamlit as st
import pandas as pd

df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [1, 2, 3, 4]})
styled_df = df.style.format({"B": "{:.2f}€"})

if 'df' not in st.session_state:
   st.session_state.df = df
   st.session_state.styled_df = styled_df

col1, col2 = st.columns(2)

with col1:
   st.subheader("Styled df in session state")
   st.data_editor(
       st.session_state.styled_df,
       key="display",
       disabled=("B"),
   )

with col2:
   st.subheader("Styled df redefined each rerun")
   st.data_editor(
       styled_df,
       key="display2",
       disabled=("B"),
   )

st.write('Styler equivalency: ' + str(st.session_state.styled_df == styled_df))
st.write("Streamlit version: ", st.__version__)

492003BB-70A3-4B42-8403-1C14F8379712

I’ve submitted an issue on GitHub here: #6898

2 Likes

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