Skip to content

📢 Notebook API announcements #93265

Closed
Closed

Description

@rebornix

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;
}

Activity

  1. roblourens commented on Mar 24, 2020

    @roblourens
    Member

    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?

  2. roblourens commented on Mar 24, 2020

    @roblourens
    Member

    If I set changes.metadata to something, am I overwriting all of metadata or merging in the properties that are set?

    And I think metadata should be non-optional, even if all its properties are, it makes lots of things simpler.

  3. rebornix commented on Mar 24, 2020

    @rebornix
    ContributorAuthor

    Does the edit API need to support reordering?

    Extensions already use splice to do reordering since Cell are created through NotebookEditor.createCell so Cells are managed objects. We need to make sure they are not disposed during splice.

    Like could there any transient state in webview cell outputs that would not be represented in CellOutput?

    This should probably be done by output in the webview sending its state through webview api to extensions.

    If I set changes.metadata to something, am I overwriting all of metadata or merging in the properties that are set?

    It should be override IMHO.

    And I think metadata should be non-optional, even if all its properties are, it makes lots of things simpler.

    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 | disable enum instead of boolean.

  4. jrieken commented on Mar 24, 2020

    @jrieken
    Contributor

    We could make this even more similar to TextEditor#edit by also having an edit-callback which in the only place to create new cell, delete cell etc. Something like this

    export interface NotebookEditorCellEdit {
    	insert(content: string, language: string, type: CellKind, outputs: CellOutput[], metadata: NotebookCellMetadata | undefined): void;
    	delete(cell: NotebookCell): void;
    	// more
    }
    
    export interface NotebookEditor {
    	readonly document: NotebookDocument;
    	viewColumn?: ViewColumn;
    
    	readonly onDidReceiveMessage: Event<any>;
    
    	postMessage(message: any): Thenable<boolean>;
    
    	editCells(callback: (cellEdit: NotebookEditorCellEdit) => any): any
    }
  5. roblourens commented on Mar 24, 2020

    @roblourens
    Member

    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 | disable enum instead of boolean.

    Right, I mean having metadata non-optional but its properties can be optional, where undefined means "inherit"

  6. rebornix commented on Mar 27, 2020

    @rebornix
    ContributorAuthor

    Current state of the Notebook API: we added Document Edit API and make document.cells readonly. All changes to the cells should be performed through the Edit API

    export interface NotebookEditorCellEdit {
        insert(index: number, content: string, language: string, type: CellKind, outputs: CellOutput[], metadata: NotebookCellMetadata | undefined): void;
        delete(index: number): void;
    }
    
    export interface NotebookEditor {
        readonly document: NotebookDocument;
        viewColumn?: ViewColumn;
        readonly onDidReceiveMessage: Event<any>;
        postMessage(message: any): Thenable<boolean>;
        edit(callback: (editBuilder: NotebookEditorCellEdit) => void): Thenable<boolean>;
    }

    The renderer API is also simplified as

    export interface NotebookOutputRenderer {
        render(document: NotebookDocument, output: CellOutput, mimeType: string): string;
        preloads?: Uri[];
    }

    Note that we didn't pass NotebookEditor to renderers, instead they can only get the NotebookDocument. 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.

  7. connor4312 commented on Mar 27, 2020

    @connor4312
    Member

    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 render will only be called for my own mime types, not CellStreamOutput/CellStreamInput. This was my boilerplate:

    render(document: vscode.NotebookDocument, output: vscode.CellOutput): string {
      if (output.outputKind !== vscode.CellOutputKind.Rich) {
        return '';
      }
    
      if (output.data[Constants.TableMimeType] === undefined) {
        return '';
      }
    
      // actually do stuff...
    }

    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.

  8. 120 remaining items

  9. jrieken commented on Jun 16, 2021

    @jrieken
    Contributor

    Please speak up if you are using the vscode.notebook.createConcatTextDocument()-API. No customer is known to us and it is scheduled for removal: #126485

  10. rchiodo commented on Jun 16, 2021

    @rchiodo
    Contributor

    Please speak up if you are using the vscode.notebook.createConcatTextDocument()-API. No customer is known to us and it is scheduled for removal: #126485

    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

  11. jrieken commented on Jun 16, 2021

    @jrieken
    Contributor

    Ok, good to hear. In that case, I don't remove it ;-)

  12. connor4312 commented on Jul 21, 2021

    @connor4312
    Member

    Note that the order of arguments in NotebookRendererMessaging.postMessage has changed. There is a back-compat patch in place for the moment:

    90aa979#diff-f127724f8c5dbf0c8371ad0a100f8a9bc0a398b6b8ec29aa6cd7f265bd01a096R11580

  13. jrieken commented on Mar 16, 2022

    @jrieken
    Contributor

    🔈 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 ASAP

  14. jrieken commented on Mar 24, 2022

    @jrieken
    Contributor

    FYI that with 1.67(-insiders) the deprecated events from the notebookEditor-proposed are being removed. Instead the new notebookDocumentEvents proposal should be used

  15. DonJayamanne commented on Mar 29, 2022

    @DonJayamanne
    Contributor

    Johannes Rieken (@jrieken) the new API has been adopted in the Jupyter extension

  16. mjbvz commented on Aug 17, 2022

    @mjbvz
    Contributor

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

apinotebooknotebook-apiplan-item

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions