Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download_button always returns False when the data provided changes in each rerun #7308

Closed
3 of 4 tasks
mateusccoelho opened this issue Sep 11, 2023 · 3 comments · Fixed by #7316
Closed
3 of 4 tasks
Assignees

Comments

@mateusccoelho
Copy link

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • 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.

  1. Dummy example just to show it doesn't work. I generate random strings and pass them to st.download_button.
  2. 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 ####

import streamlit as st
from random import random

random_str = str(random())
print(random_str)

clicked = st.download_button(
    label='Download some text', 
    data=random_str
)
st.write(clicked)
print(clicked)

#### Example 2 ####

from io import BytesIO
import streamlit as st

def get_file_obj():
    random_bytes = bytes("idfisudfiusdhf".encode('utf-8'))
    file_obj = BytesIO(random_bytes)
    return file_obj

clicked = st.download_button(
    label='Download some text', 
    data=get_file_obj()
)
st.write(clicked)
print(clicked)

Steps To Reproduce

  1. Use streamlit==1.26
  2. 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

@mateusccoelho mateusccoelho added status:needs-triage Has not been triaged by the Streamlit team type:bug Something isn't working labels Sep 11, 2023
@github-actions
Copy link

If this issue affects you, please react with a 👍 (thumbs up emoji) to the initial post.

Your feedback helps us prioritize which bugs to investigate and address first.

Visits

@kajarenc
Copy link
Collaborator

kajarenc commented Sep 12, 2023

Hello @mateusccoelho and thank you for opening this issue.
I was able to reproduce the issue here and can confirm that this is a bug.

Looks like this is a regression in 1.26

We are working on fixing this issue

@kajarenc kajarenc added priority:P2 and removed status:needs-triage Has not been triaged by the Streamlit team labels Sep 12, 2023
@mateusccoelho
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants