Day 11 of the Anvil Advent Calendar

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

Veto Powers

It turns out that if you build an app for playing random Christmas songs, people within earshot rapidly lose the Christmas spirit. Especially in early November, which is when we have to actually create the Anvil Advent Calendar.

So we reached a compromise. I can play all the Christmas songs I like, but everyone else can veto any of the particularly objectionable choices. To make that possible, I built them a giant ‘Skip’ button.

I can’t imagine why people would demand a Skip button.

I can’t imagine why people would demand a Skip button.

Let’s see how it works!

Spotify Remote-Control

This app is almost as simple as the random song chooser, but this one remembers my Spotify credentials so that any visitor to the page sees my current playback status. Like the other app, this one uses my Spotify OAuth Helper app.

A server function uses the Spotipy library to talk to the Spotify API, and returns the currently-playing track if there is one. A second function uses the same library to skip when the button is clicked. And that’s it!

@anvil.server.callable
def get_current_track():
  spot = get_spotify()
  return spot.current_playback()

@anvil.server.callable
def skip():
  spot = get_spotify()
  spot.next_track()

I’m not going to link to my actual app here, because I don’t really want to give the whole Internet the power to skip everything I’m listening to, but you can check out the source code below and try it for yourself!


Give the Gift of Python

Share this post: