Separate Button Commands

Hey there,
I’ve currently hit an issue that I can’t seem to resolve.
My goal is to get a button, when clicked, to run a function with some variables. Here is the code:

      def get_sku(sku):
           print(sku)

      def window_setup(self):
           with ui.VStack(height=0, spacing=self.spacing):
               data = load_testing_data()
               for row in data:
                   with ui.HStack(spacing=self.spacing):
                       sku = row[0]
                       ui.Button(f"Apply", clicked_fn=lambda: get_sku(sku))

This creates multiple rows, each with a button that should have an SKU (my indentifier) for each button. This data is gathered through my database, though that shouldn’t be important to the issue.

My issue is: When I click any of the buttons, the function will always return the last rows SKU, not the button it should correspond with.

If anyone has any suggestions I’d love to hear them! Thank you!

Hi @coopgod

A possible solution is to pass the value to a lambda argument as follows:

clicked_fn=lambda value=sku: get_sku(value)

or instantiate a partial object/function with the argument

 import functools

 clicked_fn=functools.partial(get_sku, sku))
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.