Stdout and stderr are normally directed to appear in the output of the cell whose code produced them. The output widget allows you to consolidate output in one place, both captured IO and rich objects.
This can be used for logging and also for catching the output of background threads, which can be left running after the cell that starts them completes.
The widget can be configured to capture just stdout or just stderr.
First we create the Output widget, and direct output to it.
The setOutput
method returns the same widget you pass to it, so the widget is returned from the cell.
Initially there is nothing in it, the Output is empty and so nothing appears.
In [ ]:
%import com.twosigma.beakerx.widget.Output
out = new Output()
OutputManager.setOutput(out)
The next cell starts a background thread that writes into that output. The results appear above, in the output widget.
In [ ]:
t = new Thread({
println("background thread start")
for (i = 0; i < 20 ; i++) {
Thread.sleep(1000)
println("tick " + i)
if (i % 5 == 3) System.err.println("stderr!")
if (i % 6 == 2) display(HTML('any <b>MIME</b> <em>type</em>'))
if (i % 7 == 4) out.display(new Plot(initHeight:150) << new Line(y: [0,5,2,3,11]))
}});
println("ready set go")
t.start()
Run the following cells after starting the threads with the previous cell.
In [ ]:
println("some other cell")
In [ ]:
display(HTML('any <b>MIME</b> <em>type</em>'))
display(new Plot(initHeight:150) << new Line(y: [0,11,2,8,1]))
In [ ]:
OutputManager.clearOutput()
In [ ]:
OutputManager.setOutput(null)
In [ ]:
t.stop()
In [ ]:
OutputManager.setStandardOutput(new Output())
In [ ]:
println("only stdout is captured")
System.err.println("stderr goes to its own cell")
In [ ]:
OutputManager.clear()
OutputManager.setStandardOutput(null)
In [ ]:
OutputManager.setStandardError(new Output())
In [ ]:
println("stdout goes to its own cell")
System.err.println("only stderr is captured")
In [ ]:
OutputManager.clear()
OutputManager.setStandardError(null)