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

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

Is using Jupyter Widgets in combination with invoking the display function manually an option for you? That way you only have one cell but you can add user interaction as well.

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

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

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 is the better way to go? Of course at this point it depends on your business requirements.

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:

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

More like never implemented - as mentioned in the docs, JLab javascript is a different architecture with different constraints and capabilities compared to the classic Jupyter Notebook.

1 Like