Create dynamic and content rich presentations in Jupyter
Abdul Saboor
Posted on August 22, 2021
Jupyter is an effective tool for data analysis whether it is classic notebook, lab, or notebooks in popular text editors like VS code. You do analysis but when it comes to presenting your results, most of the time you need to move out of the ecosystem. Currently there are many tools to present your analysis to non-technical people without showing code cells including voila, reveal slides etc. These tools present either static html or slides of plain cell outputs, so you do not have a fine grain control over content. Facing all such difficulties, I decided to leverage the IPython's rich content capabilities for creating presentation without leaving notebook. The resultant package ipyslides is in active development and can use every kind of content from widgets, audio, video, HTML etc. Without more intro, let's dig into code a little bit.
Install
The most preferred environment is jupyterlab, so after having that installed, you can do
pip install ipyslides
Usage
You can start creating presentation like this:
import ipyslides as isd
slides = isd.Slides()
%%slide 0 -m
# Title Markdown
%%slide 1 # Can also use with slides.build(1):or build(-1) syntax
slides.write('# Slide Title')
slides.write('## Column 1',"## Column 2")
Below slide will give same result as above:
%%slide 1 -m
# Slide Title
||## Column 1 || ## Column 2 ||
Both %%slide
and with Slides.build
save results to IPython's capture mechanism. There is another way where you can add slide frames:
%%slide -1
for obj in Slides.fsep.loop([1,2,3]):
slides.write(f'## Dynamic Slide ${obj}^2 = {obj**2}$'))
This will create three frames.
Now let's create multiple slides from single cell using context manager:
import matplotlib.pyplot as plt, numpy as np
for i in range(5):
with slides.build(i+5) as s:
x = np.linspace(0,i+1,50+10*i)
_ = plt.plot(x,np.sin(x))
slides.write(slides.plt2html(),f'#### Slide {i+5} but I am {i+1} of 5 other slides created from single cell\n{s.get_source()}')
Show Slides
slides
object at end of cell automatically displays itself, you can also do this explicitly:
slides.show()
You have write
command to write Markdown, HTML and plots after using plt2html
and plotly2html
. You can extend to other plotting libraries, or you can simply use native commands like plt.show
, fig.show
etc.
You can see comprehensive demo at Binder where rich content like YouTube video, tables, graphs, widgets are embedded.
Posted on August 22, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.