Jupyter notebook: show output “in real time” instead of once cell finishes running

At least on my system*, the following Jupyter cell shows no output until execution finishes after 10s, when all the output appears at once.

## In:
for i in 1:10
    @show i
    sleep(1)
end

#= Out:
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10

This is annoying for long computations where I want to print progress data or calculation results in real time, e.g.:

## In:
Flux.train!(loss, Flux.params(model), data, opt; Flux.throttle(() -> @show(loss(testbatch...)), 1))

#= Out:

loss(testbatch...) = 0.748777660585992
loss(testbatch...) = 0.7432442125793893
loss(testbatch...) = 0.7378878076566309
loss(testbatch...) = 0.7327608453399246
loss(testbatch...) = 0.727844955342706
loss(testbatch...) = 0.7230646648872169
loss(testbatch...) = 0.7185265377063492
loss(testbatch...) = 0.7141560986532893

How can I have real time output from notebook cells?


My system

macOS 10.15.7

$ julia -v
julia version 1.7.2

$ jupyter --version
jupyter core     : 4.7.0
jupyter-notebook : 6.1.5
qtconsole        : not installed
ipython          : 7.19.0
ipykernel        : 5.4.2
jupyter client   : 6.1.7
jupyter lab      : 2.2.9
nbconvert        : 6.0.7
ipywidgets       : not installed
nbformat         : 5.0.8
traitlets        : 5.0.5
for i in 1:10
    @show i
    sleep(1)
    flush(stdout)
end
2 Likes