Skip to content

Cannot pass additional arguments to rx.upload on_drop handler #5290

@elliottsj

Description

@elliottsj

Describe the bug
I want to pass an additional argument to the on_drop handler so that I can determine which field is uploaded into:

on_drop=State.handle_upload(rx.upload_files(upload_id=str(field)), field),

However, an error occurs:

TypeError: State.handle_upload() missing 1 required positional argument: 'field'

To Reproduce
Steps to reproduce the behavior:

  • Code/Link to Repo:
class State(rx.State):
    fields: rx.Field[list[str]] = rx.field(["field1", "field2"])
    
    @rx.event
    async def handle_upload(self, files: list[rx.UploadFile], field: str) -> None:
        print("field", field)
        for file in files:
            print("file", file)

def index() -> rx.Component:
    return rx.foreach(
        State.fields,
        lambda field: rx.fragment(
            rx.text(field),
            rx.upload(
                rx.text(
                    "Drag and drop files here or click to select files"
                ),
                id=str(field),
                multiple=False,
                max_files=1,
                on_drop=State.handle_upload(rx.upload_files(upload_id=str(field)), field),
            )
        )
    )

Expected behavior
No error.

Screenshots

Image

Specifics (please complete the following information):

  • Python Version: 3.11.12
  • Reflex Version: 0.7.11
  • OS: macOS
  • Browser (Optional):

Additional context
Related discussion: https://github.com/orgs/reflex-dev/discussions/5266

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions