{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Workspaces" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First we show the ability to set a workspace from Python:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "class Workspace:\n", " def __init__(self, workspace):\n", " self.workspace = workspace\n", " def _repr_mimebundle_(self, include=None, exclude=None):\n", " return {\n", " \"application/x.jupyterlab.workspace+json\": self.workspace\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Click the button below to reload the page with the new workspace:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "application/x.jupyterlab.workspace+json": { "@jupyterlab/settingeditor-extension:plugin": { "container": { "plugin": "@jupyterlab/filebrowser-extension:browser", "sizes": [ 0.4791970802919708, 0.5208029197080292 ] }, "sizes": [ 0.11319534282018111, 0.8868046571798189 ] }, "application-mimedocuments:Demo.ipynb:JSON": { "data": { "factory": "JSON", "path": "Demo.ipynb" } }, "application-mimedocuments:package.json:JSON": { "data": { "factory": "JSON", "path": "package.json" } }, "application-mimedocuments:tmp.json:JSON": { "data": { "factory": "JSON", "path": "tmp.json" } }, "cloned-outputs:explorations/Untitled6.ipynb:17": { "data": { "index": 17, "path": "explorations/Untitled6.ipynb" } }, "file-browser-filebrowser:cwd": { "path": "" }, "ipywidgets:Demo.ipynb:c2fe7388d7fa4a8597d6f690a26f7ba9": { "data": { "data": { "model_id": "c2fe7388d7fa4a8597d6f690a26f7ba9", "version_major": 2, "version_minor": 0 }, "notebook": "Demo.ipynb" } }, "layout-restorer:data": { "left": { "collapsed": true, "widgets": [ "filebrowser", "running-sessions", "command-palette", "tab-manager" ] }, "main": { "current": "notebook:Demo.ipynb", "dock": { "children": [ { "currentIndex": 0, "type": "tab-area", "widgets": [ "notebook:Demo.ipynb" ] }, { "children": [ { "currentIndex": 0, "type": "tab-area", "widgets": [ "application-mimedocuments:Demo.ipynb:JSON" ] }, { "currentIndex": 0, "type": "tab-area", "widgets": [ "terminal:1" ] } ], "orientation": "horizontal", "sizes": [ 0.5, 0.5 ], "type": "split-area" } ], "orientation": "vertical", "sizes": [ 0.3756139489194499, 0.6243860510805501 ], "type": "split-area" }, "mode": "multiple-document" }, "right": { "collapsed": true, "widgets": [] } }, "notebook:Demo.ipynb": { "data": { "factory": "Notebook", "path": "Demo.ipynb" } }, "terminal:1": { "data": { "name": "1" } } }, "text/plain": [ "<__main__.Workspace at 0x7ff198381be0>" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Workspace({\n", " \"ipywidgets:Demo.ipynb:c2fe7388d7fa4a8597d6f690a26f7ba9\": {\n", " \"data\": {\n", " \"data\": {\n", " \"model_id\": \"c2fe7388d7fa4a8597d6f690a26f7ba9\",\n", " \"version_major\": 2,\n", " \"version_minor\": 0,\n", " },\n", " \"notebook\": \"Demo.ipynb\",\n", " }\n", " },\n", " \"layout-restorer:data\": {\n", " \"main\": {\n", " \"dock\": {\n", " \"type\": \"split-area\",\n", " \"orientation\": \"vertical\",\n", " \"sizes\": [0.3756139489194499, 0.6243860510805501],\n", " \"children\": [\n", " {\n", " \"type\": \"tab-area\",\n", " \"currentIndex\": 0,\n", " \"widgets\": [\"notebook:Demo.ipynb\"],\n", " },\n", " {\n", " \"type\": \"split-area\",\n", " \"orientation\": \"horizontal\",\n", " \"sizes\": [0.5, 0.5],\n", " \"children\": [\n", " {\n", " \"type\": \"tab-area\",\n", " \"currentIndex\": 0,\n", " \"widgets\": [\n", " \"application-mimedocuments:Demo.ipynb:JSON\"\n", " ],\n", " },\n", " {\n", " \"type\": \"tab-area\",\n", " \"currentIndex\": 0,\n", " \"widgets\": [\"terminal:1\"],\n", " },\n", " ],\n", " },\n", " ],\n", " },\n", " \"mode\": \"multiple-document\",\n", " \"current\": \"notebook:Demo.ipynb\",\n", " },\n", " \"left\": {\n", " \"collapsed\": True,\n", " \"widgets\": [\n", " \"filebrowser\",\n", " \"running-sessions\",\n", " \"command-palette\",\n", " \"tab-manager\",\n", " ],\n", " },\n", " \"right\": {\"collapsed\": True, \"widgets\": []},\n", " },\n", " \"notebook:Demo.ipynb\": {\"data\": {\"path\": \"Demo.ipynb\", \"factory\": \"Notebook\"}},\n", " \"@jupyterlab/settingeditor-extension:plugin\": {\n", " \"sizes\": [0.11319534282018111, 0.8868046571798189],\n", " \"container\": {\n", " \"plugin\": \"@jupyterlab/filebrowser-extension:browser\",\n", " \"sizes\": [0.4791970802919708, 0.5208029197080292],\n", " },\n", " },\n", " \"cloned-outputs:explorations/Untitled6.ipynb:17\": {\n", " \"data\": {\"path\": \"explorations/Untitled6.ipynb\", \"index\": 17}\n", " },\n", " \"file-browser-filebrowser:cwd\": {\"path\": \"\"},\n", " \"application-mimedocuments:tmp.json:JSON\": {\n", " \"data\": {\"path\": \"tmp.json\", \"factory\": \"JSON\"}\n", " },\n", " \"application-mimedocuments:package.json:JSON\": {\n", " \"data\": {\"path\": \"package.json\", \"factory\": \"JSON\"}\n", " },\n", " \"application-mimedocuments:Demo.ipynb:JSON\": {\n", " \"data\": {\"path\": \"Demo.ipynb\", \"factory\": \"JSON\"}\n", " },\n", " \"terminal:1\": {\"data\": {\"name\": \"1\"}},\n", "})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## IPyWidgets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we show the ablity to launch ipywidgets in new tabs. Click the button bellow to launch a new indows in JupyterLab with the widget:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "class Widget:\n", " def __init__(self, widget):\n", " self.widget = widget\n", " def _repr_mimebundle_(self, include=None, exclude=None):\n", " return {\n", " \"application/x.jupyterlab.widget+json\": {\n", " 'model_id': self.widget.model_id,\n", " 'version_major': 2,\n", " 'version_minor': 0,\n", " }\n", " }" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import ipywidgets as widgets" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/x.jupyterlab.widget+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "text/plain": [ "<__main__.Widget at 0x7ff1680ce0d0>" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "items = [widgets.Label(str(i)) for i in range(5)]\n", "left_box = widgets.VBox([items[0], items[1], items[4]])\n", "right_box = widgets.VBox([items[2], items[3]])\n", "w = widgets.HBox([left_box, right_box])\n", "Widget(w)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## IPyWidget Workspaces" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can combine our widget creating commands with our workspace commands and programatically generate workspaces that launch widgets in certain positions:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "PATH = 'Demo.ipynb'" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# area = widget or [orientation, sizes, areas]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def _output_widgets(area, data):\n", " if isinstance(area, widgets.Widget):\n", " model_id = area.model_id\n", " \n", " key = f\"ipywidgets:{PATH}:{model_id}\"\n", " data[key] = {\n", " \"data\": {\n", " \"data\": {\n", " \"model_id\": model_id,\n", " \"version_major\": 2,\n", " \"version_minor\": 0\n", " },\n", " \"notebook\": PATH\n", " }\n", " }\n", " return {\"type\": \"tab-area\", \"currentIndex\": 0, \"widgets\": [key]}\n", " orientation, sizes, areas = area\n", " return {\n", " \"type\": \"split-area\",\n", " \"orientation\": orientation,\n", " \"sizes\": sizes,\n", " \"children\": [_output_widgets(area, data) for area in areas]\n", " }\n", "def output_widgets(area):\n", " data = {}\n", " data[\"layout-restorer:data\"] = {\n", " \"main\": {\n", " \"dock\": _output_widgets(area, data),\n", " \"mode\": \"multiple-document\"\n", " }\n", " }\n", " return Workspace(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This `output_widgets` function takes in a recursive data structure to represent the layout you want to render, with widgets inside" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: matplotlib in /usr/local/Caskroom/miniconda/base/envs/jupyter-widgets-takeover/lib/python3.8/site-packages (3.1.3)\n", "Requirement already satisfied: cycler>=0.10 in /usr/local/Caskroom/miniconda/base/envs/jupyter-widgets-takeover/lib/python3.8/site-packages (from matplotlib) (0.10.0)\n", "Requirement already satisfied: numpy>=1.11 in /usr/local/Caskroom/miniconda/base/envs/jupyter-widgets-takeover/lib/python3.8/site-packages (from matplotlib) (1.18.1)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/Caskroom/miniconda/base/envs/jupyter-widgets-takeover/lib/python3.8/site-packages (from matplotlib) (1.1.0)\n", "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/Caskroom/miniconda/base/envs/jupyter-widgets-takeover/lib/python3.8/site-packages (from matplotlib) (2.4.6)\n", "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/Caskroom/miniconda/base/envs/jupyter-widgets-takeover/lib/python3.8/site-packages (from matplotlib) (2.8.1)\n", "Requirement already satisfied: six in /usr/local/Caskroom/miniconda/base/envs/jupyter-widgets-takeover/lib/python3.8/site-packages (from cycler>=0.10->matplotlib) (1.14.0)\n", "Requirement already satisfied: setuptools in /usr/local/Caskroom/miniconda/base/envs/jupyter-widgets-takeover/lib/python3.8/site-packages (from kiwisolver>=1.0.1->matplotlib) (45.2.0.post20200210)\n" ] } ], "source": [ "!pip install matplotlib" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "application/x.jupyterlab.workspace+json": { "ipywidgets:Demo.ipynb:00236f01d6bd41878a50bec86239d151": { "data": { "data": { "model_id": "00236f01d6bd41878a50bec86239d151", "version_major": 2, "version_minor": 0 }, "notebook": "Demo.ipynb" } }, "ipywidgets:Demo.ipynb:04e4191f1c0a4cef8c260bffa69eda02": { "data": { "data": { "model_id": "04e4191f1c0a4cef8c260bffa69eda02", "version_major": 2, "version_minor": 0 }, "notebook": "Demo.ipynb" } }, "ipywidgets:Demo.ipynb:538943de4f424fcd9cd1756947e63045": { "data": { "data": { "model_id": "538943de4f424fcd9cd1756947e63045", "version_major": 2, "version_minor": 0 }, "notebook": "Demo.ipynb" } }, "ipywidgets:Demo.ipynb:96c24332bb5247f18efe299c9ee245e9": { "data": { "data": { "model_id": "96c24332bb5247f18efe299c9ee245e9", "version_major": 2, "version_minor": 0 }, "notebook": "Demo.ipynb" } }, "ipywidgets:Demo.ipynb:cbd885243a2d4c56b025b476f7587ce9": { "data": { "data": { "model_id": "cbd885243a2d4c56b025b476f7587ce9", "version_major": 2, "version_minor": 0 }, "notebook": "Demo.ipynb" } }, "layout-restorer:data": { "main": { "dock": { "children": [ { "currentIndex": 0, "type": "tab-area", "widgets": [ "ipywidgets:Demo.ipynb:cbd885243a2d4c56b025b476f7587ce9" ] }, { "currentIndex": 0, "type": "tab-area", "widgets": [ "ipywidgets:Demo.ipynb:00236f01d6bd41878a50bec86239d151" ] }, { "children": [ { "currentIndex": 0, "type": "tab-area", "widgets": [ "ipywidgets:Demo.ipynb:96c24332bb5247f18efe299c9ee245e9" ] }, { "currentIndex": 0, "type": "tab-area", "widgets": [ "ipywidgets:Demo.ipynb:04e4191f1c0a4cef8c260bffa69eda02" ] }, { "currentIndex": 0, "type": "tab-area", "widgets": [ "ipywidgets:Demo.ipynb:538943de4f424fcd9cd1756947e63045" ] } ], "orientation": "vertical", "sizes": [ 0.1, 0.1, 0.8 ], "type": "split-area" } ], "orientation": "horizontal", "sizes": [ 0.3, 0.3, 0.4 ], "type": "split-area" }, "mode": "multiple-document" } } }, "text/plain": [ "<__main__.Workspace at 0x7ff1784783a0>" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%matplotlib inline\n", "\n", "from ipywidgets import interactive\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "def f(m, b):\n", " plt.figure(2)\n", " x = np.linspace(-10, 10, num=1000)\n", " plt.plot(x, m * x + b)\n", " plt.ylim(-5, 5)\n", " plt.show()\n", "\n", "interactive_plot = interactive(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))\n", "interactive_plot.children\n", "\n", "output_widgets(\n", " [\"horizontal\", [0.3, 0.3, 0.4], [widgets.IntSlider(), widgets.ToggleButton(), [\"vertical\", [0.1, 0.1, 0.8], interactive_plot.children]]]\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.1" } }, "nbformat": 4, "nbformat_minor": 4 }