{ "cells": [ { "cell_type": "markdown", "id": "e06e6145-5d8a-42f0-a573-883ceefeb456", "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": "cc0f626a-4e9e-4cd7-b9d5-37aa72656045", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using existing profile dir: '/Users/minrk/.ipython/profile_default'\n", "Starting 4 engines with \n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "dfdbae12208c47ea98ef749d45d64f62", "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", "rc = ipp.Cluster(n=4).start_and_connect_sync()\n", "rc.activate()" ] }, { "cell_type": "markdown", "id": "9a68b37e-8f8c-4e21-87ff-ff993d74af4d", "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": "a2ee17a5-ba31-4d3e-9a91-cba386b8e2a9", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "358962d716384d2199b8522907c9033b", "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": "6ff1fe0c-adb3-43f7-b89f-f259acc8a72a", "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": "2d510a7e-69af-4d5f-bdd3-bb35933933bd", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "05a2877edaee4d75bae10bfa7ada3c32", "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": "307a2f86-0948-461e-94f1-748471707feb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[output:3]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e02c6493e62e4ed38d2dfd45b7115667", "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:2]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "197376ab6c2247c1af0769eae93220cb", "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": { "text/plain": [ "[output:0]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "dd5ef98076d94493b57b021455162c56", "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:1]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "697cf15dec6447beaa227add1fa986e8", "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": { "application/vnd.jupyter.widget-view+json": { "model_id": "8b5ac39bd6114df9b1820ff1402e5a6c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "%px: 0%| | 0/4 [00:00