How can I upload an excel file in the background and then send reports generated from it to a client?

Hi, I am trying to generate a dashboard from an excel file that the user uploads, however, I want to upload the file in the background so the client to which I sent the dashboard, only sees the generated report and not the uploaded file. Is there a way to do this?

Do you have a simple example of what you’re trying to achieve?

Generally speaking, when a file is uploaded through a streamlit app, it won’t be visible (its content) unless you explicitly display it.

So I’m afraid it’s not quite clear what problem you are having, or trying to solve.

Tell us more!

1 Like

Yes, @madflier please find the git hub repo here,
here’s the screenshot of what I am getting :

I do not want this whole “choose a file” block to be shown in the app, to the client. I could upload the file directly to the github repo, but only I have access to that, I want to create a data app, that will take data from a user and generate a dashboard to be sent to the client without my intervention.

Why not have two separate scripts - one allows the file to be uploaded, the other is your dashboard but without the file upload function. Once the file is uploaded, store it locally on the server to be accessed by the dashboard app.

Either run them on different ports, or use a select box or something like the st.experimental_get_query_params to show a different interface based on the URL.

Edited to add:
Or perhaps st.beta_expander would suffice.

Just looking at your code be a little careful with the way you’re using cache - if you find the dashboard seems to be referencing an old version of the xls file, you might need to try removing the cache decorator.

1 Like

can you show me an example of how to save the uploaded file on the server for the dashboard app to access?

To save the file, just something basic like this:

import streamlit as st

#Get uploaded file
temp_file = st.file_uploader("Enter file here!")

#Read the contents into a bytes object
if temp_file: temp_file_contents = temp_file.read()

#Write back out to disk (button to confirm)
if st.button("Save as working file"):
    with open("ON_DISK_FILE.extension","wb") as file_handle:
        file_handle.write(temp_file_contents)

Once it’s on disk, you should be able to read it directly with the same pandas excel read function that you are already using - just pass in the filename, eg pd.read_excel("ON_DISK_FILE.extension").

Obviously you can use different filenames, and you can add error checking in case the file doesn’t exist etc.

1 Like

Note, there is actually a bug in this code(! :man_facepalming:!) - there will be an error in the app until you have selected a file.
If you want to just have a minimal working version, the code below is a terse functioning version of the same thing.

import streamlit as st
temp_file = st.file_uploader("Enter file here!")
if st.button("Save as working file"):
    with open("ON_DISK_FILE.extension","wb") as file_handle:
        file_handle.write(temp_file.read())

Got it, thank you so much @madflier !!!

Obviously bear in mind that you need to be careful about the security of a script that allows anyone to upload arbitrary files onto your streamlit server. You may want to consider some form of access control or just run the upload script only very briefly when you wish to change the backing data.

Yes, I have been seeing it, I couldn’t understand the error, thanks again I’ll use the basic version.

I think the latter one works because I only need to upload data once a month.

1 Like