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 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 can start creating presentation like this:

import ipyslides as isd 
slides = isd.Slides()

Enter fullscreen mode Exit fullscreen mode

%%slide 0 -m
# Title Markdown 

Enter fullscreen mode Exit fullscreen mode

%%slide 1 # Can also use with slides.build(1):or build(-1) syntax slides.write('# Slide Title')
slides.write('## Column 1',"## Column 2")

Enter fullscreen mode Exit fullscreen mode

Below slide will give same result as above:

%%slide 1 -m
# Slide Title ||## Column 1 || ## Column 2 || 

Enter fullscreen mode Exit fullscreen mode

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}$'))

Enter fullscreen mode Exit fullscreen mode

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()}')

Enter fullscreen mode Exit fullscreen mode

Show Slides

slides object at end of cell automatically displays itself, you can also do this explicitly:

slides.show()

Enter fullscreen mode Exit fullscreen mode

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.

原文链接:Create dynamic and content rich presentations in Jupyter

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容