在笔记本中编写代码的约定。
pidgin的Python项目详细描述
pidgin
是ipython魔术的集合,用于创建可计算的文章。
if__name__=='__main__':%load_extpidgin
降价模式
%pidginmarkdown
---With`pidgin.markdown`,codecellsacceptmarkdown.Anyindentedcodeblocksareexecuted.foo=42print(f"foo is {foo}")>Acceptingthe`pidgin.markdown`convetionmeanstheauthoragreestoindentalltheircodeatleastonce;andsometimesmoreinnestedlists.---
使用pidgin.markdown
,代码单元格接受标记。执行任何缩进的代码块。
foo = 42
print(f"foo is {foo}")
Accepting the
pidgin.markdown
convetion means the author agrees to indent all their code at least once; and sometimes more in nested lists.
foo is 42
模板模式
有了模板,真正的数据可以插入到计算论文中。作者应该希望他们的笔记本重新启动并在模板模式下运行。
%pidgintemplateSkippingthefirstlinesuppressesthemarkdownoutput.
---Intemplatemode,`jinja2`maybeinvokedtotemplatemarkdownandcode.Wealreadyknowthat`foo`is42,butcantestthatassertionwithassertfoois{{foo}}is42{%foriinrange(3)%}print({{i}}){%endfor%}---
在模板模式下,可以调用jinja2
来标记和编码模板。我们已经知道foo
是42,但是可以用
assert foo is 42 is 42
print(0)
print(1)
print(2)
0
1
2
# Turning off magics%pidgin--offtemplatemarkdown
关闭魔法
%pidgin --off template markdown
山药
用---
%pidginconventions
---a:42
asserta==42
图形化
用graph
或digraph
!conda install -y graphviz
graph{{IpythonJuliaR}--Jupyter}
File "<ipython-input-9-1661b3d05729>", line 1
graph { {Ipython Julia R}--Jupyter}
^
SyntaxError: invalid syntax
笔记本电脑作为来源
pidgin使用笔记本作为源代码;保留行号,这样笔记本源代码就可以产生半正常的回溯。
frompidginimportmarkdown,template,conventions
pidgin加载器允许作者直接将笔记本作为源文件导入。这意味着所有的洋泾浜文件都是可导入的。
%%pidginmarkdowntemplateconventionsimportreadme
assertall(file.__file__.endswith('.ipynb')forfilein(markdown,template,conventions))
一切都应该计算
将文档转换为其他格式;重新启动,全部运行,nbconvert
。
%%pidginmarkdowntemplateUsepidginacellmagictotemporarilyemployanyconvetions.if__name__=='__main__':!jupyternbconvert--tomarkdownreadme.ipynb