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.
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.
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.
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:
Editing the dataframe
Clicking the form submit button so the data editor updates its output
Clicking the update button (without any further edits that will get missed)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.