Using st.data_editor on dataframes that have styled columns

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