我有一个 Jupyter 笔记本,它在 Markdown 单元格中包含 python 变量,如下所示:
代码单元格:
x = 10
Run Code Online (Sandbox Code Playgroud)
降价单元格:
The value of x is {{x}}.
Run Code Online (Sandbox Code Playgroud)
在IPython的笔记本扩展Python的降价使得如果我移请在笔记本执行降价电池我动态显示这些变量。
降价单元格:
The value of x is 10.
Run Code Online (Sandbox Code Playgroud)
我想以编程方式执行笔记本中的所有单元格,并使用以下内容将它们保存到新笔记本中:
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
with open('report.ipynb') as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
ep.preprocess(nb, {})
with open('report_executed.ipynb', 'wt') as f:
nbformat.write(nb, f)
Run Code Online (Sandbox Code Playgroud)
这将执行代码单元而不是降价单元。它们仍然是这样的:
The value of x is {{x}}.
Run Code Online (Sandbox Code Playgroud)
我认为问题在于笔记本不受信任。有没有办法告诉 ExecutePreprocessor 信任笔记本?是否有另一种方法以编程方式执行笔记本,包括降价单元格中的 python 变量?
我有2个点(一个大小为8的向量)和3个不同的按位运算(And,Or,Xor)我映射了每个点和2D绘图上的按位运算的结果.现在我想要显示每个点真实数据本身和绘图旁边的操作结果(绘图的右边或绘图上方(只要可能或者它更好))所以稍后当我想分析结果时我可以轻松读取数据值.现在我的形象是这样的,你可以看到传说被切断了,我没有在情节之外写任何东西的地方:
我希望在我的情节之外显示的文字:
P1 P2 And Or Xor
0 1 0 1 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 1 0
1 1 1 1 0
1 1 1 1 0
1 1 1 1 0
Run Code Online (Sandbox Code Playgroud)
我正在使用的代码:
import numpy as np
import pylab as pl
fig = pl.figure()
ax = fig.add_subplot(111)
ax.plot(p1x, p1y, 'bx', label='Point 1', alpha=.55, markersize=30)
ax.plot(p2x, p2y, 'r+', label='Point 2', alpha=.55, markersize=30)
ax.plot(Andx, Andy, 'go', label='AND', …
Run Code Online (Sandbox Code Playgroud)