This is an example for the extension displaytools
from the package ipydex for the IPython Notebook.
The extension introduces some "magic" comments (like ##:
, ##:T
, ##:S
, and ##:i
) which trigger additional output (normally only the return value of the last line of a cell is printed). See below for why is this useful?
Note that this extension is not yet in mature state and is likely to have some issues.
This example was tested successfully under python3.6. However it should also work on python 2.7.
%load_ext ipydex.displaytools
import ipydex
ipydex.__version__
import numpy as np
import sympy as sp
from sympy import sin, cos
from sympy.abc import t, pi
from sympy.interactive import printing
printing.init_printing() # this activates latex rendering for better readable output
x = 2*pi*t
y1 = cos(x)
y2 = cos(x)*t
ydot1 = y1.diff(t) ##:
ydot2 = y2.diff(t) ##:
ydot1_obj = y1.diff(t, evaluate=False) ##:
If there is no assignment taking place, ##:
nevertheless causes the display of the respective result.
y1.diff(t,t) ##:
y2.diff(t,t) ##:
Sometimes, it can save much space if some return value is displayed in transposed form (while still being assigned not transposed). Compare these examples:
xx = sp.Matrix(sp.symbols('x1:11')) ##:
yy = sp.Matrix(sp.symbols('y1:11')) ##:T
xx.shape, yy.shape ##:
Sometimes the actual content is not of interest or simply too big to be displayed. Nevertheless the shape is already informative.
zz = sp.Matrix(sp.symbols('z1:101')) ##:S
zz2 = np.arange(70).reshape(14, -1) ##:S
# special comments can be combined with other comments:
a = 3 # comment ##:
# Multiline statements are not yet supported:
a = [1,
2] ##:
# indented lines are
def f(x):
b = [10+x, 20] ##:
return b
# in the case of multiple assignments the rightmost is taken:
c1, c2 = cc = [100, 200] ##:
res = f(3)
res = f(4)
# numpy arrays and matrices are indented to take left-hand-side into account
np.random.seed(0)
A = np.random.random((5, 4)) ##:
np.matrix(A) ##:
# heuristic to insert a newline if indentation would be too long
A_with_long_name = A ##:
# that new-line feature is aware of the configured linewidth
np.set_printoptions(linewidth=110)
B = np.random.random((5, 7)) ##:
B_with_long_name = B ##:
With ##:i
some general type information is diplayed
C = 2*B ##:i
list(C) ##:i
C[0] ##:i
C[0, 0] ##:i
The supported syntax is somewhat flexible (and does not anymore rely on spaces). In case of multiple assignments the rightmost is used.
w=0 ##:
w1, w2= 10, 20 ##:
w1, w2,=w= 10, 20 ##:
w = w1, w2 = 10, 20 ##:
Compatibility with indentation is also given:
u = 10
v = 20 ##:
if 1:
w = 0 ##:
x = 1 ##:
if 1:
y = 2
z = 3 ##:
a = 0
b = 1 ##:
import pandas
# this looks suboptimal
a = pandas.Series(range(50, 55)) ##:
# this looks better
a = pandas.Series(range(50, 55)) ##:\n
If something goes wrong inside displaytools, a message is printed the cell is left unchanged and executed normally.
x = 1 ##:
##!! raise TestException !!
y = 1234 ##:
y
from ipydex import save_current_nb_as_html
save_current_nb_as_html()