在 IPython 笔记本中显示作业结果?

Showing assignment results in IPython notebook?

我正在 ipython 笔记本中的传热问题写一系列 equations/assignments(我是那个笔记本的新手),像这样:

# 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 魔法,但它按字母顺序显示变量。

理想情况下,我想得到这样的东西:

# 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

可能有 In/Out 提示(我不介意)和语法高亮。

用 IPython 记录计算的正确方法是什么?

此功能是not part of the core functionality of IPython. It has instead been incorporated into the extension displaytools。引用自回购协议:

Load this extension with %load_ext displaytools or %reload_ext displaytools. The latter is useful for debugging.

Example invocation:

my_random_variable = np.random.rand() ##

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.

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).