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

[REF-2157] rx.download doesn't work with rx.get_upload_url from a backend event handler #2812

Open
masenf opened this issue Mar 8, 2024 · 2 comments · Fixed by #2813 · May be fixed by #4470
Open

[REF-2157] rx.download doesn't work with rx.get_upload_url from a backend event handler #2812

masenf opened this issue Mar 8, 2024 · 2 comments · Fixed by #2813 · May be fixed by #4470

Comments

@masenf
Copy link
Collaborator

masenf commented Mar 8, 2024

Describe the bug
I want to write a file to a path obtained via rx.get_upload_dir() and then cause the user to download it via rx.download, but it doesn't work.

To Reproduce

import reflex as rx

class State(rx.State):
    button_text: str = "Download"
    def do_download(self):
        filename = "foo.txt"
        (rx.get_upload_dir() / filename).write_text("test")
        return rx.download(rx.get_upload_url(filename))

@rx.page()
def index():
    return rx.button(State.button_text, on_click=State.do_download)

app = rx.App()

Expected behavior
Download the file that we write in the event handler

Instead, exception

Traceback (most recent call last):
  File "/Users/masenf/code/reflex-dev/reflex/reflex/state.py", line 1439, in _process_event
    events = fn(**payload)
             ^^^^^^^^^^^^^
  File "/Users/masenf/code/reflex-dev/repro-download-from-upload-dir/repro_download_from_upload_dir/repro_download_from_upload_dir.py", line 8, in do_download
    return rx.download(rx.get_upload_url(filename))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/masenf/code/reflex-dev/reflex/reflex/event.py", line 589, in download
    raise ValueError("The URL argument should start with a /")
ValueError: The URL argument should start with a /

Specifics (please complete the following information):

  • Python Version: 3.11.4
  • Reflex Version: 0.4.4a1
  • OS: macOS
  • Browser (Optional):

Additional context
Add any other context about the problem here.

REF-2157

@masenf masenf changed the title rx.download doesn't work with rx.get_upload_url from a backend event handler [REF-2157] rx.download doesn't work with rx.get_upload_url from a backend event handler Mar 8, 2024
masenf added a commit that referenced this issue Mar 8, 2024
A common use case is processing some data, writing it to the
`rx.get_upload_dir`, then triggering a download of the processed data.

Using the `data` arg for `rx.download` works up to the size limitation of the
browser data URI handling mechanism, but offering a real download can
accomodate much larger data using the standard mechanism.

Fix #2812
@goncalog
Copy link

goncalog commented Dec 3, 2024

hello, is it possible that this is no longer working with more recent versions? i can download a file in "/assets" but not in "/uploaded_files" using a very similar code to what you have in the bug description. thank you

@masenf
Copy link
Collaborator Author

masenf commented Dec 3, 2024

hello, is it possible that this is no longer working with more recent versions?

yes it appears to be broken in 0.6.0+

@masenf masenf reopened this Dec 3, 2024
masenf added a commit that referenced this issue Dec 3, 2024
Update the special case in a way that works with the new Var system and its
unstoppable determination to concatenate strings instead of using template
string

This has been broken since 0.6.0, so a regression test was added to avoid
future inadvertant breakage.

Fix #2812 (again)
masenf added a commit that referenced this issue Dec 3, 2024
Update the special case in a way that works with the new Var system and its
unstoppable determination to concatenate strings instead of using template
string

This has been broken since 0.6.0, so a regression test was added to avoid
future inadvertant breakage.

Fix #2812 (again)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants