Exporting dataframe to xlsx by clicking st.button("Export")

I am export dataframe as excel file by clicking st.button(โ€œExportโ€). For that I need to upload a excel file and process it another function, in return I am receiving two dataframe from that function. After receiving I am displaying it with st.table().

Yeah It displays the Dataframe correctly. But when the export button is clicked. It makes the received dataframe to empty dataframe.

Kindly help me resolve this issue.

Below are my code, which creates above scene:

st.header("Happy Cow")
    
    
 selected_tab = option_menu(
        menu_title = None,
        options=["A", "B","C","D"],
        default_index=0,
        orientation="horizontal"   
    )
    
    if selected_tab == "A":
        own_payment_excel = pd.DataFrame()
        neft_payment_excel = pd.DataFrame()

        with st.form("A_forml"):
            uploaded_file = st.file_uploader("Upload a  File (Excel):")
            col1, col2, col3, col4, col5 = st.columns(5)
            with col1:
                company = st.selectbox("Select a company", ["Company A", "Company B"])

            with col5:
                st.write("")
                st.write("")
                generatebtn = st.form_submit_button("Generate Excel")
                if generatebtn:
                    payment_excel_A, payment_excel_B = payment_excel_gen(company, uploaded_file)

        # Export  to Excel file
        
        exportbtn = st.button("Export", key="export_button")
          
        if exportbtn:

                payment_excel_A.to_excel("payment_excel_A.xlsx", index=False)
                st.success("payment_excel_A exported to Excel successfully!")
        else:
            st.warning("payment_excel_A is empty. No data to export.")


        tab1, tab2 = st.tabs([company + " Payment Excel", company + " Payment Excel - Others"])
        
        with tab1:
            if not own_payment_excel.empty:
                st.header("Company A ")
                st.table(payment_excel_A)
                st.write("") 
        with tab2:
            if not neft_payment_excel.empty:
                st.header("Company B")
                data = payment_excel_B.to_dict(orient='records')
                st.table(data)
                st.write("")

When dataframes are recieved it displays it in their respective tabs. when export button is clicked it empties it.

Kindly guide me to resolve it. Thanks in advance

On a first glance, I can see that you have used the option โ€œAโ€ many times the code, but in the selected tab variable, in the options field, the code has a typo !

It should have been :
selected_tab = option_menu( menu_title = None, options=["A", "B","C","D"], default_index=0, orientation="horizontal" )

I am sure this will fix the error, if not do reply and weโ€™ll see what else can be done !

Hi, @Hitesh_Bandhu. Thanks for the reply!. I have corrected it. But it is not the error that facing. Whenever the button is clicked, It resets the session. So I have implemented session_state to handle it.

Thank you once again.

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