Stop page from reloading after using downloader

Hi,

In my app a user uploads a file which is processed, the results of which are offered as a download.

Now when I press the download button on my webpage, it reloads and starts processing the uploaded file again. From what I gather this is the intended behaviour of Streamlit? Can I prevent this reloading somehow, though? Seems like caching is an option, but that requires decorating a function? And I’m not really sure it would really solve my problem anyway.

Basically what I want is the user to be able to upload a file and having to option to download the results of the processing. If the user then wants to upload a new file, do the processing on the new file and offer the results as a new download.

6 Likes

Hi Chiel, I ran into similar issue. Every time I click the download button, the whole app will refresh. May I know how you fixed it? Thanks!

Hello from 2022.

I have faced same problem and reached out this page now. Do we have a solution now?

How about this semi-solution heavily based on Creating a PDF file generator - #2 by ash2shukla

import base64
import time
import streamlit as st
import pandas as pd


def long_computation():
    time.sleep(15)
    return pd.Series(range(1000), name='number')


@st.cache
def convert_csv(data):
    return data.to_csv(index=False).encode('utf-8')


def create_download_link(val, filename):
    b64 = base64.b64encode(val)
    return f'<a href="data:application/octet-stream;base64,{b64.decode()}" download="{filename}.csv">Download file</a>'


st.write(' ... doing long computation ...')

res = long_computation()

st.success('Long computation done!')

csv = convert_csv(res)

download_url = create_download_link(csv, 'test')

st.markdown(download_url, unsafe_allow_html=True)

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