One button to create and download a file

Hi there,

I would like to have a button that creates a file and then downloads that file in one click.

I couldn’t figure out how to do that, so right now I have two buttons to do this (one to create the file and one to download it). Can someone please explain how this can be done in one button?

This is my code with two buttons:

but_html= st.button('create html-file')   #first button

if but_html:
   with st.spinner('creating html-file...'):
      f_pn= 'export/interactive.html'     # the file that's going to be created      
      fuan.html_exp(f_pn)                 # function that creates the file
      with open(f_pn, 'rb') as exfile:
         st.download_button(              #second button
            label="download html-file",

It works, but I always have to tell the user, that they need to click the download button after creating the file… it’s not intuitive and confuses a lot of people. I’m sure there is a better way to do this…

1 Like

Maybe there is a way to use the on_click argument of st.download_button? I tried something similar but could not figure it out either, the documentation for the argument does not provide an example for it.

That’s what I thought as well… I’d need to define a function that starts the download and put that in the on_click. I just don’t know how to write that function…

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