Why download button reload page?

test code is:

import streamlit as st

with st.form("form"):

    submitted = st.form_submit_button("submit")

if submitted:
    st.write("this line should not disappear after click download")

    st.download_button(
        label="download",
        data="file",
        file_name='ok.txt',
    )

My real code will do some calculation and show the result (chart and tables) after click form_submit_button.

But if I click the download_button, the calculation result will disappear.

How to resolve this?

And a suggestion, data parameter could be a function name, which runs on click

1 Like

Hi @johnlyu, welcome to the Streamlit community!

The answer to your questions is that everything causes the Streamlit page to re-load, that’s how the execution model works:

If you want to maintain a specific result between runs, you can accomplish that by using st.experimental_memo or st.experimental_singleton (or the legacy version st.cache, but I wouldn’t recommend that for new code).

Best,
Randy

Hi @randyzwitch , I know I can save result using cache or state between rerun/sessions.

But my main question is why download_button reload the page, it shouldn’t.

any time something must be updated on the screen, Streamlit reruns your entire Python script from top to bottom

Download button is not a kind of control flow or will update something on the screen but just an output. For example, a chart created by st.altair_chart will provide a download button on the chart element and click it won’t reload the page.

Is there any way to disable the reload feature of download_button ?

2 Likes

I found it has been discussed in github

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