i want to read file by using file_uploader
than i have a function that allow user to update the data in the uploaded file than it save the file on the desktop after that it reopen the saved file and display the updated data.
What i want is to override the uploaded file in order to display the new dataframe once the user click on button replace
In the image above i want to replace the file book2.xlsx by the updated file after the user change cat6 to cat1
code:
import pandas as pd
import streamlit as st
def main():
Activities = ["EDA","Plot","About"]
choice = st.sidebar.selectbox("Select Activity",Activities)
radio = st.sidebar.radio(label="", options=["Single File", "Multiple Files"])
df = pd.DataFrame()
pt1=re.compile(".csv$")
pt2=re.compile(".xlsx$")
if radio == "Multiple Files":
data = st.sidebar.file_uploader('Multiple Excel files', type=["csv","xlsx","xls"], accept_multiple_files=True)
elif radio=="Single File":
data = st.sidebar.file_uploader("Upload Dataset",type=["csv","xlsx","xls"])
if data is not None:
#EDA Page
if choice =="EDA":
st.subheader("Exploratiry Data Analysis")
if radio=="Single File":
if data.type =="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
df=pd.read_excel(data)
#show replace
if st.checkbox("replace"):
mydf = st.dataframe(df)
columns = st.selectbox("Select column", df.columns)
old_values = st.multiselect("Current Values",list(df[columns].unique()),list(df[columns].unique()))
with st.form(key='my_form'):
col1,col2 = st.beta_columns(2)
st_input = st.number_input if is_numeric_dtype(df[columns]) else st.text_input
with col1:
old_val = st_input("old value")
with col2:
new_val = st_input("new value")
if st.form_submit_button("Replace"):
df[columns]=df[columns].replace(old_val,new_val)
st.success("{} replace with {} successfully ".format(old_val,new_val))
excel = df.to_excel(r"F:\book2.xlsx", index = False, header=True,encoding="utf-8")
df =pd.read_excel(r"F:\book2.xlsx")
mydf.add_rows(df)