Hey all. I have this piece of code and

Hey all. I have this piece of code and can't quite seem to figure out why it's behaving the way it is.
import solara


def select_well(x: int, y: int):
print(x, y)


@solara.component
def Page():

with solara.Column(align="center", style={"width": "100%"}):
for y in range(2):
with solara.Row(gap="2px", margin="2px", justify="center"):
for x in range(2):
solara.Button(
f"{x}-{y}",
outlined=True,
on_click=lambda: select_well(x, y),
)
import solara


def select_well(x: int, y: int):
print(x, y)


@solara.component
def Page():

with solara.Column(align="center", style={"width": "100%"}):
for y in range(2):
with solara.Row(gap="2px", margin="2px", justify="center"):
for x in range(2):
solara.Button(
f"{x}-{y}",
outlined=True,
on_click=lambda: select_well(x, y),
)
Basically, I'm trying to create a grid where any button clicked calls a function with the associated grid postion. The problem is that when you click any button, it always uses the last x,y coordinates even though the UI seems to register the proper labels on each button
No description
2 Replies
mariobuikhuizen
mariobuikhuizen8mo ago
Because in python x and y have the last value of the loop when the lambda is called you can store the current values like this: on_click=lambda x=x,y=y: select_well(x, y)
Elder Millenial
Elder MillenialOP8mo ago
ARG, yeah, good call. I feel like a moron sometimes. Thanks.
Want results from more Discord servers?
Add your server