Experimental_data_editor not writing new changes to a file

Hello,
After editing the dataframe, I want to copy the new edited_df into a file, so that I can use it later. But I am not clear if this is possible in streamlit?

here is my code.

import streamlit as st
import pandas as pd


data=st.sidebar.file_uploader("Upload your file", type=['xlsx'])
with st.form("my_form"):
    submit_button = st.form_submit_button(label='Submit')
    if data is not None:
        df=pd.read_excel(data, engine='openpyxl', index_col=None)
        dd1=pd.DataFrame(df)
        pd.DataFrame.reset_index(dd1, drop=True, inplace=True)

        options = st.multiselect(
        'What are the fields  you want to display?',
        ['SN','CName','Hname','OS Version','Group','Site','Model','Risk','Risk Age','Detected Date','Severity','Impact Area','Impact Level','Category','Risk Triggered Date','Acknowledged','Risk ID','Details','Potential Impact','Mitigation','Corrective Action 1','Corrective Action 2','Has Fixit'])
        if options ==[]:
            st.write('You selected no fields')
            dd2=dd1
        else:   
            st.write('You selected:', options)
            dd2=dd1[options]

        dd2=dd2.style.format(precision=0)
        edited_df = st.experimental_data_editor(dd2)

if st.button("Update"):
    edited_df.to_excel("testfile2.xlsx")
    st.write("update the new filee")

Hi @raghava4u, yes this is possible in streamlit. It’s difficult to read your code because you didn’t post it as a code block, so all of the indentation has been lost, but at a glance it seems fine.

@blackary , thank you for response, i just updated with proper indentation. could you please check again.

Hi @raghava4u, thanks for the update. Unfortunately, most of your code is still being treated as plain text. To make it formatted as code, please put it in a code block by putting triple backticks ``` around it. Right now if I copy and paste your code to try it out it doesn’t work because when you pasted it as normal text, all of the quotes got converted to curly quotes. Pasting code within a code block is the only way to avoid your quotes and indentation being messed up that way.

See Using Streamlit: how to post a question in the Streamlit forum for more details.

Also, if your question is just: “Is this possible”, the answer is yes. If your question is: “Why isn’t this code working the way I expect”, could you please explain what the issue is?

@blackary i have updated the code block with code. and yes, my requirement is to load the data from excelfile, and add comments into some fields and save it back to a new excel file. ( later idea to push intoa sqqlite3 db).

but if you look at the code, when i save the edited_df into a new file, the added new informations are not updated/displayed in new excel file. hope I am clear with my question.

@blackary , did you get chance to check the code, appreciate any response.

Just a quick observation: You have your data editor inside a form. As such, I would not expect it to return the edited result in real time. I would expect the value of edited_df to reflect whatever the state was when the form submit button was last clicked. I see that the button for saving your edited dataframe is separate from the form.

So, are you:

  1. Editing the dataframe
  2. Clicking the form submit button so the data editor updates its output
  3. Clicking the update button (without any further edits that will get missed)

You might consider something like this not using a form at all

import pandas as pd
import streamlit as st

data = st.sidebar.file_uploader("Upload your file", type=["xlsx"])
if data is not None:
    df = pd.read_excel(data, engine="openpyxl", index_col=None)
    dd1 = pd.DataFrame(df)
    pd.DataFrame.reset_index(dd1, drop=True, inplace=True)

    options = st.multiselect(
        "What are the fields  you want to display?",
        [
            "SN",
            "CName",
            "Hname",
            "OS Version",
            "Group",
            "Site",
            "Model",
            "Risk",
            "Risk Age",
            "Detected Date",
            "Severity",
            "Impact Area",
            "Impact Level",
            "Category",
            "Risk Triggered Date",
            "Acknowledged",
            "Risk ID",
            "Details",
            "Potential Impact",
            "Mitigation",
            "Corrective Action 1",
            "Corrective Action 2",
            "Has Fixit",
        ],
    )
    if options == []:
        st.write("You selected no fields")
        dd2 = dd1
    else:
        st.write("You selected:", options)
        dd2 = dd1[options]

    dd2 = dd2.style.format(precision=0)
    edited_df = st.experimental_data_editor(dd2)

    if st.button("Update"):
        edited_df.to_excel("testfile2.xlsx")
        st.write("update the new filee")

    st.write(pd.read_excel("testfile2.xlsx", engine="openpyxl", index_col=None))

Thank heaps @blackary, appriciate your support.

1 Like

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