1

When I run this code, I'm expecting the "running action" notification to not be shown until the action function is called, similar to how the "handling upload" notification is not shown until the handle_upload function is called. I'm not sure why it works as expected with handle_upload, but not with action. When I upload a file, I see both notifications, "running action" and "handling upload". When I click the "Action the Data" button, I don't see any notification. Appreciate any advice/suggestions for getting the expected behavior (and better understanding how NiceGUI works).

@ui.page("/submit")
def submit():
    def action_it(data: bytes) -> None:
        ui.notify("running action")

    def handle_upload(file: events.UploadEventArguments) -> None:
        ui.notify("handling upload")
        data = file.content.read()
        lines = [line for line in data.splitlines() if line.strip()]
        output.clear()
        with output:
            ui.label('Lines uploaded:')
            for line in lines:
                ui.label(line.decode())
    
    ui.button("Action the Data", on_click=action_it(data))
    ui.button("Cancel")

    ui.upload(on_upload=handle_upload, auto_upload=True)
    output = ui.column()
nopori
  • 76
  • 5
  • Are you sure in this line: `on_click=action_it(data)`? It seems like you are passing not a delegate but rather result of function execution (which in your case is None). – markalex Mar 28 '23 at 17:02
  • @markalex Ahh yes, you're right, I think that explains what's happening. Thank you for pointing that out. I will do some research on how/whether I can pass an argument to the function delegate and update my question – nopori Mar 28 '23 at 17:07
  • Just as a reference, here is a very similar discussion: https://github.com/zauberzeug/nicegui/issues/648 – Falko Mar 28 '23 at 20:43

1 Answers1

1

@markalex Pointed out that I wasn't passing a function delegate to on_click but was actually sending it the results of the function execution, and that is why I wasn't getting the behaviour I expected.

To get the behavior I wanted, I just needed to use a lambda function to call my function and pass it the argument:

ui.button("Action the Data", on_click=lambda: action_it(data))

nopori
  • 76
  • 5