简体   繁体   English

如何使用 python 将多个数据帧写入 excel 并使用 streamlit 下载结果?

[英]How do I write multiple dataframes to excel using python and download the results using streamlit?

I have a Python script using streamlit, that allows the user to upload certain excel files, then it automatically runs my anslysis on it, and then I want them to download the results in xlsx format using the streamlit download button.我有一个使用 streamlit 的 Python 脚本,它允许用户上传某些 excel 文件,然后它会自动运行我的分析,然后我希望他们使用 streamlit 下载按钮以 xlsx 格式下载结果。 However, I know how to make them download one dataframe to a csv, but not an xlsx file using the streamlit download button, which is what I want to do.但是,我知道如何让他们使用流光下载按钮将 dataframe 下载到 csv,而不是 xlsx 文件,这是我想要做的。

Here's what I've tried so far, and this is after my analysis where I'm just trying to create the download button for the user to download the results that are stored in 3 different dataframes:这是我到目前为止所尝试的,这是在我的分析之后,我只是试图为用户创建下载按钮,以下载存储在 3 个不同数据帧中的结果:

Import pandas as pd
Import streamlit as st

# arrived_clean, booked_grouped, and arrived_grouped are all dataframes that I want to export to an excel file as results for the user to download. 

def convert_df():
    writer = pd.ExcelWriter('test_data.xlsx', engine='xlsxwriter')
    arrived_clean.to_excel(writer, sheet_name='Cleaned', startrow=0, startcol=0, index=False)
    booked_grouped.to_excel(writer, sheet_name='Output', startrow=0, startcol=0, index=False)
    arrived_grouped.to_excel(writer, sheet_name='Output', startrow=0, startcol=20, index=False)
    writer.save()

csv = convert_df()

st.download_button(
     label="Download data",
     data=csv,
     file_name='test_data.xlsx',
     mime='text/xlsx',
 )

When I first run the streamlit app locally I get this error:当我第一次在本地运行 streamlit 应用程序时,出现此错误:

"NameError: name 'booked_grouped' is not defined" “NameError:未定义名称‘booked_grouped’”

I get it because I haven't uploaded any files yet.我明白了,因为我还没有上传任何文件。 After I upload my files the error message goes away and everything runs normally.上传文件后,错误消息消失,一切正常运行。 However, I get this error and I don't see the download button to download my new dataframes:但是,我收到此错误,并且没有看到下载新数据帧的下载按钮:

RuntimeError: Invalid binary data format: <class 'NoneType'> line 313, in marshall_file raise RuntimeError("Invalid binary data format: %s" % type(data)) RuntimeError:无效的二进制数据格式:<class 'NoneType'> 第 313 行,在 marshall_file 中引发 RuntimeError("Invalid binary data format: %s" % type(data))

Can someone tell me what I'm doing wrong?有人可以告诉我我做错了什么吗? It's the last piece I have to figure out.这是我必须弄清楚的最后一块。

When I first run the streamlit app locally I get this error:当我第一次在本地运行 streamlit 应用程序时,出现此错误:

"NameError: name 'booked_grouped' is not defined" “NameError:未定义名称‘booked_grouped’”

Assuming your code假设你的代码

booked_grouped = st.fileuploader('Something.....`)

You can use the below method to skip the error您可以使用以下方法跳过错误

if booked_grouped:
    # All your code inside this indentation

To Download excel下载 excel

Convert all dataframes to one single excel将所有数据帧转换为单个dataframes

# Function to save all dataframes to one single excel
def dfs_tabs(df_list, sheet_list, file_name):
    writer = pd.ExcelWriter(file_name,engine='xlsxwriter')   
    for dataframe, sheet in zip(df_list, sheet_list):
        dataframe.to_excel(writer, sheet_name=sheet, startrow=0 , startcol=0)   
    writer.save()

# list of dataframes 
dfs = [df, df1, df2]

# list of sheet names
sheets = ['df','df1','df2']    

# run function
dfs_tabs(dfs, sheets, 'multi-test.xlsx')

An excel file with 'multi-test.xlsx will be saved locally带有'multi-test.xlsx test.xlsx”的 excel 文件将保存在本地

st.download_button(
     label="Download data as Excel",
     data='multi-test.xlsx',
     file_name='multi-test.xlsx',
     mime='text/xlsx',
 )

If the above method doesn't work, try this如果上面的方法不起作用,试试这个

data = pd.read_excel('multi-test.xlsx')
st.download_button(
     label="Download data as Excel",
     data=data,
     file_name='multi-test.xlsx',
     mime='text/xlsx',
 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM