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 ddd29cf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 27 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
77 changes: 50 additions & 27 deletions packages/notebook/src/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ import {
isRawCellModel,
MarkdownCell
} from '@jupyterlab/cells';
import { signalToPromise } from '@jupyterlab/coreutils';
import { PageConfig, URLExt, signalToPromise } from '@jupyterlab/coreutils';
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 +2480,56 @@ 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, document_id: documentId});
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 ddd29cf

Please sign in to comment.