How to create cell with custom text in JupyterLab extension?

I created simple extension project for JupyterLab extension. I’m loading it in Jupyter Notebook 7.

My aim is for extension to insert a cell to the start of the notebook and set its content to a custom text.

I can create the cell and insert it into desired location. However, I’m struggling to find out how to set cell value.

I found example that does it like this:

    const current = notebookTracker.currentWidget;
    const notebook = current.content;
    NotebookActions.insertBelow(notebook);
    const activeCell = notebook.activeCell;
    activeCell.model.value.text = "my content";

In my case, I get typescript error that says that there’s no “value” on model. If I try to type it to “any”, I get error that value is undefined when the code tries to set its text so typings do not lie.

I figured it out. The last row should be the following in JupyterLab 4:

  activeCell.model.sharedModel.setSource("my content");
3 Likes