Use CherryPy add a web interface to a Python script

Moving On

I don't have room here to cover the long middle part of the code in detail, and, given what I've described so far, the functions should be fairly self-explanatory.

Briefly, lines 320-375 check for a winner at the end of the hand. Then, lines 388-417 cover the card class, which represents a playing card in the deck. Rather than an __init__ class, its variables are simply defined outside the scope of any functions. For example, suit is a single-letter designation of the card's suit and rank is a numerical designation of the card's value, with aces converted to 1, jacks to 11, and so on; value is its "face" value unconverted.

Next, the deck class (lines 419-447) represents a full deck of cards. Each set of lines inside the loop creates a new instance of the card class and adds it to its internal list as a specific card in the deck.

The checkHand class (lines 449-664) accepts as arguments a hand (list of cards) and a playerName. It evaluates the hand it is given and sets internal properties as to the type and rank of the win, cards used in the win, and cards left over (called kickers). In the next if/elif series, each of the internal functions is called to see what type of win the hand is (if any). Once a match is found, the winType and winRank are set appropriately.

Finally, after all of the definitions, it's time to start up the main classes that run everything else. Lines 668-672 get the IP address of the server. This is the same code as the joinTheGame function. On line 674, this address is passed to cherrypy.config.update to make the web service visible to the outside world. By default, CherryPy will only serve to localhost.

The next line starts CherryPy serving the web class in its own thread. In this way, the web server and Pygame tasks will run independently (although they can communicate with each other). Listing 1 shows the configuration information passed to CherryPy (see also Figure 6).

Listing 1

Information Passed to CherryPy

01 ( web ( tbl ) , '/' ,
02   { '/cards' :
03       { "tools.staticdir.on" : "True" , "    aticdir.dir" : "/home/pi/poker/Playing Cards/PNG-cards-1.3/" } ,
04     '/web' :
05       { "tools.staticdir.on" : "True" , "tools.stat    icdir.dir" : "/home/pi/poker/web/" }
06   }
07 )
Figure 6: The file structure and web and Playing Cards folders [5]. The folders are defined as static HTML sources to CherryPy.

In Listing 1, web ( tbl ) is instantiating the web class and passing a reference to tbl – the poker game. Additionally, '/' says to make the functions from the web class accessible at the root path. The next two dictionaries define static web directories: tools.staticdir.on is set to True, and tools.staticdir.dir is set to the absolute path of the files to serve.

Line 677 instantiates the gfx() class, which initializes Pygame. Then line 678 sets the screen property inside tbl, my instance of the table class, or the poker game itself. The last two lines create an infinite loop to keep the main process running. As calls come in from the web class, they will call functions in gfx and table that cause the screen to update. Once everything is initialized, however, all of the calls come from other classes.


When players join the game, they are redirected to index.html (available on the Raspberry Pi Geek FTP site). All further calls are performed in the background with Ajax. jQuery handles all of the Ajax functions and updating the player view on the phone or tablet.

An HTML document is divided into two sections – the <head> and the <body>. Code placed in the <head> is received by the browser but not displayed to the user directly. This is where JavaScript functions, CSS styles, page titles, and other configuration data for the page live. Code placed in the <body> section is rendered by the browser and creates the contents you see as your favorite web page. It references the CSS and other data sent in the <head> and can also call the JavaScript functions defined there. JavaScript can manipulate a page once it has arrived at your browser or even replace it completely.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content