Jupyter Notebook 文件¶
可以使用 Jupyter notebooks 创建内容。
例如,当前页的内容包含在 这个笔记本文件中。
Jupyter Book 支持所有被 Jupyter Notebook 支持的 Markdown。 这主要是一种 Markdown 的风格,叫做 CommonMark Markdown,只是做了一些小修改。 要想了解更多关于如何编写 Jupyter 风格的 Markdown 的信息,请参见 Markdown 文件。
代码块和图像输出¶
Jupyter Book 还将在书中嵌入代码块和输出。例如,下面是一些 Matplotlib 代码的示例:
from matplotlib import rcParams, cycler
import matplotlib.pyplot as plt
import numpy as np
plt.ion()
<matplotlib.pyplot._IonContext at 0x7fc2e86f8790>
# Fixing random state for reproducibility
np.random.seed(19680801)
N = 10
data = [np.logspace(0, 1, 100) + np.random.randn(100) + ii for ii in range(N)]
data = np.array(data).T
cmap = plt.cm.coolwarm
rcParams['axes.prop_cycle'] = cycler(color=cmap(np.linspace(0, 1, N)))
from matplotlib.lines import Line2D
custom_lines = [Line2D([0], [0], color=cmap(0.), lw=4),
Line2D([0], [0], color=cmap(.5), lw=4),
Line2D([0], [0], color=cmap(1.), lw=4)]
fig, ax = plt.subplots(figsize=(10, 5))
lines = ax.plot(data)
ax.legend(custom_lines, ['Cold', 'Medium', 'Hot']);
注意,上面的图像被捕获并显示在您的站点中。
[Text(0.5, 1.0, 'Smoother linez')]
在发布前删除内容¶
你也可以在将你的书发布到网上之前删除一些内容。
作为参考,您可以下载本页的笔记本内容。
您可以只删除代码,以便图像和其他输出仍然显示。
thisvariable = "this plot *will* show up in the textbook."
fig, ax = plt.subplots()
x = np.random.randn(100)
y = np.random.randn(100)
ax.scatter(x, y, s=np.abs(x*100), c=x, cmap=plt.cm.coolwarm)
ax.text(0, .5, thisvariable, fontsize=20, transform=ax.transAxes)
ax.set_axis_off()
如果您想快速显示单元格输出,而不需要用代码混淆内容,那么这种方法可以很好地工作。 这适用于任何单元格输出,比如 Pandas DataFrame。
import pandas as pd
pd.DataFrame([['hi', 'there'], ['this', 'is'], ['a', 'DataFrame']], columns=['Word A', 'Word B'])
| Word A | Word B | |
|---|---|---|
| 0 | hi | there |
| 1 | this | is |
| 2 | a | DataFrame |
查阅 删除代码单元格内容 了解更多关于如何删除和隐藏内容的信息。
可交互的输出¶
我们可以对“交互”材料做同样的事情。下面我们将展示一张使用 folium 的地图。在构建图书时,将保留创建交互式地图的代码。
import folium
m = folium.Map(
location=[45.372, -121.6972],
zoom_start=12,
tiles='Stamen Terrain'
)
folium.Marker(
location=[45.3288, -121.6625],
popup='Mt. Hood Meadows',
icon=folium.Icon(icon='cloud')
).add_to(m)
folium.Marker(
location=[45.3311, -121.7113],
popup='Timberline Lodge',
icon=folium.Icon(color='green')
).add_to(m)
folium.Marker(
location=[45.3300, -121.6823],
popup='Some Other Location',
icon=folium.Icon(color='red', icon='info-sign')
).add_to(m)
m
Make this Notebook Trusted to load map: File -> Trust Notebook
笔记本单元格的富文本输出¶
因为笔记本有富文本输出,你可以存储这些在你的 Jupyter Book!例如,下面是命令行帮助菜单,看看它是如何很好地格式化的。
!jupyter-book build --help
Usage: jupyter-book build [OPTIONS] PATH_SOURCE
Convert your book's or page's content to HTML or a PDF.
Options:
--path-output TEXT Path to the output artifacts
--config TEXT Path to the YAML configuration file
(default: PATH_SOURCE/_config.yml)
--toc TEXT Path to the Table of Contents YAML file
(default: PATH_SOURCE/_toc.yml)
-W, --warningiserror Error on warnings.
-n, --nitpick Run in nit-picky mode, to generates warnings
for all missing references.
--keep-going With -W, do not stop the build on the first
warning, instead error on build completion
--all Re-build all pages. The default is to only
re-build pages that are new/changed since
the last run.
--builder [html|dirhtml|singlehtml|pdfhtml|latex|pdflatex|linkcheck|custom]
Which builder to use.
--custom-builder TEXT Specify alternative builder provided by
Sphinx, including text and epub. This can
only be used with --builder=custom. Valid
options listed at https://www.sphinx-
doc.org/en/master/man/sphinx-build.html
-v, --verbose increase verbosity (can be repeated)
-q, --quiet -q means no sphinx status, -qq also turns
off warnings
--individualpages [pdflatex] Enable build of PDF files for
each individual page
-h, --help Show this message and exit.
这里有个错误。可以通过添加 raises-exception 标签。
this_will_error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/tmp/ipykernel_2420/1151370313.py in <module>
----> 1 this_will_error
NameError: name 'this_will_error' is not defined