DEV Community

Cover image for Create dynamic and content rich presentations in Jupyter
Abdul Saboor
Abdul Saboor

Posted on

Create dynamic and content rich presentations in Jupyter

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 almost 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
Enter fullscreen mode Exit fullscreen mode

Usage

You have multiple ways to create slides, but there is a shortcut way that fills out code in cells with less effort. You can start creating presentation like this:

import ipyslides as isd 
isd.initilize()
Enter fullscreen mode Exit fullscreen mode

Running above cell will push code inside same cell, you will get
plenty of code in the cell, but I will show few lines:

from ipyslides import load_magics, convert2slides, write_title
from ipyslides.utils import write, plt2html, print_context, slide

# Command below registers all the ipyslides magics that are used in this file
load_magics()
# Set this to True for Slides output
convert2slides(False) #Set this to True for Slides output
write_title("# Title Markdown")
Enter fullscreen mode Exit fullscreen mode

After running above cell, your slides' environment is almost set. You can start building slides with usual python code with just one extra line in cell.

%%slide 1
write('# Slide Title')
write('## Column 1',"## Column 2")
Enter fullscreen mode Exit fullscreen mode

Both %%slide and with side save results to IPython's capture mechanism. There is another way where you can add dymanic slides with helper function:

isd.insert_after(1, 1,2,3,func=lambda x: write(f'## Dynamic Slide ${x}^2 = {x**2}$'))
Enter fullscreen mode Exit fullscreen mode

This will create three slides after slide 1.

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 slide(i+5):
        x = np.linspace(0,i+1,50+10*i)
        _ = plt.plot(x,np.sin(x))
        write(plt2html(),f'#### Slide {i+5} but I am {i+1} of 5 other slides created from single cell\n{isd.get_cell_code()}')
Enter fullscreen mode Exit fullscreen mode

Build Slides

After you have fee slides, you can run command isd.build() which will inform you to turn on convert2slides(True) in first cell and when you do that, executing cell will populate the code or you can write code yourself without using isd.build command.

# Only this cell should show output. For JupyterLab >=3, pip install sidecar for fullscreen access
# You can also double click on output and select `Create New View for Output` that will let you enable fullscreen.
# ------ Slides End Here -------- 
from ipyslides.core import  LiveSlides 
ls = LiveSlides()
ls.set_footer('<span style="color:green">Author: Abdul Saboor')
ls.show()
Enter fullscreen mode Exit fullscreen mode

This last cell will generate slides, all slides should be above this cell. Now if you are in jupyterlab, you can use create new view for output form menu or if you have sidecar installed, presentation will automatically pop up there from where you can make it fullscreen from left setting panel.
image

Slides
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 comprehesive examples of slides at Kaggle and Binder where rich content like youtube video, tables, graphs, widgets are embeded.

Discussion (0)