# How to programmatically add serveral new cells in a notebook in JupyterLab

**URL:** <https://discourse.jupyter.org/t/how-to-programmatically-add-serveral-new-cells-in-a-notebook-in-jupyterlab/4323>\
**Category:** JupyterLab\
**Created:** [May 9, 2020, 10:43am UTC](https://discourse.jupyter.org/t/how-to-programmatically-add-serveral-new-cells-in-a-notebook-in-jupyterlab/4323 "2020-05-09T10:43:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PhilippZman](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@PhilippZman](https://discourse.jupyter.org/u/PhilippZman)\
**Post date:** [May 9, 2020, 10:43am UTC](https://discourse.jupyter.org/t/how-to-programmatically-add-serveral-new-cells-in-a-notebook-in-jupyterlab/4323/1 "2020-05-09T10:43:21Z")

</div>

I want to programmatically create several cells in a Jupyter notebook in JupyterLab.  
The idea is that a user can select some experimental data and depending on the selection e.g. serveral plots will be added to the notebook (so that the user can manually change e.g. fit parameters …).  
Right now I am using this code to add cells and it is working fine in a classic Jupyter notebook, but it is only creating one cell in JupyterLab (2.1.2):

```
def create_new_cell(contents):
    from IPython.core.getipython import get_ipython
    shell = get_ipython()
    payload = dict(
        source='set_next_input',
        text=contents,
        replace=False,
    )
    shell.payload_manager.write_payload(payload, single = False)

```

> for x in [‘a’, ‘b’, ‘c’]:  
> create\_new\_cell(x)

Does anybody have any suggestions what a solution might be?  
Is there also a way to add markdown cells to the current Notebook in JupyterLab?  
`

---

<div class="post-metadata">

**Author:** ![1kastner](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/1kastner/32/1537_2.png) [@1kastner](https://discourse.jupyter.org/u/1kastner)\
**Post date:** [May 9, 2020, 12:18pm UTC](https://discourse.jupyter.org/t/how-to-programmatically-add-serveral-new-cells-in-a-notebook-in-jupyterlab/4323/2 "2020-05-09T12:18:22Z")

</div>

Is using [Jupyter Widgets](https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Basics.html) in combination with invoking the [display function](https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html) manually an option for you? That way you only have one cell but you can add user interaction as well.

---

<div class="post-metadata">

**Author:** ![PhilippZman](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@PhilippZman](https://discourse.jupyter.org/u/PhilippZman)\
**Post date:** [May 9, 2020, 1:14pm UTC](https://discourse.jupyter.org/t/how-to-programmatically-add-serveral-new-cells-in-a-notebook-in-jupyterlab/4323/3 "2020-05-09T13:14:52Z")

</div>

This is not really an option for me. Jupyter widgets is what I am using in the first part, so that the user can interactively select the data to analyze.  
After the first selection I have different steps (depending on the problem) with quite some plots and fits -\> and I want to use the Jupyter Notebook as an interactive way of analyzing the data.  
The idea is that a user can just go through it (without any knowledge of how the functions are called) and adjust parameters and see the feedback directly in the Juypter notebook, when running the cell. Later the notebook is just saved and the user can check the analysis again (as the plots are also saved in the notebook).

---

<div class="post-metadata">

**Author:** ![jasongrout](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/jasongrout/32/371_2.png) [@jasongrout](https://discourse.jupyter.org/u/jasongrout)\
**Post date:** [May 10, 2020, 6:08am UTC](https://discourse.jupyter.org/t/how-to-programmatically-add-serveral-new-cells-in-a-notebook-in-jupyterlab/4323/4 "2020-05-10T06:08:14Z")

</div>

Looking at the code, it does seem like only the first `set_next_input` payload is used: [https://github.com/jupyterlab/jupyterlab/blob/5af220eeeda3cb02eb4d596888f9a6d53e16c45a/packages/notebook/src/actions.tsx#L1630-L1632](https://github.com/jupyterlab/jupyterlab/blob/5af220eeeda3cb02eb4d596888f9a6d53e16c45a/packages/notebook/src/actions.tsx#L1630-L1632)

---

<div class="post-metadata">

**Author:** ![1kastner](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/1kastner/32/1537_2.png) [@1kastner](https://discourse.jupyter.org/u/1kastner)\
**Post date:** [May 10, 2020, 6:52am UTC](https://discourse.jupyter.org/t/how-to-programmatically-add-serveral-new-cells-in-a-notebook-in-jupyterlab/4323/5 "2020-05-10T06:52:05Z")

</div>

Second piece of information is that according to the doc string payloads are deprecated. If you, @PhilippZman, want to build a long-lasting solution maybe the [yet-to-be-implemented solution](https://jupyter-client.readthedocs.io/en/latest/messaging.html#payloads-deprecated) is the better way to go? Of course at this point it depends on your business requirements.

---

<div class="post-metadata">

**Author:** ![PhilippZman](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@PhilippZman](https://discourse.jupyter.org/u/PhilippZman)\
**Post date:** [May 14, 2020, 7:37am UTC](https://discourse.jupyter.org/t/how-to-programmatically-add-serveral-new-cells-in-a-notebook-in-jupyterlab/4323/6 "2020-05-14T07:37:39Z")

</div>

Ok thank you for the information.  
I guess there is currently no other way implemented to add cells in the current workbook from a python script in Jupyter Lab?  
As I understand the Javascript option like for cell execution and so on like:

```auto
from IPython.display import Javascript
display(Javascript('IPython.notebook.execute_cells_above()'))

```

is taken out of JupyterLab on purpose right?  
[https://github.com/jupyterlab/jupyterlab/pull/6596/commits/ece554e97ae3bc2e6d048a1bef9ecaf90b36d8ed](https://github.com/jupyterlab/jupyterlab/pull/6596/commits/ece554e97ae3bc2e6d048a1bef9ecaf90b36d8ed)

---

<div class="post-metadata">

**Author:** ![jasongrout](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/jasongrout/32/371_2.png) [@jasongrout](https://discourse.jupyter.org/u/jasongrout)\
**Post date:** [May 18, 2020, 6:16pm UTC](https://discourse.jupyter.org/t/how-to-programmatically-add-serveral-new-cells-in-a-notebook-in-jupyterlab/4323/7 "2020-05-18T18:16:37Z")

</div>

> [@PhilippZman](#):
>
> is taken out of JupyterLab on purpose right?

More like never implemented - as mentioned in [the docs](https://jupyterlab.readthedocs.io/en/stable/getting_started/faq.html#development), JLab javascript is a different architecture with different constraints and capabilities compared to the classic Jupyter Notebook.
