{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Output Widget\n", "\n", "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.\n", "\n", "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.\n", "\n", "The widget can be configured to capture just stdout or just stderr.\n", "\n", "## Example\n", "\n", "First we create the Output. Initially there is nothing in it, the Output is empty and so nothing appears." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%import com.twosigma.beakerx.widget.Output\n", "out = new Output()\n", "OutputManager.setOutput(out)\n", "out" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next cell starts a background thread that writes into that output. The results appear above, in the output." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t = new Thread({\n", " println(\"thread start\")\n", " for (int i = 0; i <20 ; i++){\n", " Thread.sleep(1000)\n", " println(\"thread \"+ i)\n", " if (i % 5 == 3) System.err.println(\"stderr!\")\n", " if (i % 6 == 2) out.display(HTML('any MIME type'))\n", " if (i % 7 == 4) out.display(new Plot(initHeight:150) << new Line(y: [0,5,2,3,11]))\n", " }});\n", "println(\"ready set go\")\n", "t.start()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the following cells after starting the threads with the previous cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "println(\"some other cell\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "OutputManager.clearOutput()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "OutputManager.setOutput(null)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t.stop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Only StdOut" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "onlyOut = new Output()\n", "OutputManager.setStdout(onlyOut)\n", "onlyOut" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "println(\"only stdout is captured\")\n", "System.err.println(\"stderr goes to its own cell\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "OutputManager.clear()\n", "OutputManager.setStdout(null)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Only Stderr" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "onlyErr = new Output()\n", "OutputManager.setStderr(onlyErr)\n", "onlyErr" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "println(\"only stdout is captured\")\n", "System.err.println(\"stderr goes to its own cell\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "OutputManager.clear()\n", "OutputManager.setStderr(null)" ] } ], "metadata": { "kernelspec": { "display_name": "Groovy", "language": "groovy", "name": "groovy" }, "language_info": { "codemirror_mode": "groovy", "file_extension": ".groovy", "mimetype": "", "name": "Groovy", "nbconverter_exporter": "", "version": "2.4.3" } }, "nbformat": 4, "nbformat_minor": 2 }