View on GitHub

Adding animated tables to IPython presentations


Available as a github gist

A couple weeks ago, I made a small class to create animated build figures for IPython presentations. This a short addendum to that post, this time with animated tables instead of figures. These draw on the formatted IPython tables that I posted yesterday.

The idea here is same with the figures: often when you are presenting a figure or table, using animation to slowly build it up helps you tell the story in a more organized way. Powerpoint is full of options to do this, but I would like to do it programatically in Python.

The goal of the animated tables is to make a widget that takes in a formatted table and a list of functions to apply to the table With each click, it applies the next function in succession. This is best illustrated with a short example:

In [6]:
import pandas as pd
from ipywidgets import *

# Create a data frame that we will make into a table
df = pd.DataFrame({"x":[1,2,3], "y": [9,8,7], "x": [0.1,0.2,0.3]})
df.index = ["row1", "row2", "row3"]

# Make a PrettyTable out of it
pt = PrettyTable(df, tstyle=TableStyle(theme="theme1"), center=True, header_col=True, header_row=True)

#### Functions for animation ###
# Highlight second row
def f1(pt):
    pt.update_cell_style(rows=[1], background_color="yellow", font_weight="bold")

# Make the font in the top right corner red
def f2(pt):
    pt.update_cell_style(rows=[0], cols=[1], color="red")
# Make a ststic build table
# pass it the PrettyTable, and the list of functions to apply
StaticBuildTable(pt, [f1, f2])

Try clicking on the above table to walk through the animation. You can also use "a" and "r" to "(a)dvance" and "(r)everse" the animation.

You can get these in my forked ipywidgets repository. You can see more about fun resources for IPython presentations here. Finally, to see how this works, you can check out my post on how to do this for figures.

comments powered by Disqus