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?
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 linedisplay(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 addingdisplay(my_random_variable)
.
来源:https://stackoverflow.com/questions/32839954/showing-assignment-results-in-ipython-notebook