Skip to content

Commit

Permalink
Change start_maybe to to_task and switch to 'console' type of ses…
Browse files Browse the repository at this point in the history
…sion. Drop injectCode.
  • Loading branch information
Alan Fleming committed Aug 22, 2024
1 parent 6f523ba commit 7d447d8
Show file tree
Hide file tree
Showing 20 changed files with 265 additions and 406 deletions.
93 changes: 51 additions & 42 deletions examples/autostart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@
"source": [
"# @my_module.autostart.py\n",
"\n",
"import asyncio\n",
"\n",
"import ipylab\n",
"\n",
"\n",
Expand All @@ -79,81 +77,65 @@
"\n",
" import ipylab\n",
"\n",
" global ma # noqa: PLW0603\n",
"\n",
" ma = ipylab.MainArea(name=\"My demo app\", path=path)\n",
" await ma.wait_ready()\n",
" ma.content.title.label = \"Simple app\"\n",
" ma.content.title.caption = ma.kernelId\n",
" console_button = ipw.Button(description=\"Toggle console\")\n",
" panel = ipylab.Panel()\n",
" panel.title.label = path\n",
" panel.title.caption = panel.app.kernelId\n",
" error_button = ipw.Button(\n",
" description=\"Do an error\",\n",
" tooltip=\"An error dialog will pop up when this is clicked.\\n\"\n",
" \"The dialog demonstrates the use of the `on_frontend_error` plugin.\",\n",
" )\n",
" console_button.on_click(\n",
" lambda _: ma.load_console(name=\"console\") if ma.console_status == \"unloaded\" else ma.unload_console()\n",
" )\n",
" error_button.on_click(lambda _: ma.execute_command(\"Not a command\"))\n",
" console_status = ipw.HTML()\n",
" ipw.dlink((ma, \"console_status\"), (console_status, \"value\"))\n",
" ma.content.children = [\n",
" ipw.HTML(f\"<h3>My simple app</h3> Welcome to my app.<br> kernel id: {ma.kernelId}\"),\n",
" ipw.HBox([console_button, error_button]),\n",
" console_status,\n",
" ]\n",
"\n",
" # Shutdown when MainArea is unloaded.\n",
" def on_status_change(change):\n",
" if change[\"new\"] == \"unloaded\":\n",
" ma.app.shutdownKernel()\n",
"\n",
" ma.observe(on_status_change, \"status\")\n",
" error_button.on_click(lambda _: panel.app.execute_command(\"Not a command\"))\n",
" panel.children = [ipw.HTML(f\"<h3>{path}</h3> Welcome to my app.<br> kernel id: {panel.app.kernelId}\"), error_button]\n",
"\n",
" # Demonstrate usage of a plugin\n",
" class IpylabPlugins:\n",
" # Define plugins (see IpylabHookspec for available hooks)\n",
" @ipylab.hookimpl\n",
" def on_frontend_error(self, obj, error, content): # noqa: ARG002\n",
" ma.app.dialog.show_error_message(\"Error\", str(error))\n",
" panel.app.dialog.show_error_message(\"Error\", str(error))\n",
"\n",
" # Register plugin for this kernel.\n",
" ipylab.hookspecs.pm.register(IpylabPlugins()) # type: ignore\n",
"\n",
" await ma.load()\n",
" return ipylab.pack(ma)\n",
" disposable = await panel.addToShell()\n",
" # Shutdown the kernel when the window is closed.\n",
" disposable.observe(lambda _: panel.app.shutdown_kernel(), names=\"comm\")\n",
"\n",
"\n",
"n = 0\n",
"app = ipylab.JupyterFrontEnd()\n",
"\n",
"\n",
"async def start_my_app(cwd): # noqa: ARG001\n",
" global n # noqa: PLW0603\n",
" n = getattr(start_my_app, \"callcount\", 0)\n",
" n += 1\n",
" start_my_app.callcount = n\n",
" path = f\"my app {n}\"\n",
" launcher_id = app.current_widget_id if app.current_widget_id.startswith(\"launcher\") else \"\"\n",
" await app.execEval(\n",
" code=create_app,\n",
" user_expressions={\"main_area_widget\": \"create_app(path)\"},\n",
" launcher = (\n",
" ipylab.DisposableConnection(id=app.current_widget_id) if app.current_widget_id.startswith(\"launcher\") else None\n",
" )\n",
"\n",
" main_area = await app.exec_eval(\n",
" execute=create_app,\n",
" evaluate={\"path\": f\"'{path}'\", \"payload\": \"create_app(path)\"},\n",
" path=path,\n",
" )\n",
" if launcher_id:\n",
" await app.execute_method(widget=launcher_id, method=\"dispose\")\n",
" if launcher:\n",
" launcher.dispose()\n",
" return main_area\n",
"\n",
"\n",
"async def register_commands():\n",
" await app.command.addPythonCommand(\n",
" \"start_my_app\",\n",
" execute=start_my_app,\n",
" label=\"Start Custom App\",\n",
" icon_class=\"jp-PythonIcon\",\n",
" \"start_my_app\", execute=start_my_app, label=\"Start Custom App\", icon_class=\"jp-PythonIcon\", just_coro=True\n",
" )\n",
" await app.launcher.add_item(\"start_my_app\", \"Ipylab\")\n",
" return \"done\"\n",
"\n",
"\n",
"ipylab_plugin = None\n",
"t = asyncio.create_task(register_commands())"
"t = app.to_task(register_commands())"
]
},
{
Expand Down Expand Up @@ -182,6 +164,33 @@
"source": [
"t = app.execute_command(\"launcher:create\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cmd = ipylab.DisposableConnection(id=\"start_my_app\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cmd.dispose()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"app.launcher.remove_item(\"start_my_app\", \"Ipylab\")"
]
}
],
"metadata": {
Expand Down
29 changes: 27 additions & 2 deletions examples/commands.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
" \"console:create\",\n",
" insertMode=\"split-right\",\n",
" kernelPreference={\"id\": app.kernelId},\n",
" transform={\"mode\": ipylab.TransformMode.attribute, \"parts\": [\"id\"]}, # optional\n",
" execute_kwgs={\"transform\": ipylab.TransformMode.connection}, # Optional\n",
")"
]
},
Expand All @@ -91,6 +91,24 @@
"t.result()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"w = t.result()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"w.dispose()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -359,6 +377,13 @@
"source": [
"assert \"swap_orientation\" not in app.command.commands # noqa: S101"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -377,7 +402,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
59 changes: 1 addition & 58 deletions examples/generic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as ipw\n",
"\n",
"import ipylab\n",
"\n",
"app = ipylab.JupyterFrontEnd()"
Expand Down Expand Up @@ -171,61 +169,6 @@
"source": [
"t = app.list_attributes(\"app.shell\", depth=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## IpyWidgets\n",
"\n",
"It is also possible to run the same methods of `JupyterFrontEnd` on widgets by using the argument `widget`. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"widget = ipw.HTML(\"<h3>Hello world</h3>\")\n",
"widget"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t = app.list_attributes(widget=widget)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.result()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t = app.get_attribute(\"views\", widget=widget)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.result()"
]
}
],
"metadata": {
Expand All @@ -244,7 +187,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
16 changes: 5 additions & 11 deletions examples/sessions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,7 @@
"metadata": {},
"outputs": [],
"source": [
"app.execute_command(\"notebook:create-console\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example: Create New Session (Python with comms)"
"t = app.execute_command(\"notebook:create-new\", execute_kwgs={\"transform\": ipylab.TransformMode.connection})"
]
},
{
Expand All @@ -105,7 +98,8 @@
"metadata": {},
"outputs": [],
"source": [
"t = app.newSession(\"second\", code=\"import ipylab;app = ipylab.JupyterFrontEnd()\")"
"w = t.result()\n",
"w"
]
},
{
Expand All @@ -114,7 +108,7 @@
"metadata": {},
"outputs": [],
"source": [
"t.result()"
"w.dispose()"
]
}
],
Expand All @@ -134,7 +128,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
5 changes: 4 additions & 1 deletion ipylab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"__version__",
"HasApp",
"JupyterFrontEnd",
"DisposableConnection",
"Panel",
"SplitPanel",
"MainArea",
Expand All @@ -18,9 +19,11 @@
"hookimpl",
"TransformMode",
"pack",
"pack_code",
]

from ipylab.asyncwidget import TransformMode, pack
from ipylab.asyncwidget import TransformMode, pack, pack_code
from ipylab.disposable_connection import DisposableConnection
from ipylab.hasapp import HasApp
from ipylab.hookspecs import hookimpl
from ipylab.jupyterfrontend import JupyterFrontEnd
Expand Down
Loading

0 comments on commit 7d447d8

Please sign in to comment.