Skip to content

Commit

Permalink
Support server-side execution
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Nov 28, 2023
1 parent 7523d3a commit 21cc49a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 28 deletions.
10 changes: 10 additions & 0 deletions jupyterlab/labapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,10 @@ class LabApp(NotebookConfigShimMixin, LabServerApp):
This flag is now deprecated and will be removed in JupyterLab v5.""",
)
flags["server-side-execution"] = (
{"LabApp": {"server-side-execution": True}},
"""Whether to execute notebooks in the server using the REST API, not using the kernel protocol over WebSocket. The frontend only interacts with the notebook through its shared model. This execution mode requires jupyter-collaboration.""",
)

subcommands = {
"build": (LabBuildApp, LabBuildApp.description.splitlines()[0]),
Expand Down Expand Up @@ -591,6 +595,12 @@ class LabApp(NotebookConfigShimMixin, LabServerApp):
This flag is now deprecated and will be removed in JupyterLab v5.""",
)

server_side_execution = Bool(
False,
config=True,
help="""Whether to execute notebooks in the server using the REST API, not using the kernel protocol over WebSocket. The frontend only interacts with the notebook through its shared model. This execution mode requires jupyter-collaboration.""",
)

news_url = Unicode(
"https://jupyterlab.github.io/assets/feed.xml",
allow_none=True,
Expand Down
79 changes: 51 additions & 28 deletions packages/notebook/src/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import {
isRawCellModel,
MarkdownCell
} from '@jupyterlab/cells';
import { signalToPromise } from '@jupyterlab/coreutils';
import { PageConfig, URLExt, signalToPromise } from '@jupyterlab/coreutils';

Check failure on line 20 in packages/notebook/src/actions.tsx

View workflow job for this annotation

GitHub Actions / Linux (lint, 3.11)

Member 'signalToPromise' of the import declaration should be sorted alphabetically
import * as nbformat from '@jupyterlab/nbformat';
import { KernelMessage } from '@jupyterlab/services';
import { KernelMessage, ServerConnection } from '@jupyterlab/services';
import { ISharedAttachmentsCell } from '@jupyter/ydoc';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import { every, findIndex } from '@lumino/algorithm';
Expand Down Expand Up @@ -2477,36 +2477,59 @@ namespace Private {

let ran = false;
try {
const reply = await CodeCell.execute(
cell as CodeCell,
sessionContext,
{
deletedCells,
recordTiming: notebook.notebookConfig.recordTiming
}
);
deletedCells.splice(0, deletedCells.length);

ran = (() => {
if (cell.isDisposed) {
return false;
}
if (PageConfig.getOption('serverSideExecution') === 'true') {
const kernelId = sessionContext.session?.kernel?.id;
const settings = ServerConnection.makeSettings();
const apiURL = URLExt.join(
settings.baseUrl,
`api/kernels/${kernelId}/execute`
);
const cellId = cell.model.sharedModel.getId();
const documentId = `json:notebook:${notebook.model?.sharedModel.getState(
'file_id'
)}`;
const body = JSON.stringify({
cell_id: cellId,

Check failure on line 2492 in packages/notebook/src/actions.tsx

View workflow job for this annotation

GitHub Actions / Linux (lint, 3.11)

Identifier 'cell_id' is not in camel case
document_id: documentId

Check failure on line 2493 in packages/notebook/src/actions.tsx

View workflow job for this annotation

GitHub Actions / Linux (lint, 3.11)

Identifier 'document_id' is not in camel case
});
const init = {
method: 'POST',
body
};
ServerConnection.makeRequest(apiURL, init, settings);
ran = true;
} else {
const reply = await CodeCell.execute(
cell as CodeCell,
sessionContext,
{
deletedCells,
recordTiming: notebook.notebookConfig.recordTiming
}
);
deletedCells.splice(0, deletedCells.length);

if (!reply) {
return true;
}
if (reply.content.status === 'ok') {
const content = reply.content;
ran = (() => {
if (cell.isDisposed) {
return false;
}

if (content.payload && content.payload.length) {
handlePayload(content, notebook, cell);
if (!reply) {
return true;
}
if (reply.content.status === 'ok') {
const content = reply.content;

return true;
} else {
throw new KernelError(reply.content);
}
})();
if (content.payload && content.payload.length) {
handlePayload(content, notebook, cell);
}

return true;
} else {
throw new KernelError(reply.content);
}
})();
}
} catch (reason) {
if (cell.isDisposed || reason.message.startsWith('Canceled')) {
ran = false;
Expand Down

0 comments on commit 21cc49a

Please sign in to comment.