Dynamically updating codecell contents

As a part of my extension, I am creating a CodeCell widget inside the widget itself (attached to the right pane). This code works fine to initialize the cell and set the contents to “Waiting for result”.

    const factoryService = new CodeMirrorEditorFactory({});
    this.codeCell = new CodeCell({
      contentFactory: new Cell.ContentFactory({
        editorFactory: factoryService.newInlineEditor.bind(factoryService)
      }),
      model: new CodeCellModel(),
      rendermime: new RenderMimeRegistry({ initialFactories }),
    }).initializeState();
...
    this.codeCell.model.sharedModel.setSource('Waiting for result');
    this.codeCell.model.sharedModel.setMetadata({ 'language': 'python' });
    this.codeCell.inputArea?.editorWidget?.model.sharedModel.setSource('Waiting for result');
    this.codeCell.activate;
    layout.addWidget(this.codeCell);
    this.outputGroup.append(this.codeCell.node);

Next, when I try to update the cell programmatically, the content is not changing.

    this.codeCell.inputArea?.editorWidget?.model.sharedModel.setSource(this._result);

this._result is set properly and contains the string I expect. No amount of .activate .update or other similar function calls that I’ve tried result in the new string being displayed in the cell. Any suggestions?

1 Like

It appears that revelant context might be missing in your question. It seems that you are mixing up two calls:

this.codeCell.model.sharedModel.setSource('Waiting for result');

and

this.codeCell.inputArea?.editorWidget?.model.sharedModel.setSource('Waiting for result');

Why are you trying to use the latter? Did you try just:

this.codeCell.model.sharedModel.setSource(this._result);

?

2 Likes

Yep, thanks for the help. I ended up arriving at that solution not long after I posted this. I did not have a good mental model of how a cell is built and my question was at about the third or fourth permutation of me trying setSource() in various places to no avail.

My brain might just be slightly broken but if not it might be helpful if we documented the hierarchy of these cells/widgets/etc. for future folks like me. :slight_smile:

1 Like