# Dynamically updating codecell contents

**URL:** <https://discourse.jupyter.org/t/dynamically-updating-codecell-contents/22135>\
**Category:** extensions\
**Created:** [October 27, 2023, 9:27pm UTC](https://discourse.jupyter.org/t/dynamically-updating-codecell-contents/22135 "2023-10-27T21:27:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gplasky](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/gplasky/32/11446_2.png) [@gplasky](https://discourse.jupyter.org/u/gplasky)\
**Post date:** [October 27, 2023, 9:27pm UTC](https://discourse.jupyter.org/t/dynamically-updating-codecell-contents/22135/1 "2023-10-27T21:27:44Z")

</div>

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”.

```auto
    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.

```auto
    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?

---

<div class="post-metadata">

**Author:** ![krassowski](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/krassowski/32/1685_2.png) [@krassowski](https://discourse.jupyter.org/u/krassowski)\
**Post date:** [October 28, 2023, 11:34am UTC](https://discourse.jupyter.org/t/dynamically-updating-codecell-contents/22135/2 "2023-10-28T11:34:50Z")

</div>

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

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

```

and

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

```

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

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

```

?

---

<div class="post-metadata">

**Author:** ![gplasky](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/gplasky/32/11446_2.png) [@gplasky](https://discourse.jupyter.org/u/gplasky)\
**Post date:** [November 19, 2023, 8:05pm UTC](https://discourse.jupyter.org/t/dynamically-updating-codecell-contents/22135/3 "2023-11-19T20:05:26Z")

</div>

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. 🙂
