From e7f8fe6d0ab154a31770617f74988fb643c66def Mon Sep 17 00:00:00 2001 From: Micah R Ledbetter Date: Thu, 2 Nov 2017 21:34:33 -0500 Subject: [PATCH] Add examples for asynchronous display and interactive_output --- docs/source/examples/Using Interact.ipynb | 88 +++++++++++++++++++ .../source/examples/Widget Asynchronous.ipynb | 69 +++++++++++++++ 2 files changed, 157 insertions(+) diff --git a/docs/source/examples/Using Interact.ipynb b/docs/source/examples/Using Interact.ipynb index b7464eb142..cf3485c350 100644 --- a/docs/source/examples/Using Interact.ipynb +++ b/docs/source/examples/Using Interact.ipynb @@ -894,6 +894,94 @@ "interact(slow_function,i=FloatSlider(min=1e5, max=1e7, step=1e5, continuous_update=False));" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `interactive_output`\n", + "\n", + "`interactive_output` provides additional flexibility: you can control how the UI elements are laid out.\n", + "\n", + "Unlike `interact`, `interactive`, and `interact_manual`, `interactive_output` does not generate a user interface for the widgets. This is powerful, because it means you can create a widget, put it in a box, and then pass the widget to `interactive_output`, and have control over the widget and its layout." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "e6e8696551f64c45a73ef7cb61af3c08", + "version_major": 2, + "version_minor": 0 + }, + "text/html": [ + "

Failed to display Jupyter Widget of type VBox.

\n", + "

\n", + " If you're reading this message in Jupyter Notebook or JupyterLab, it may mean\n", + " that the widgets JavaScript is still loading. If this message persists, it\n", + " likely means that the widgets JavaScript library is either not installed or\n", + " not enabled. See the Jupyter\n", + " Widgets Documentation for setup instructions.\n", + "

\n", + "

\n", + " If you're reading this message in another notebook frontend (for example, a static\n", + " rendering on GitHub or NBViewer),\n", + " it may mean that your frontend doesn't currently support widgets.\n", + "

\n" + ], + "text/plain": [ + "VBox(children=(HBox(children=(IntSlider(value=0), IntSlider(value=0), IntSlider(value=0))),))" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "597c952b0a5b43248e25011e2d6bbfed", + "version_major": 2, + "version_minor": 0 + }, + "text/html": [ + "

Failed to display Jupyter Widget of type Output.

\n", + "

\n", + " If you're reading this message in Jupyter Notebook or JupyterLab, it may mean\n", + " that the widgets JavaScript is still loading. If this message persists, it\n", + " likely means that the widgets JavaScript library is either not installed or\n", + " not enabled. See the Jupyter\n", + " Widgets Documentation for setup instructions.\n", + "

\n", + "

\n", + " If you're reading this message in another notebook frontend (for example, a static\n", + " rendering on GitHub or NBViewer),\n", + " it may mean that your frontend doesn't currently support widgets.\n", + "

\n" + ], + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "a = widgets.IntSlider()\n", + "b = widgets.IntSlider()\n", + "c = widgets.IntSlider()\n", + "ui = widgets.HBox([a, b, c])\n", + "def f(a, b, c):\n", + " print((a, b, c))\n", + "\n", + "out = widgets.interactive_output(f, {'a': a, 'b': b, 'c': c})\n", + "\n", + "display(ui, out)" + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/docs/source/examples/Widget Asynchronous.ipynb b/docs/source/examples/Widget Asynchronous.ipynb index b7262c30c0..771bf2b454 100644 --- a/docs/source/examples/Widget Asynchronous.ipynb +++ b/docs/source/examples/Widget Asynchronous.ipynb @@ -280,6 +280,75 @@ "thread.start()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Interacting with output widgets from background threads\n", + "\n", + "Output widgets capture output based on the thread in which they were invoked.\n", + "In other words, in the thread where the output is invoked, there is a context\n", + "manager which starts and then stops the output capturing. If you call `display`\n", + "in a thread other than the thread containing the context manager, you cannot\n", + "rely on those `display` calls being captured by the context manager.\n", + "\n", + "Instead, we can pass an `Output` widget to the function executing in a thread,\n", + "and use the `Output`'s `append_display_data()`, `append_stdout()`, or\n", + "`append_stderr()` methods to append displayable output (such as from `HTML()`),\n", + "standard output (such as from `print()`), or standard error to the `Output`\n", + "widget." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Display in main thread'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "32e8e3b06ec44fabb832283d779fda97", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "A Jupyter Widget" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import threading\n", + "from IPython.display import display, HTML\n", + "import ipywidgets as widgets\n", + "\n", + "def thread_func(something, out):\n", + " for i in range(1, 10):\n", + " out.append_stdout('{} {} {}'.format(i, '**'*i, something))\n", + " out.append_display_data(HTML(\"

All done!

\"))\n", + "\n", + "display('Display in main thread')\n", + "out = widgets.Output()\n", + "# Now the key: the container is displayed (while empty) in the main thread\n", + "display(out)\n", + "\n", + "thread = threading.Thread(\n", + " target=thread_func,\n", + " args=(\"whatever\", out))\n", + "thread.start()" + ] + }, { "cell_type": "markdown", "metadata": {