使用Jupyter制作Slides

王成军

wangchengjun@nju.edu.cn

计算传播网 http://computational-communication.com

RISE: "Live" Reveal.js Jupyter/IPython Slideshow Extension

https://github.com/damianavila/RISE

Installation

from the RISE repository.

In the notebook toolbar, a new button ("Enter/Exit Live Reveal Slideshow") will be available.

The notebook toolbar also contains a "Cell Toolbar" dropdown menu that gives you access to metadata for each cell. If you select the Slideshow preset, you will see in the right corner of each cell a little box where you can select the cell type (similar as for the static reveal slides with nbconvert).

将ipynb文件转为slides.html

  • download the reveal.js from Github https://github.com/hakimel/reveal.js
  • generate html using the following code
  • put the generated html into the reveal.js folder
  • open the html using chrome
chengjuns-MacBook-Pro:~ chengjun$ cd github/cjc2016/code/

chengjuns-MacBook-Pro:code chengjun$ jupyter nbconvert slides.ipynb --to slides --post serve

批量生成slides.html¶

chengjuns-MacBook-Pro:~ chengjun$ cd github/cjc2016/code/

chengjuns-MacBook-Pro:code chengjun$ jupyter nbconvert *.ipynb --to slides

数学公式

$E = MC^2$

In [10]:
%%latex
\begin{align}
a = \frac{1}{2}\\
\end{align}
\begin{align} a = \frac{1}{2}\\ \end{align}

程序代码

In [1]:
print 'hello world'
hello world
In [2]:
for i in range(10):
    print i
0
1
2
3
4
5
6
7
8
9
In [ ]:
# get a list of all the available magics
In [21]:
% lsmagic
Out[21]:
Available line magics:
%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %install_default_config  %install_ext  %install_profiles  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %popd  %pprint  %precision  %profile  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%latex  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.
In [20]:
% env
# to list your environment variables.
Out[20]:
{'Apple_PubSub_Socket_Render': '/private/tmp/com.apple.launchd.Cti3IOL0XG/Render',
 'CLICOLOR': '1',
 'DISPLAY': '/private/tmp/com.apple.launchd.GQAU1RS6KM/org.macosforge.xquartz:0',
 'GIT_PAGER': 'cat',
 'HOME': '/Users/chengjun',
 'JPY_PARENT_PID': '84860',
 'LANG': 'en_US.UTF-8',
 'LC_ALL': 'en_US.UTF-8',
 'LC_CTYPE': 'UTF-8',
 'LOGNAME': 'chengjun',
 'PAGER': 'cat',
 'PATH': '/Users/chengjun/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/texbin',
 'PWD': '/Users/chengjun',
 'SECURITYSESSIONID': '186a5',
 'SHELL': '/bin/bash',
 'SHLVL': '2',
 'SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.VNCcz4m0az/Listeners',
 'TERM': 'xterm-color',
 'TERM_PROGRAM': 'Apple_Terminal',
 'TERM_PROGRAM_VERSION': '343',
 'TERM_SESSION_ID': 'FDFD985A-CDD6-415E-A3B0-E8A2A05CC9B4',
 'TMPDIR': '/var/folders/l6/ntr5b4610hx38gy0_2xp7ngh0000gn/T/',
 'USER': 'chengjun',
 'XPC_FLAGS': '0x0',
 'XPC_SERVICE_NAME': '0',
 '_': '/Users/chengjun/anaconda/python.app/Contents/MacOS/python',
 '__CF_USER_TEXT_ENCODING': '0x1F5:0x0:0x0'}
In [11]:
%prun
 
In [15]:
%time range(10)
CPU times: user 11 µs, sys: 9 µs, total: 20 µs
Wall time: 21 µs
Out[15]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [14]:
%timeit range(100)
The slowest run took 295.47 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 748 ns per loop

!: to run a shell command. E.g., ! pip freeze | grep pandas to see what version of pandas is installed.

In [17]:
! cd /Users/chengjun/github/
In [19]:
% matplotlib inline 
# to show matplotlib plots inline the notebook.
import matplotlib.pyplot as plt

plt.plot(range(10), range(10), 'r-o')
plt.show()

This is the End.

Thanks for your attention.