Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an on_complete or on_focus_leave for st.data_editor to debounce saving state #8604

Open
2 tasks done
Ben-Epstein opened this issue May 3, 2024 · 3 comments
Open
2 tasks done
Labels
area:events feature:st.data_editor type:enhancement Requests for feature enhancements or new features

Comments

@Ben-Epstein
Copy link

Ben-Epstein commented May 3, 2024

Checklist

  • I have searched the existing issues for similar feature requests.
  • I added a descriptive title and summary to this issue.

Summary

I'm looking for a trigger for data_editor that fires only when the user leaves focus of the table, rather than on every change to every cell. Kind of like a debounce to the table.

Why?

I want to use the data editor in a multi page application and keep it persisted across pages in an intuitive way.

The naive thought would be

st.session_state["my_data"] = st.data_editor(st.session_state["my_data"], ...)

You cant do this because it causes the table to reset whenever you change a value. If I add a row, it then gets removed when I move to a new cell/click out.

There is a key and on_change but that has the same issue, wherein I can do something like

def _update_state():
    base_df = se.session_state["my_data"].copy()
    edited_rows = st.session_state.get("ed", {})
    added = edited_rows.get("added_rows", [])
    edited = edited_rows.get("edited_rows", {})
    deleted_idxs = edited_rows.get("deleted_rows", [])
    if deleted_idxs:
        base_df = base_df.drop(deleted_idxs)
    for idx, changes in edited.items():
        for col, changed_value in changes.items():
            base_df.loc[idx, col] = changed_value
    for new_row in added:
        ... # add new rows
    if not st.session_state["my_data"].equals(base_df):
        st.session_state["my_data"] = base_df

st.data_editor(st.session_state["my_data"], key="ed", on_change=_update_state)

but when you update my_data in session state, it breaks out the users focus from the data editing table.

How?

What i'd love is a trigger on_complete or on_focus_remove where the user actively chooses to click out of the table, then the trigger fires. In that world, my _update_state would actually work perfectly.

Additional Context

This relates to #7749 but i wanted to formalize the request

@Ben-Epstein Ben-Epstein added the type:enhancement Requests for feature enhancements or new features label May 3, 2024
Copy link

github-actions bot commented May 3, 2024

To help Streamlit prioritize this feature, react with a 👍 (thumbs up emoji) to the initial post.

Your vote helps us identify which enhancements matter most to our users.

Visits

@amanchaudhary-95
Copy link

I've the same issue. Every time I try to update the dataframe using the on_change callback, it breaks out the focus from the data editing table. It's not ideal from UI perspective.

@Ben-Epstein
Copy link
Author

This is actually how the behavior of text_area works today. on_change only takes effect when the user clicks out of scope of the text area box.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:events feature:st.data_editor type:enhancement Requests for feature enhancements or new features
Projects
None yet
Development

No branches or pull requests

3 participants