Skip to content

Commit

Permalink
version 2.0.0 for jupyterlab 4
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Oct 25, 2023
1 parent 715c30f commit b625a33
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 69 deletions.
87 changes: 42 additions & 45 deletions examples/test_trame_jupyter_comm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "fb77731b-bdb5-4df4-88ff-0d7c11e77a4d",
"execution_count": null,
"id": "5bbfe449-4bd7-497f-8ba7-d93f32f06b32",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ[\"TRAME_DISABLE_V3_WARNING\"] = \"1\"\n",
"os.environ[\"TRAME_IFRAME_BUILDER\"] = \"jupyter-extension\"\n",
"os.environ[\"TRAME_BACKEND\"] = \"jupyter\""
"\n",
"for key in os.environ:\n",
" if \"TRAME_\" in key:\n",
" print(f\"{key}={os.environ[key]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb77731b-bdb5-4df4-88ff-0d7c11e77a4d",
"metadata": {},
"outputs": [],
"source": [
"# import os\n",
"# os.environ[\"TRAME_DISABLE_V3_WARNING\"] = \"1\"\n",
"# os.environ[\"TRAME_IFRAME_BUILDER\"] = \"jupyter-extension\"\n",
"# os.environ[\"TRAME_BACKEND\"] = \"jupyter\"\n",
"\n",
"os.environ[\"TRAME_IPYWIDGETS_DISABLE\"] = \"1\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "ce8d1a4b-7a08-4645-9c62-6d0acebce26d",
"metadata": {},
"outputs": [],
Expand All @@ -25,72 +41,42 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "1a884c45-a925-4bb9-97d6-dc8e2cde906a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"trame_trame__template_main\" src=\"/trame-jupyter-server/vue2/index.html?ui=main&server=trame&wsProxy&reconnect=auto\" data-kernel-id=\"b0b9aba5-3440-434e-89f7-49a39225d473\" style=\"border: none; width: 100%; height: 600px;\"></iframe>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"cone = Cone()\n",
"await cone.ui.ready\n",
"print(cone.ui._jupyter_content())\n",
"cone.ui"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"id": "9830c4d6-63aa-4049-ac47-a2b9508256ea",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"trame_trame__template_main\" src=\"/trame-jupyter-server/vue2/index.html?ui=main&server=trame&wsProxy&reconnect=auto\" data-kernel-id=\"b0b9aba5-3440-434e-89f7-49a39225d473\" style=\"height: 200px; width: 100%; border: none;\"></iframe>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"cone.ui.iframe_style = \"height: 200px; width: 100%; border: none;\"\n",
"cone.ui"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"id": "faf01f47-878d-44ad-ae2c-aa3c8d3952fd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"c2 = Cone(\"b\")\n",
"await c2.ui.ready"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"id": "333a3a94-1e35-44ae-ba7c-76661eb00402",
"metadata": {},
"outputs": [],
Expand All @@ -104,6 +90,17 @@
"id": "673a6706-b7b9-407a-aa63-46c664f131e0",
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
"widgets.IntSlider()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e51605f9-afee-4cf3-9bcd-ceefe8f9fb94",
"metadata": {},
"outputs": [],
"source": []
}
],
Expand All @@ -123,7 +120,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trame-jupyter-extension",
"version": "0.1.3",
"version": "2.0.0",
"description": "A JupyterLab extension for trame communication layer",
"keywords": [
"jupyter",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ classifiers = [
"Programming Language :: Python :: 3.11",
]
dependencies = [
"jupyterlab >=3.5,<5",
"jupyterlab >=4,<5",
"jupyter_server>=2.0.1,<3",
"trame>=3.2.7",
]
Expand Down
38 changes: 16 additions & 22 deletions src/active.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { JupyterFrontEnd } from '@jupyterlab/application';
import {
IKernelConnection,
IModel as IModelKernel
} from '@jupyterlab/services/lib/kernel/kernel';
import { IKernelConnection } from '@jupyterlab/services/lib/kernel/kernel';
import { URLExt } from '@jupyterlab/coreutils';
import { ServerConnection } from '@jupyterlab/services';

export class ActiveManager {
app: JupyterFrontEnd;
www: string;
sessionToKernel: Record<string, IModelKernel>;
endpoint: string;
kernels: Record<string, IKernelConnection>;

constructor(app: JupyterFrontEnd) {
this.app = app;
this.sessionToKernel = {};
this.kernels = {};
this.www = '';
this.endpoint = '/trame-jupyter-server';
this.updateExtensionLocation();
}

Expand All @@ -30,6 +27,7 @@ export class ActiveManager {

async updateExtensionLocation() {
const settings = ServerConnection.makeSettings();
this.endpoint = URLExt.join(settings.baseUrl, 'trame-jupyter-server');
const requestUrl = URLExt.join(
settings.baseUrl,
'trame-jupyter-server',
Expand Down Expand Up @@ -59,14 +57,24 @@ export class ActiveManager {
this.finishInitialization();
}

getKernelCode(): string {
return `
import os
os.environ["TRAME_DISABLE_V3_WARNING"] = "1"
os.environ["TRAME_IFRAME_BUILDER"] = "jupyter-extension"
os.environ["TRAME_BACKEND"] = "jupyter"
os.environ["TRAME_JUPYTER_WWW"] = "${this.www}"
os.environ["TRAME_JUPYTER_ENDPOINT"] = "${this.endpoint}"
`;
}

updateSessionMapping() {
const runningSessions = this.app.serviceManager.sessions.running();
let entry = null;
do {
entry = runningSessions.next();
const session = entry.value;
if (session?.kernel) {
this.sessionToKernel[session.name] = session.kernel;
const kernelId = session.kernel.id;

// Create a connection by default and set ENV on kernel
Expand All @@ -79,27 +87,13 @@ export class ActiveManager {

kernelConnection.requestExecute({
silent: true,
code: `
import os
os.environ["TRAME_DISABLE_V3_WARNING"] = "1"
os.environ["TRAME_IFRAME_BUILDER"] = "jupyter-extension"
os.environ["TRAME_BACKEND"] = "jupyter"
os.environ["TRAME_JUPYTER_EXTENSION"] = "${this.www}"
`
code: this.getKernelCode(),
});
}
}
} while (!entry.done);
}

getActiveKernel(): IModelKernel | null {
const activeName = window.location.pathname.split('/').at(-1);
if (activeName) {
return this.sessionToKernel[activeName];
}
return null;
}

getKernelConnection(kernelId: string): IKernelConnection | null {
return this.kernels[kernelId];
}
Expand Down

0 comments on commit b625a33

Please sign in to comment.