How to refresh datasets in st.data_editor

hello,
I want to implement a simple app that can read in a CSV file, display and modify it using “st.data_editor”. I would like to add a ‘Reset’ button, so that if I click on it, the data in the st.data_editor table will revert to the original data from the CSV file, rather than the modified data.

Original:

Edit:

Click function:

here is my code:

import streamlit as st
import pandas as pd


meta_ori = None

def main():
    global meta_ori
    meta_file = st.file_uploader("Choose a CSV file for group comparisons", type="csv")

    if meta_file is not None:
        # load data
        meta     = pd.read_csv(meta_file)
        meta_ori = meta.copy()

        if "meta" not in st.session_state:
            st.session_state.meta = meta

        # show data
        st.subheader("group comparisons")
        annotated = st.data_editor(st.session_state.meta, hide_index=True, use_container_width=True, disabled=["sample"])

        st.button("Reset", on_click=update_value)

def update_value():
    global meta_ori
    print( meta_ori )
    st.session_state.meta = meta_ori


if __name__ == "__main__":
    main()

Just create a new key for the data editor.

from streamlit import session_state as ss
import uuid

# Create de key
if 'dek' not in ss:
    ss.dek = str(uuid.uuid4())

def update_value():
    """
    Located on top of the data editor.
    """
    ss.dek = str(uuid.uuid4())  # triggers reset

edf = st.data_editor(..., key=ss.dek)

st.button("Reset", on_click=update_value)
1 Like

:star_struck: Thanks very much !!!

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