{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Using widgets in IPython Parallel\n", "\n", "IPython Parallel 7.1 introduces basic support for using Jupyter widgets from a notebook to engines.\n", "\n", "This allows things like progress bars for incremental stages,\n", "when IPP's own task-level progress doesn't show you enough information.\n", "\n", "As always, we start by creating and connecting to a cluster:" ] }, { "cell_type": "code", "execution_count": 1, "id": "1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Starting 4 engines with \n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8ac284e0bbb1461baef9fffe347c18a0", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/4 [00:00" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import ipyparallel as ipp\n", "\n", "rc = ipp.Cluster(n=4).start_and_connect_sync()\n", "rc.activate()" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "IPython widgets support updateable readouts of things like progress.\n", "\n", "There are [lots of widgets to choose from][widget list], but for our purposes,\n", "we are going to use `IntProgress`\n", "\n", "[widget list]: https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20List.html" ] }, { "cell_type": "code", "execution_count": 2, "id": "3", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "dc361372c39e4a88a9eddd35967bc4b8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "IntProgress(value=0, description='Step 0', max=10)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import time\n", "\n", "import ipywidgets as W\n", "from IPython.display import display\n", "\n", "progress = W.IntProgress(min=0, max=10, description=\"Step 0\", readout=True)\n", "display(progress)\n", "\n", "for i in range(10):\n", " progress.value += 1\n", " progress.description = f\"Step {progress.value}/{progress.max}\"\n", " time.sleep(0.1)\n", "\n", "progress.bar_style = \"success\" # change color when it's done" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "IPython Parallel supports progress and interactive waits on AsyncResult objects,\n", "which is great when your tasks are small and you have a lot of them:" ] }, { "cell_type": "code", "execution_count": 3, "id": "5", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b969bda88b784a9fb4c1b3e47af978d1", "version_major": 2, "version_minor": 0 }, "text/plain": [ ": 0%| | 0/1000 [00:00" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rc[:].scatter(\"rank\", rc.ids, flatten=True)" ] }, { "cell_type": "code", "execution_count": 6, "id": "10", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[output:1]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "fab62a72396946c4a23713818e810cf6", "version_major": 2, "version_minor": 0 }, "text/plain": [ "IntProgress(value=0, description='rank 1: 0', max=10)" ] }, "metadata": { "engine": 1 }, "output_type": "display_data" }, { "data": { "text/plain": [ "[output:3]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "09fed44290574465abc2af7da1473db8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "IntProgress(value=0, description='rank 3: 0', max=10)" ] }, "metadata": { "engine": 3 }, "output_type": "display_data" }, { "data": { "text/plain": [ "[output:0]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6ba7ddeac80b4b25af7e2f76df76fb9f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "IntProgress(value=0, description='rank 0: 0', max=10)" ] }, "metadata": { "engine": 0 }, "output_type": "display_data" }, { "data": { "text/plain": [ "[output:2]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9248af9c8c604c30a83386b742afab1d", "version_major": 2, "version_minor": 0 }, "text/plain": [ "IntProgress(value=0, description='rank 2: 0', max=10)" ] }, "metadata": { "engine": 2 }, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "cb2270f240a547e0a6e14a500026deb3", "version_major": 2, "version_minor": 0 }, "text/plain": [ "%px: 0%| | 0/4 [00:00