Day 1 of the Anvil Advent Calendar

Build a web app every day until Christmas, with nothing but Python!

Make a wish

Every year, during this season, family and friends ask me the same dreaded question: what do you want for Christmas? I don’t know about you, but at that moment, every single thing I’ve been wanting to buy dissipates from my mind.

This is where today’s app comes in handy. You can log in and add gift ideas to it throughout the year, so when Christmas comes around you can just send a link to it to the people who will be getting you gifts. There’s also an option for each person to claim the gift they want to get you, so you won’t find yourself in a situation where everyone has bought you that same sweater you wanted. Check it out below, and keep reading to see how it works.

https://simple-wishlist.anvil.app

How it works

This is a simple CRUD app (that is, Create/Read/Update/Delete), which you can read more about in our CRUD tutorial. To this, I added a couple of extra features.

Logging In

I want to be the only one that is capable of adding and removing gifts from the list, so I used Anvil’s Users service to set up a way to log in and make these features available. I added a button, which triggers a Login form when clicked.

def login_button_click(self, **event_args):
    self.user = anvil.users.login_with_form()
    self.refresh_data_bindings()
    self.display_gifts()

Then, I modified the functions that should only be accessible when I’m logged in on the server side, like the one below.

# Add an argument to the decorator to restrict its usage
@anvil.server.callable(require_user=True)
def add_gift(gift_dict):
    app_tables.gifts.add_row(**gift_dict)

Finally, I added data bindings to the buttons from the Design page, to make them visible only when I have logged in.

Now, I’m the only one who can add gifts! I repeated the same process on my edit and delete functions. Here’s how the app looks when I’m logged in:

Claiming gifts

I want my friends to be able to claim a gift, so that other people won’t buy me the same thing, so each gift component has a claim_button. When clicked, this displays an alert so that someone can claim the gift they want to buy me:

def claim_button_click(self, **event_args):
    user = {}
    if alert(content=Claim(item=user), large=True, buttons=[('Claim', True), ('Cancel', False)]):
        anvil.server.call('claim_gift', self.item, user)
        self.refresh_data_bindings()

The names are then stored into the data table where the rest of the gift information is stored. Once someone has claimed a gift, the button is disabled and the text is changed.

And with this, we’re done with our wishlist app! Check out the full source code by cloning the app:


Give the Gift of Python

Share this post: