Standalone Python Tutor

HTML slide shows from view creation


SPT is a variation of the Online Python Tutor project. Both projects execute a Python program internally to create a set of images (think of a slide show) that allow you to monitor the execution of the program. Online Python Tutor tracks the building of Python internal data for each Python instruction. SPT uses a "snapshot" approach that builds each visualization on request. There are advantages to each.

With Online Python Tutor you see your program running with something like a microscope. The evolving internal data structures and stack frames are shown with each instruction, though this can be modified. This is very valuable for beginning programmers where there is often some confusion about exactly what the code is doing. It is especially enlightening when used to illustrate a recursive function call.

With Standalone Python Tutor you control when and what is shown with each slide. The slides can be tailored to display appropriate information at critical moments in the execution of the algorithm. SPT comes with View and Matrix classes that make this quite easy to do.

Online Python Tutor (OPT) works with small programs that you can load into a web text area which is then run by OPT remotely. A "Slide show" returned to you online. Because of this your program cannot import other modules that are not part of the remote system.

Standalone Python Tutor (SPT) can be used for a program of any size. The building of the slide show happens locally creating a standalone html file containing all parts necessary. The HTML file may be imported to a browser via a web link, as an attachment to an email or simply as a local file. SPT is very helpful for visualizing more complex algorithms such as those from dynamic programming and even LALR1 bottom up compilation.

Like OPT, SPT executes your program to produce the slide show. But because this happens on your local computer, the program may also import other modules, read and write data files and even use command line arguments.

A Simple Example

The easiest way to show this is with an example. Look at the following screenshot.


To get this screen shot I used sptBuild to build an HTML file.

> sptBuild    # produces demo.html in same directory
> firefox demo.html   # First slide pops up. Click <forward> to proceed

The file demo.html is produced by sptBuild executing In turn uses a matrix and a view object to create a very simple slide show. We will look at this code in small pieces.

Walking through an demo

Lets see how these pieces come together.

When we do an import spt there are two object classes defined, View and Matrix. An instance of View generates slides with each call to its snapshot method. The View instance may also contain attributes that are matched to labels in an HTML template string. The standard Python "%(label)s" in the template converts the label attribute of a view to a string and places it at this point in the HTML output for the slide. The following code sets up a tiny template, creates a View instance and attaches an attribute banner with its place in the template.

import spt

template = """
  <h2>This is a simple visualization</h2>

view = spt.View(template)
view.banner = "Simple Demo of SPT"

The other class is Matrix. An instance of Matrix contains a 2 dimensional array that will map to an HTML table in slides. A Matrix instance is created by supplying the initial number of rows and columns and setting some attributes for display. For example

numbers = spt.Matrix(1,4)
numbers.tableHeaders=["Col 0","Col 1","Col 2","Col 3"]
numbers.tableAttr = "border=1 cellpadding=10"

So numbers is an instance of Matrix with one row and four columns. Cells in the matrix instance are addressed by "[row,col]" and cells may contain anything that can be converted to a string. This can even include another matrix! Each matrix cell also has two companion cells. The style cell sets things like background color, fonts etc. using normal CSS syntax. The format cell supplies either a formating string beginning with the % character or a function taking one argument which converts a cell value to a form beyond the capabilities of Python string formatting. Roman numerals, perhaps.

colors = ['red','green','yellow','lightblue']
for i in range(4) :
    numbers[0,i] = i
    numbers.format[0,i] = "%03d"[0,i] = 'background-color:%s' % colors[i]

A default cell formatting and styling can be set at the matrix level as follows. These will apply to all cells that are not specifically overridden.

numbers.dftStyle  = "background-color:grey"
numbers.dftFormat = "%2s"

Installing SPT on your Computer

Download the zip file and copy it to a directory where you would like to place the installation. We'll refer to this directory as yourpath. From a shell issue the following.

> cd yourpath
> unzip
> [sudo] spt/ [altDirectory]

The last step creates a shell script sptBuild, either in /usr/bin/ or in an alternative directory probably on your executable path. It's assumed that /usr/bin is in everyones path for executables. It's also assumed that python will invoke the python interpretor from wherever it is located. You may have to use sudo to get root permission to build a file in /usr/bin.

Creating the Slide Show (HTML)

After installing SPT from the instructions above, go to any working directory and copy yourpath/spt/sample/ into it. Issue the following command.

> sptBuild

This will produce a demo.html in the same directory. If you have firefox you may now see the annimation simply by

> firefox demo.html

Or simply load the file to any browser using open file or a localhost address.

Other Sample Programs

Some other programs in the sample/ directory illustrate features of the View and Matrix classes. The best to start with is which generates the fibonacci sequence. Less than 20 lines of code, it is not really any more complex than

Next, generates prime numbers using the classic "Sieve of Erastosthenes" algorithm.

Then there are two more complex examples using Dynamic Programming. Code for Minimum Path and code for the Knapsack Problem are also in the sample/ directory. However if you are unfamiliar with Dynamic Programming you can see these examples explained in detail and then return here for a look at the python code.