Showing assignment results in IPython notebook?

霸气de小男生 提交于 2019-12-08 04:54:49

问题


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

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.

What is the proper way to document the computation this way with IPython?


回答1:


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



来源:https://stackoverflow.com/questions/32839954/showing-assignment-results-in-ipython-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!