Skip to content

Commit

Permalink
JupyterFrontEnd - added exec_eval (execEval) for executing code in an…
Browse files Browse the repository at this point in the history
…other kernel via the model in that kernel.

renamed `execute_method` to `executeMethod`
schedule_operation renamed to scheduleOperation
  • Loading branch information
Alan Fleming committed Feb 26, 2024
1 parent 9b77b1c commit aa8d339
Show file tree
Hide file tree
Showing 21 changed files with 304 additions and 148 deletions.
49 changes: 36 additions & 13 deletions examples/autostart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@
"source": [
"# @my_module.autostart.py\n",
"\n",
"import asyncio\n",
"\n",
"import ipylab\n",
"\n",
"app = ipylab.JupyterFrontEnd()\n",
"\n",
"n = 0\n",
"\n",
"\n",
"async def create_app():\n",
" # The code in this function is called in the new kernel (session).\n",
Expand All @@ -85,9 +77,9 @@
" import ipylab\n",
"\n",
" ma = ipylab.MainArea(name=\"My demo app\")\n",
" await ma.wait_ready()\n",
" ma.content.title.label = \"Simple app\"\n",
" ma.content.title.caption = ma.kernelId\n",
" await ma.load()\n",
" console_button = ipw.Button(description=\"Toggle console\")\n",
" error_button = ipw.Button(\n",
" description=\"Do an error\",\n",
Expand All @@ -97,7 +89,7 @@
" console_button.on_click(\n",
" lambda b: ma.load_console() if ma.console_status == \"unloaded\" else ma.unload_console()\n",
" )\n",
" error_button.on_click(lambda b: ma.execute_command(\"Not a command\"))\n",
" error_button.on_click(lambda b: ma.executeCommand(\"Not a command\"))\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",
Expand All @@ -118,12 +110,27 @@
"\n",
" # Register plugin for this kernel.\n",
" ipylab.hookspecs.pm.register(IpylabPlugins())\n",
" await ma.load()\n",
"\n",
"\n",
"import asyncio\n",
"\n",
"import ipylab\n",
"\n",
"n = 0\n",
"app = ipylab.JupyterFrontEnd()\n",
"\n",
"def start_my_app(cwd):\n",
"\n",
"async def start_my_app(cwd):\n",
" global n\n",
" n += 1\n",
" app.newSession(path=f\"my app {n}\", code=create_app)\n",
" task = app.execEval(\n",
" code=create_app, user_expressions={\"main_area_widget\": \"create_app()\"}, path=f\"my app {n}\"\n",
" )\n",
" if app.current_widget_id.startswith(\"launcher\"):\n",
" await app.executeMethod(\"app.shell.currentWidget.dispose\")\n",
" return app\n",
" return await task\n",
"\n",
"\n",
"async def register_commands():\n",
Expand All @@ -150,15 +157,31 @@
"t.result()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"app = ipylab.JupyterFrontEnd()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# There is a new launcher called 'Start custom app'\n",
"t = app.execute_command(\"launcher:create\")"
"t = app.executeCommand(\"launcher:create\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
12 changes: 6 additions & 6 deletions examples/commands.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"metadata": {},
"outputs": [],
"source": [
"t = app.execute_command(\n",
"t = app.executeCommand(\n",
" \"console:create\",\n",
" insertMode=\"split-right\",\n",
" kernelPreference={\"id\": app.kernelId},\n",
Expand Down Expand Up @@ -104,7 +104,7 @@
"metadata": {},
"outputs": [],
"source": [
"app.execute_command(\"apputils:change-theme\", theme=\"JupyterLab Dark\")"
"app.executeCommand(\"apputils:change-theme\", theme=\"JupyterLab Dark\")"
]
},
{
Expand All @@ -113,7 +113,7 @@
"metadata": {},
"outputs": [],
"source": [
"t = app.execute_command(\"apputils:change-theme\", theme=\"JupyterLab Light\")"
"t = app.executeCommand(\"apputils:change-theme\", theme=\"JupyterLab Light\")"
]
},
{
Expand All @@ -138,7 +138,7 @@
"metadata": {},
"outputs": [],
"source": [
"app.execute_command(\"terminal:create-new\")\n",
"app.executeCommand(\"terminal:create-new\")\n",
"# It can take up to 10 seconds for the terminal to show up after the cell returns."
]
},
Expand Down Expand Up @@ -229,7 +229,7 @@
"metadata": {},
"outputs": [],
"source": [
"t = app.execute_command(\"swap_orientation\")"
"t = app.executeCommand(\"swap_orientation\", transform=ipylab.TransformMode.raw)"
]
},
{
Expand Down Expand Up @@ -345,7 +345,7 @@
"metadata": {},
"outputs": [],
"source": [
"app.command.remove_command(\"swap_orientation\")"
"app.command.removePythonCommand(\"swap_orientation\")"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions examples/generic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"In cases where a method hasn't been implemented in the Python `JupyterFrontEnd`, it may be possible to perform the operation using one of the generic functions.\n",
"\n",
"* schedule_operation('FE_execute', ...)\n",
"* execute_method\n",
"* executeMethod\n",
"* listAttributes - to list available attributes\n",
"* listMethods (listAttributes tweaked to list only the methods)\n",
"* get_attribute"
Expand Down Expand Up @@ -67,7 +67,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Some of the methods above have already been implemented in 'app.shell' on the Python object. And if you look at the code you'll see these methods use the *generic* method (technically, it is a subclass which inserts `'shell.'` infront of the path in the method `execute_method` to match it's level in the JupyterFrontend Object. So basically the same function call is sent to the frontend.\n",
"Some of the methods above have already been implemented in 'app.shell' on the Python object. And if you look at the code you'll see these methods use the *generic* method (technically, it is a subclass which inserts `'shell.'` infront of the path in the method `executeMethod` to match it's level in the JupyterFrontend Object. So basically the same function call is sent to the frontend.\n",
"\n",
"In case you're interested, the custom part of message sent to the frontend looks like this:\n",
"\n",
Expand All @@ -76,7 +76,7 @@
" \"ipylab_BE\": \"83a513b1-0ab3-443f-9521-b740be0b8cd3, # unique per message\n",
" \"operation\": \"FE_execute\",\n",
" \"kwgs\": {\n",
" \"FE_execute\": {\"mode\": \"execute_method\", \"kwgs\": {\"method\": \"listAttributes\"}},\n",
" \"FE_execute\": {\"mode\": \"executeMethod\", \"kwgs\": {\"method\": \"listAttributes\"}},\n",
" \"args\": (\"app.shell\", \"function\", 2),\n",
" },\n",
" \"transform\": \"raw\",\n",
Expand Down Expand Up @@ -123,7 +123,7 @@
"metadata": {},
"outputs": [],
"source": [
"t = app.execute_method(\"shell.collapseLeft\") # Generic"
"t = app.executeMethod(\"shell.collapseLeft\") # Generic"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/icons.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@
},
"outputs": [],
"source": [
"panel.app.execute_command(\"apputils:activate-command-palette\")"
"panel.app.executeCommand(\"apputils:activate-command-palette\")"
]
},
{
Expand All @@ -313,7 +313,7 @@
"metadata": {},
"outputs": [],
"source": [
"panel.app.execute_command(\"my-icon:randomize\", count=1)"
"panel.app.executeCommand(\"my-icon:randomize\", count=1)"
]
}
],
Expand Down
4 changes: 2 additions & 2 deletions examples/ipytree.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"When the \"Open\" button is clicked, we call `app.execute_command` with the path to the file to open it in the JupyterLab interface."
"When the \"Open\" button is clicked, we call `app.executeCommand` with the path to the file to open it in the JupyterLab interface."
]
},
{
Expand All @@ -328,7 +328,7 @@
" for node in file_tree.selected_nodes:\n",
" filepath = node.fullpath\n",
" if filepath:\n",
" app.execute_command(\"docmanager:open\", {\"path\": filepath})\n",
" app.executeCommand(\"docmanager:open\", {\"path\": filepath})\n",
"\n",
"\n",
"open_button.on_click(on_open_clicked)"
Expand Down
4 changes: 2 additions & 2 deletions examples/sessions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"metadata": {},
"outputs": [],
"source": [
"app.execute_command(\"console:create\", **app.current_session)"
"app.executeCommand(\"console:create\", **app.current_session)"
]
},
{
Expand All @@ -89,7 +89,7 @@
"metadata": {},
"outputs": [],
"source": [
"app.execute_command(\"notebook:create-console\")"
"app.executeCommand(\"notebook:create-console\")"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/widgets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
"metadata": {},
"outputs": [],
"source": [
"t = split_panel.app.execute_method(\"app.shell.expandRight\")"
"t = split_panel.app.executeMethod(\"app.shell.expandRight\")"
]
},
{
Expand Down
Loading

0 comments on commit aa8d339

Please sign in to comment.