a field-theory motivated approach to computer algebra

By default the output generated by display will generate a new output cell. However, it is possible to put the output into an existing cell. In order to do this, use the cell_id parameter of the display call, passing it the ID of the cell you want to use for output. To obtain that cell, note that all display calls return the ID of the cell generated or re-used. The following example shows how to use this. On the first iteration of the loop we pass the ID '0', which leads to generation of a new cell. The call returns the ID of the newly generated cell, which is then used for any subsequent iterations of the loop.
from time import time, sleep
out=0 for i in range(100): if i!=0: sleep(0.02) out=display(i+1, cell_id=out)
100
You do not necessarily have to refer to an output cell generated by the current input cell, as the following example shows. The first input cell generates output, which is then changed by the following output cell.
out=0 for i in range(40): out=display("🍅"*i, cell_id=out) sleep(0.01)
🍅
for i in range(40): out=display("🍅"*(40-i), cell_id=out) sleep(0.01)
Note that while the updates are reasonably fast, there is of course some overhead; the following cell is a modified version of the first example, timing the total run.
start=time() out=0 for i in range(1000): if i!=0: sleep(0.001) out=display(i+1, cell_id=out) end=time() display(f"time taken {(end-start):.2f} sec")
1000
time taken 1.35 sec
You can also use this mechanism to generate simple animations of functions. If you use display to put a plot into an existing output cell, the plot will be replaced.
from cdb.graphics.plot import plot import numpy as np
out=0 for i in range(3): for v in np.concatenate([np.linspace(1.0, 3.0, 50), np.linspace(3.0, 1.0, 50)]): ex := \cos(v x) + x; substitute(ex, $v -> @(v)$) out = display(plot(ex, ($x$, 0, 6.28)), out)