Repository navigation
Closed
Description
We introduced a proposed API for Notebook but currently the API is majorly two managed object: NotebookDocument and NotebookCell. We create them for extensions and listen to their properties changes. However this doesn't follow the principal of TextEditor/Document where TextDocument is always readonly and TextEditor is the API for applying changes to the document.
If we try to follow TextEditor/Document, the API can be shaped as below
export interface NotebookEditor {
readonly document: NotebookDocument;
viewColumn?: ViewColumn;
/**
* Fired when the output hosting webview posts a message.
*/
readonly onDidReceiveMessage: Event<any>;
/**
* Post a message to the output hosting webview.
*
* Messages are only delivered if the editor is live.
*
* @param message Body of the message. This must be a string or other json serilizable object.
*/
postMessage(message: any): Thenable<boolean>;
/**
* Create a notebook cell. The cell is not inserted into current document when created. Extensions should insert the cell into the document by [TextDocument.cells](#TextDocument.cells)
*/
createCell(content: string, language: string, type: CellKind, outputs: CellOutput[], metadata: NotebookCellMetadata): NotebookCell;
/**
* Insert/Delete cells from the document
*/
spliceCells(index: number, deleteCnt: number, insertedCells: NotebookCell[]): Promise<void>;
/**
* Make changes to individual cells
*/
applyCellEdits(cell: NotebookCell, changes: { language?: string, outputs?: CellOutput[], metadata?: NotebookCellMetadata }): Promise<void>;
}
export interface NotebookDocument {
readonly uri: Uri;
readonly fileName: string;
readonly isDirty: boolean;
readonly languages: string[];
readonly cells: NotebookCell[];
readonly displayOrder?: GlobPattern[];
readonly metadata?: NotebookDocumentMetadata;
}
export interface NotebookCell {
readonly uri: Uri;
readonly handle: number;
readonly language: string;
readonly cellKind: CellKind;
readonly outputs: CellOutput[];
readonly metadata?: NotebookCellMetadata;
getContent(): string;
}Reacted by Mathias Soeken, JeffreyCA, TylerLeonhardt and Kun Ren
Activity
roblourens commentedon Mar 24, 2020
Does the edit API need to support reordering? Like could there any transient state in webview cell outputs that would not be represented in
CellOutput?roblourens commentedon Mar 24, 2020
If I set
changes.metadatato something, am I overwriting all ofmetadataor merging in the properties that are set?And I think
metadatashould be non-optional, even if all its properties are, it makes lots of things simpler.rebornix commentedon Mar 24, 2020
Extensions already use
spliceto do reordering sinceCellare created throughNotebookEditor.createCellsoCells are managed objects. We need to make sure they are not disposed duringsplice.This should probably be done by output in the webview sending its state through webview api to extensions.
It should be override IMHO.
I agree making metadata non-optional is clear to both us (for implementation) and extensions author (maybe) but it will make cell-default-metadata on document impossible, unless we use
inherit | enable | disableenum instead ofboolean.jrieken commentedon Mar 24, 2020
We could make this even more similar to
TextEditor#editby also having an edit-callback which in the only place to create new cell, delete cell etc. Something like thisroblourens commentedon Mar 24, 2020
Right, I mean having metadata non-optional but its properties can be optional, where
undefinedmeans "inherit"rebornix commentedon Mar 27, 2020
Current state of the Notebook API: we added Document Edit API and make
document.cellsreadonly. All changes to the cells should be performed through the Edit APIThe renderer API is also simplified as
Note that we didn't pass
NotebookEditorto renderers, instead they can only get theNotebookDocument. This is because The same document can be opened in two editors but the document is shared. My current thinking is custom renderers don't care about which editor the transformed output (html) will be rendered in and it also doesn't talk with the output through webview api because it doesn't know which editor the document will be loaded in.connor4312 commentedon Mar 27, 2020
When implementing it the NotebookOutputRenderer felt a little awkward. The types indicated that I need check the output kind and check that rich output had data matching the mime type I care about... and throw an error if it's not right? Or return an empty string? Hard to know. It's also a little confusing to me because it seems like
renderwill only be called for my own mime types, not CellStreamOutput/CellStreamInput. This was my boilerplate:Maybe the NotebookOutputRenderer's render function could be
render(document: NotebookDocument, source: string, mimeType: string): CellOutput, where vscode would only call the renderer and pass in the source when the cell mime type matches. I also like the idea proposed of returning a CellOutputKind--gives me an easy way to deal with errors and higher-order renderers might be interesting.120 remaining items
jrieken commentedon Jun 16, 2021
Please speak up if you are using the
vscode.notebook.createConcatTextDocument()-API. No customer is known to us and it is scheduled for removal: #126485rchiodo commentedon Jun 16, 2021
We use it in the python extension to support concating cells for intellisense.
Here exactly:
https://github.com/microsoft/vscode-python/blob/3698950c97982f31bb9dbfc19c4cd8308acda284/src/client/jupyter/languageserver/notebookConcatDocument.ts#L66
jrieken commentedon Jun 16, 2021
Ok, good to hear. In that case, I don't remove it ;-)
connor4312 commentedon Jul 21, 2021
Note that the order of arguments in
NotebookRendererMessaging.postMessagehas changed. There is a back-compat patch in place for the moment:90aa979#diff-f127724f8c5dbf0c8371ad0a100f8a9bc0a398b6b8ec29aa6cd7f265bd01a096R11580
jrieken commentedon Mar 16, 2022
🔈 events concerning the notebook documents are moving into its own, consolidated proposal: #144662 (comment). As of today both variants are supported but extensions are asked to migrate onto the
notebookDocumentEvents-proposal ASAPjrieken commentedon Mar 24, 2022
FYI that with 1.67(-insiders) the deprecated events from the
notebookEditor-proposed are being removed. Instead the newnotebookDocumentEventsproposal should be usedDonJayamanne commentedon Mar 29, 2022
Johannes Rieken (@jrieken) the new API has been adopted in the Jupyter extension
mjbvz commentedon Aug 17, 2022
Closing this in favor since we've finalized a good chunk of the notebook API. Will keep following up the remaining notebook proposals in individual issues