Streamlit st.data_editor access edited_rows dictionary

Hey everyone,

I have a question concerning the edited_rows dictionary of streamlit data editor. How do I access that dictionary? In code examples I read, edited_rows is used in a similar way like I try to use it, but I’m obviously doing something wrong.

My current code snippet:

import streamlit as st
import pandas as pd
from streamlit import session_state as ss

def data_editor_changed():
    print("edited_rows: ", ss.edited_df["edited_rows"])

df = pd.DataFrame(
    [
       {"command": "st.selectbox", "rating": 4, "is_widget": True},
       {"command": "st.balloons", "rating": 5, "is_widget": False},
       {"command": "st.time_input", "rating": 3, "is_widget": True},
   ]
)
ss.edited_df = st.data_editor(df, on_change = data_editor_changed)

favorite_command = ss.edited_df.loc[ss.edited_df["rating"].idxmax()]["command"]
st.markdown(f"Your favorite command is **{favorite_command}** 🎈")

The error message I get:
KeyError: 'st.session_state has no key “edited_rows”. Did you forget to initialize it?

I thought that the edited_rows key would be added automatically to session state.

Link to documentation:

Versions I’m using:

  • Streamlit 1.30.0
  • Python 3.9.17

Thank you!

Add a key to the editor.

ss.edited_df = st.data_editor(df, on_change = data_editor_changed, key='ed')

And use that key to get the edited_rows.

def data_editor_changed():
    print("edited_rows: ", ss.ed["edited_rows"])

Thank you for your quick response, that works! Could have thought of it myself…

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