{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Command Registry" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipylab import JupyterFrontEnd\n", "\n", "app = JupyterFrontEnd()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "app.version" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List all commands" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "app.commands.list_commands()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a new console" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "app.commands.execute('console:create', {\n", " 'insertMode': 'split-right',\n", " 'kernelPreference': {\n", " 'shutdownOnClose': True,\n", " }\n", "})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Change the theme" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "app.commands.execute('apputils:change-theme', { 'theme': 'JupyterLab Dark' })" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a new terminal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "app.commands.execute('terminal:create-new')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ready event\n", "\n", "Some functionalities might require the `JupyterFrontEnd` widget to be ready on the frontend first.\n", "\n", "This is for example the case when listing all the available commands, or retrieving the version with `app.version`.\n", "\n", "The `on_ready` method can be used to register a callback that will be fired when the frontend is ready." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipylab import JupyterFrontEnd\n", "from ipywidgets import Output\n", "\n", "app = JupyterFrontEnd()\n", "out = Output()\n", "\n", "def init():\n", " # show the first 5 commands\n", " cmds = app.commands.list_commands()[:5]\n", " out.append_stdout(cmds)\n", "\n", "app.on_ready(init)\n", "out" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or using `asyncio`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import asyncio\n", "\n", "app = JupyterFrontEnd()\n", "\n", "out = Output()\n", "\n", "async def init():\n", " await app.ready()\n", " cmds = app.commands.list_commands()[:5]\n", " out.append_stdout(cmds)\n", "\n", "asyncio.create_task(init())\n", "out" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add your own command\n", "\n", "Let's create a nice plot with `bqlot` and generate some random data.\n", "\n", "See https://github.com/bqplot/bqplot/blob/master/examples/Advanced%20Plotting/Animations.ipynb for more details." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "from bqplot import LinearScale, Lines, Bars, Axis, Figure\n", "from ipywidgets import IntSlider" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xs = LinearScale()\n", "ys1 = LinearScale()\n", "ys2 = LinearScale()\n", "\n", "x = np.arange(20)\n", "y = np.cumsum(np.random.randn(20))\n", "y1 = np.random.rand(20)\n", "\n", "line = Lines(x=x, y=y, scales={'x': xs, 'y': ys1}, colors=['magenta'], marker='square')\n", "bar = Bars(x=x, y=y1, scales={'x': xs, 'y': ys2}, colorpadding=0.2, colors=['steelblue'])\n", "\n", "xax = Axis(scale=xs, label='x', grid_lines='solid')\n", "yax1 = Axis(scale=ys1, orientation='vertical', tick_format='0.1f', label='y', grid_lines='solid')\n", "yax2 = Axis(scale=ys2, orientation='vertical', side='right', tick_format='0.0%', label='y1', grid_lines='none')\n", "\n", "Figure(marks=[bar, line], axes=[xax, yax1, yax2], animation_duration=1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now define a function to update the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def update_data():\n", " line.y = np.cumsum(np.random.randn(20))\n", " bar.y = np.random.rand(20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "update_data()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function will now be called when the JupyterLab command is executed.\n", "\n", "> Commands can also custom [icons](./icons.ipynb) in place of `icon_class`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "app.commands.add_command('update_data', execute=update_data, label=\"Update Data\", icon_class=\"jp-PythonIcon\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Execute it!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "app.commands.execute('update_data')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The slider should now be moving and taking random values.\n", "\n", "Also the list of commands gets updated with the newly added command:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "assert 'random' in app.commands.list_commands()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's great, but the command doesn't visually show up in the palette yet. So let's add it!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add the command to the palette" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipylab.commands import CommandPalette" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "palette = CommandPalette()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "palette.add_item('update_data', 'Python Commands')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open the command palette on the left side and the command should show now be visible." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Remove a command\n", "\n", "To remove a command that was previously added:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "app.commands.remove_command('update_data')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }