What I’m trying to do:
I basically have an app that displays a variable amount of images from the database in a gridpanel depending on how many images are stored, each image should have a clickable link that takes me to an editor page, where the selected image is shown in a bigger canvas. Is there an easy way to create the click action for a variable amount of links, with the attached image somehow being passed on to be able to display it in the canvas?
What I’ve tried and what’s not working:
I tried setting event handles for each link and make one function to be called when a link is clicked on but without success. I am relatively new to Anvil so there may be something that I am missing here. I am also unsure if this code really should be in the init function.
Code Sample:
class Image_Upload_Gallery(Image_Upload_GalleryTemplate):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
self.gridpanel_images.border = "2px solid"
links = []
def link_click(self, **event_args):
self.content_panel.clear()
self.content_panel.add_component(Segmentation_Alignment_Editor())
row = 1
btn_row = 0
col = 0
no_imgs = 0
stored_imgs = app_tables.images.search()
for i in stored_imgs:
img = Image(source=i['file'],horizontal_align="center",border="1px solid",tag='image{}'.format(no_imgs))
link = Link(tag='link{}'.format(no_imgs))
link.add_component(img)
links.append(link)
if col > 11:
col = 0
row += 1
btn_row += 1
continue
else:
self.gridpanel_images.add_component(link,row=row,col_xs=col,width_xs=1)
col += 1
no_imgs += 1
for a in links:
print(a)
a.set_event_handler('click',link_click())
Clone link: