Day 5 of Hanukkah at Anvil
Build a web app for each night of Hanukkah, with nothing but Python!
Today’s app is all about using the power of Data Tables. Trying to find the perfect menorah, but can’t decide on one? Why not let my app choose for you!
https://find-my-perfect-menorah.anvil.app
A quick and easy front-end
I started out with the default “Material Design” style and added an image, a couple of labels, a link and a button to click. We only need one method in our Form - one that deals with what happens when the button is clicked.
Storage to your heart’s content
The different menorah options are stored in a custom Data Table, which lets us keep their images, descriptions, and links to how to make them conveniently together. I created text columns for name
, description
and link
and a Media column to store the pictures of each menorah.
The code to grab a randomly chosen menorah row from this table couldn’t be simpler - we just use Python’s random.choice
to display a new menorah each time the button is clicked:
def button_1_click(self, **event_args):
"""This method is called when the button is clicked"""
self.button_1.text = "Another!"
self.column_panel_1.visible = True
menorahs = [m for m in app_tables.menorahs.search()]
chosen = choice(menorahs)
self.menorah_image.source = chosen['image']
self.menorah_description.text = chosen['description']
self.menorah_name.text = chosen['name']
self.more_link.url = chosen['link']
All we have to do is grant our Form permission to search the data table, and we’re good to go!
Give the Gift of Python
Share this post:
Get tomorrow's app in your inbox
Don't miss a day! We'll mail you a new web app every night of Hanukkah: