Day 4 of the Anvil Advent Calendar

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

Which adaptation of A Christmas Carol is the best?

Nothing says “Christmas” more than Charles Dickens’s classic A Christmas Carol and a good old fashioned competition.

A Christmas Carol has been adapted into more stage productions, film adaptations and television specials than can be counted. It has even been adapted into operas, ballets, comics and graphic novels. This leads to a Very Important Question, which adaptation is the best?

There is, of course, a correct answer to this question, but for the sake of fairness, we built a web app so that you can vote for your favorite! Click on the link below and select the best adaptation.

https://christmas-carol-showdown.anvil.app

Charles Dickens wrote A Christmas Carol because he wanted society to care more for the poor, especially poor children. His story was an immediate success and has had a lasting influence on Christmas traditions to this day. A Christmas Carol has been credited with reviving enthusiasm for Christmas in Britain and with inspiring the sense of charity and goodwill that is now intertwined with the Christmas spirit. Even though it’s not worth arguing about the impact of Dickens’s story, it’s certainly worth arguing about which adaptation is the best!

Here’s how I built the app to do just that:

Creating a Data Table

After doing some research on the many, many (many) adaptations of A Christmas Carol, I stored a narrowed-down list in a Data Table. The Table stores the title of each adaptation, an image from the adaptation, a short description of the adaptation and the number of votes it has received.

Designing the UI

I then created my User Interface, which includes a DropDown menu for choosing an adaptation, a Label to display the description stored in the Data Table and an Image to display the image in the Data Table.

I also included a submit that is only visible when an option is selected from the DropDown. When a vote is submitted, the votes column of the Data Table is updated in a transaction.

@anvil.server.callable
@anvil.tables.in_transaction
def add_vote(movie):
    movie['votes'] = (movie['votes'] or 0) + 1

Displaying the live results

The app also displays live voting results using Plotly. I wrote a function to display a bar chart of the current results.

def refresh_votes(self):
    movies = app_tables.movies.search()
    self.plot_1.data = [go.Bar(x=[m['title'] for m in movies], 
                                y=[m['votes'] for m in movies], 
                                marker=dict(color="#116639"))]

refresh_votes gets called when the webpage loads and when you submit a vote. The function also updates the results every 5 seconds using a Timer.

  def timer_1_tick(self, **event_args):
    """This method is called Every [interval] seconds. Does not trigger if [interval] is 0."""
    with anvil.server.no_loading_indicator: #suppress the loading spinner
     self.refresh_votes()

You can check out the source code for the app using the clone link below:

We’ll tweet out the results of the competition on Christmas Eve, so make sure to submit your vote by then! May the best A (Muppet) Christmas Carol win!


Give the Gift of Python

Share this post: