You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have provided sufficient information below to help reproduce this issue.
Summary
st.download_button's documentation says that it returns True if the button was clicked on the last run of the app. But I noticed that it always returns False when the data passed in the parameter data changes in the next rerun.
The browser download popup shows up normally, but the return doesn't work as intended.
Below I provide two examples.
Dummy example just to show it doesn't work. I generate random strings and pass them to st.download_button.
Problematic example because apparently nothing changes. The bytes are the same every rerun. But since a different object is generated inside the function each rerun, Streamlit doesn't recognize the click. I faced this problem and it was hard to debug.
Reproducible Code Example
#### Example 1 ####importstreamlitasstfromrandomimportrandomrandom_str=str(random())
print(random_str)
clicked=st.download_button(
label='Download some text',
data=random_str
)
st.write(clicked)
print(clicked)
#### Example 2 ####fromioimportBytesIOimportstreamlitasstdefget_file_obj():
random_bytes=bytes("idfisudfiusdhf".encode('utf-8'))
file_obj=BytesIO(random_bytes)
returnfile_objclicked=st.download_button(
label='Download some text',
data=get_file_obj()
)
st.write(clicked)
print(clicked)
Steps To Reproduce
Use streamlit==1.26
Run examples 1 and 2 separately.
Expected Behavior
st.download_button return True when clicked, even if the data changes in the next rerun.
Current Behavior
Example 1 terminal output:
(env) [mateus@fedora download_test]$ streamlit run test.py
You can now view your Streamlit app in your browser.
Local URL: http://localhost:8501
Network URL: http://192.168.0.92:8501
0.39680310200698155
False
0.7461555731892388
False
0.5057767898133984
False
0.3176395196698075
False
0.5292348871076471
False
Example 2 terminal output:
(env) [mateus@fedora download_test]$ streamlit run test2.py
You can now view your Streamlit app in your browser.
Local URL: http://localhost:8501
Network URL: http://192.168.0.92:8501
False
False
False
False
False
Is this a regression?
Yes, this used to work in a previous version.
Debug info
Streamlit version: 1.26.0
Python version: 3.10.11
Operating System: Fedora 36
Browser: Firefox 112.0.1
Additional Information
No response
The text was updated successfully, but these errors were encountered:
Hi @kajarenc, thanks for taking a look at it. For those who want to stick with 1.26 while the fix is in progress, I was able to solve the problem using @st.cache_resource and the parameter on_click.
Checklist
Summary
st.download_button
's documentation says that it returns True if the button was clicked on the last run of the app. But I noticed that it always returns False when the data passed in the parameterdata
changes in the next rerun.The browser download popup shows up normally, but the return doesn't work as intended.
Below I provide two examples.
st.download_button
.Reproducible Code Example
Steps To Reproduce
Expected Behavior
st.download_button return True when clicked, even if the data changes in the next rerun.
Current Behavior
Example 1 terminal output:
Example 2 terminal output:
Is this a regression?
Debug info
Additional Information
No response
The text was updated successfully, but these errors were encountered: