esc
or O
to Overview modeF
to Fullscreen modeS
to Speaker view©amlight-qq
variable="'$1'"
the_script='tell application "terminal" to do script "jupyter notebook '
osascript -e "${the_script}${variable}\""
!pwd
/Users/light/work/8-teaching&learning/0-preparing/6-topics/0-jupyter
在jupyter中就能实现快捷文档功能,使用方法就是在你想要查看的属性或方法之前加一个?,例如我们要查看python中的str()方法,就可以执行下图中的操作,jupyter会自动弹出一个窗口返回相关信息。
?str
a=1
a #with output
a; #without output
%lsmagic
显示所有magic命令%run x.ipynb
this will execute and show the output from all code cells of the specified notebook%load x.py
load other python file%pycat test.py
read file%%time
running time%%writefile test.py
write to file%%prun
代码的执行性能分析,可以作为行命令和单元命令使用%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 %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 %%js %%latex %%markdown %%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.
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
or
%config InlineBackend.figure_format = 'retina'
plt.savefig('tmp.pdf', bbox_inches='tight')
plt.show()
import os
from IPython.display import display, Image, HTML
#display()
Image("./cov.jpg",width=100)
a = "./cov.jpg"
report_html = ''
report_html = report_html + '<img src=' + '"img/jupyter/cov.jpg"'+ ' style="width: 100px" />'
report_html
display(HTML(report_html))
display(HTML("<table><tr><td><img src='img/jupyter/cov.jpg' style='width: 100px;'/></td><td><img src='img/jupyter/cov.jpg' style='width: 100px;'/></td></tr></table>"))
使用 Makedown table
A | B |
---|---|
jupyter中有一个很好用的功能,无需print语句,输出单元格的最后一行的结果。这一功能可以进一步修改为显示所有单行的变量
a,b=1,2
a
b
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity='last_expr'
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity='all'
from ipywidgets import interact
import ipywidgets as widgets
def f(x):
return x
interact(f, x=True);
interact(f, x='Hi there!');
widgets.ColorPicker(
concise=False,
description='Pick a color',
value='blue',
disabled=False
)
tab_contents = ['P0', 'P1', 'P2', 'P3', 'P4']
children = [widgets.Text(description=name) for name in tab_contents]
tab = widgets.Tab()
tab.children = children
for i in range(len(children)):
tab.set_title(i, str(i))
tab
The detail can be found at https://github.com/dunovank/jupyter-themes
安装过程中可能的一些问题: https://www.jianshu.com/p/83e397970f73
$ conda install -c conda-forge jupyterthemes
!jt -l
查看 Available Themes: "chesterish, grade3, gruvboxd, gruvboxl, monokai, oceans16, onedork, solarizedd, solarizedl"。具体效果有人做了比较:
https://blog.csdn.net/Techmonster/article/details/73382535
$jt -t onedork
$jt -t onedork -N -T
在View中点击Toggle Toolbar$jt -r
官方网址:https://nbconvert.readthedocs.io/en/latest/usage.html
nbconvert 可以用以下命令将nb转化为其他格式文件,其中 FORMAT
可以为: HTML, LaTeX, PDF, Reveal.js HTML slideshow
, Markdown, Ascii, reStructuredText, executable script, notebook.
$ jupyter nbconvert --to FORMAT notebook.ipynb
以下我们只重点提及 Reveal.js HTML slideshow
。值得一提的是,不同格式的文档可以选择转换的格式,例如,LaTeX可以选择article或者report,notebook可以选择执行文档中的code再另存为。
config
可查看link
theme
可查看link包括:beige, black, blood, league, moon, night, serif, simple, sky, solarized, white。
在notebook中选择 View-->Cell Toolbar-->Slideshow
,这样每个cell的右上角都可以选择类型。
s
键进入 presenter 的模式时会显示具体效果可以访问:https://revealjs.com/#/ 。完成后,在Terminal中执行以下代码可以将nb文件,比如我这里的techs.ipynb,转化为xxx.slides.html文件,可用各类web浏览器演示。
$ jupyter -nbconvert --to slides techs.ipynb --reveal-prefix 'https://cdn.bootcss.com/reveal.js/3.5.0' --output xxx
如果不想要input cell显示在slide中,可以执行:
$ jupyter nbconvert no.ipynb --to slides --TemplateExporter.exclude_input=True --reveal-prefix 'https://cdn.bootcss.com/reveal.js/3.5.0' --output xxx
这里面 --to slides
表示转化为slides。官网指出 “By default, this will include a script tag in the html that will directly load reveal.js from a public CDN. This means that if you include your slides on a webpage, they should work as expected. However, some features (specifically, speaker notes & timers) will not work on website because they require access to a local copy of reveal.js.”
--output xxx
为输出文件名称。--reveal-prefix 'server name'
则指明了转化时用的 reveal.js
,以上用到了web页的版本,转化后无法打开presenter模式,也就无法看到notes。我们也可以装local版本,这样可以方便的打开presenter模式,而这一步比较麻烦。
安装 reveal.js 需要 nodejs,过程如下:
安装nodejs https://nodejs.org/zh-cn/
node -v
首先,一些简单介绍:https://sspai.com/post/42179
安装reveal.js,nbconvert官网给出的流程完美解决我的问题
$git clone https://github.com/hakimel/reveal.js.git
$cd reveal.js
$git checkout 3.5.0
其中最关键的是$git checkout 3.5.0
。这一步我在其他各网络版本的说明中都没有看到,而仅仅git reveal.js到本地并不能建好本地的server。$git checkout
的作用是 "Updates files in the working tree to match the version in the index or the specified tree." 这个可以通过 $git help checkout
来查看。所以nbconvert生成的slides应该仅仅被固定版本的reveal.js支持。
按照网上流行版本,执行git clone https://github.com/hakimel/reveal.js.git
,然后就可以用
$ jupyter nbconvert --to slides xxx.ipynb --reveal-prefix reveal.js --output xxx
但在我的mac上没有成功。开始我以为是仅仅git reveal.js不够,于是按照另一些并非针对jupyter的reveal说明,去建立local的grunt server。最终建好了server,但是还是无法用上面的命令转化nb文件。所以最关键的还是git checkout 3.5.0
。以下是建立server的漫长过程:
执行 $npm install
显示错误
>puppeteer@1.20.0 install ...
Downloading Chromium ... Failed to download Chromium ...
需要下载Chromium,结果应该是Chromium下载不了,所以失败。解决办法是忽略掉下载,执行以下
npm i --save puppeteer --ignore-scripts
接着运行 npm start
还是报错
> grunt serve
Loading "Gruntfile.js" tasks...ERROR
>> Error: ENOENT: no such file or directory, scandir '.../reveal.js/node_modules/node-sass/vendor'
Warning: Task "serve" not found. Use --force to continue.
据说这个问题很坑。试过方法 npm rebuild node-sass
还是报错 Error: not found: python2
。最终解决办法如下,
npm install node-sass --save-dev
然后 npm start
网页弹出,终于好了!可以通过运行 npm start --port=8001
换端口。替换index.html
可演示其他slides
http://localhost:8000/index.html#/
http://localhost:8000/index.html?print-pdf#/