使用Jupyter制作Slides的介绍#

王成军

wangchengjun@nju.edu.cn

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

使用Nbviewer打开Slides#

RISE: “Live” Reveal.js Jupyter/IPython Slideshow Extension#

damianavila/RISE

Installation#

  • Downnload from damianavila/RISE

  • open your teminal, cd to the RISE folder, e.g.,

  • To install this nbextension, simply run

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).

cd /github/RISE/#

python setup.py install#

将ipynb文件转为slides.html#

  • download the reveal.js from Github 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/cjc/code/

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

批量生成slides.html¶#

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

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

数学公式#

\(E = MC^2\)

%%latex
\begin{align}
a = \frac{1}{2}\\
\end{align}
\[\begin{split}\begin{align} a = \frac{1}{2}\\ \end{align}\end{split}\]

程序代码#

print 'hello world'
hello world
for i in range(10):
    print i
0
1
2
3
4
5
6
7
8
9
# get a list of all the available magics
% lsmagic
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.
% env
# to list your environment variables.
{'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'}
%prun
 
%time range(10)
CPU times: user 11 µs, sys: 9 µs, total: 20 µs
Wall time: 21 µs
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
%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.

! cd /Users/chengjun/github/
% matplotlib inline 
# to show matplotlib plots inline the notebook.
import matplotlib.pyplot as plt

plt.plot(range(10), range(10), 'r-o')
plt.show()
_images/39535419c8ce97bd8b70623b405955227e51ac3f46a606b69a25f0684004c43c.png