Function is returning "ClickEventArguments" instead of variable contents

79 Views Asked by At

Function 'handle_upload' is not returning variable (which is excel file that has been read by pd.read_excel) , instead it's returning "ClickEventArguments" object

 def handle_upload(e: events.UploadEventArguments):
            file_content = e.content.read()
            file_xlsx = pd.read_excel(file_content)
            return file_xlsx
        
 def start_campaign(file_xlsx):
           pd.set_option("display.max_rows", None)
           pd.set_option("display.max_colwidth", None)
            
           for column_name in file_xlsx.columns:
               if "Email" in column_name:
                   for email in file_xlsx[column_name]:
                       generate_email_campaign(receiver=email,)
            
    with ui.card().classes("absolute-center").style("margin: 0px 0px 0px 15px; border-radius: 8px; box-shadow: rgba(149, 157, 165, 0.2) 0px 8px 24px; "):
          
            with ui.stepper().props('vertical').classes('w-full') as stepper:
                # with ui.step('Product Details'):
                #     prompt = ui.input('Tell us about your product', on_change=lambda e: e.value)
                #     with ui.stepper_navigation():
                #         ui.button('Next', on_click=stepper.next)
                with ui.step('Upload Data'):
                    ui.label("Upload a valid Excel file (xlsx) to begin your campaign.")
                    ui.upload(on_upload=handle_upload, multiple=False).props('accept=.xlsx')
                    with ui.stepper_navigation():
                        ui.button('Start', on_click=start_campaign)
                        ui.button('Back', on_click=stepper.previous).props('flat')

ui.run()

When 'start_campaign' is passed to on_click button, the read excel is supposed to be passed into the function 'start_campaign' for further processing and execution.

1

There are 1 best solutions below

0
Falko On

The problem is probably that the on_click event handler automatically passes event arguments to the callback, start_campaign in this case, if it expects an argument. Therefore the click event arguments are passed to start_campaign, which expects file_xlsx.

To fix it, the button should look something like this:

ui.button('Start', on_click=lambda: start_campaign(file_xlsx))