在IPython笔记本中显示分配结果? [英] Showing assignment results in IPython notebook?
问题描述
我正在从ipython笔记本中的传热问题中写出一系列方程式/赋值(我是新手),像这样:
I am writing a chain of equations/assignments from a heat transfer problem in ipython notebook (I am new to that one), like this:
# nominal diameter
d=3.55 # m
# ambient temperature
T0=15 # C
# surface temperature
Tw=300 # C
# average film temperature
Tm=(T0+Tw)/2+273.15 # K!
# expansion coefficient, $$\beta=1/T$$ for ideal gas
beta=1./Tm
# temperature difference
dT=Tw-T0 # C or K
是否有一种方法可以回显每个分配,以便显示那些(主要是计算出的)值?我知道%whos
的魔力,但是可以按字母顺序显示变量.
Is there a way to echo each assignment, so that those (mostly computed) values are shown? I am aware of the %whos
magic, but that shows variables alphabetically.
理想情况下,我想得到这样的东西:
Ideally, I would like to get something like this:
# nominal diameter
d=3.55 # m
3.55
# ambient temperature
T0=15 # C
15
# surface temperature
Tw=300 # C
300
# average film temperature
Tm=(T0+Tw)/2+273.15 # K!
430.15
# expansion coefficient, $$\beta=1/T$$ for ideal gas
beta=1./Tm
0.00232477042892
# temperature difference
dT=Tw-T0 # C or K
285
也许带有输入/输出提示(我不介意)并且语法高亮.
perhaps with In/Out promps (I don't mind) and syntax-highlighted.
使用IPython以这种方式记录计算的正确方法是什么?
What is the proper way to document the computation this way with IPython?
推荐答案
此功能是不是 IPython核心功能的一部分.相反,它已合并到扩展 displaytools
中.回购报价:
This feature is not part of the core functionality of IPython. It has instead been incorporated into the extension displaytools
. Quote from the repo:
使用
%load_ext displaytools
或%reload_ext displaytools
加载此扩展名.后者对于调试很有用.
Load this extension with
%load_ext displaytools
or%reload_ext displaytools
. The latter is useful for debugging.
示例调用:
my_random_variable = np.random.rand() ##
由于特殊注释##
,扩展名插入行
display(my_random_variable)
传递给源代码,然后再传递给
解释器,即在执行之前.
Due to the special comment ##
the extension inserts the line
display(my_random_variable)
to the source code, before it is passed to
the interpreter, i.e. before its execution.
这样,将生成其他输出,从而使笔记本成为
更容易理解(因为读者知道
my_random_variable
).节省打字工作和代码
重复添加display(my_random_variable)
.
That way, additional output is generated, which makes the notebook be
more comprehensible (because the reader knows the content of
my_random_variable
). It saves the typing effort and the code
duplication of manually adding display(my_random_variable)
.
这篇关于在IPython笔记本中显示分配结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!