# From external program, append cell, execute, and show output in a running Jupyter notebook

**URL:** <https://discourse.jupyter.org/t/from-external-program-append-cell-execute-and-show-output-in-a-running-jupyter-notebook/12983>\
**Category:** JupyterLab\
**Tags:** jupyterlab\
**Created:** [February 11, 2022, 12:56am UTC](https://discourse.jupyter.org/t/from-external-program-append-cell-execute-and-show-output-in-a-running-jupyter-notebook/12983 "2022-02-11T00:56:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hiro\_Protagonist](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/hiro_protagonist/32/5736_2.png) [@Hiro\_Protagonist](https://discourse.jupyter.org/u/Hiro_Protagonist)\
**Post date:** [February 11, 2022, 12:56am UTC](https://discourse.jupyter.org/t/from-external-program-append-cell-execute-and-show-output-in-a-running-jupyter-notebook/12983/1 "2022-02-11T00:56:47Z")

</div>

My desired outcome is a jupyter notebook running in the browser.  
From another program (say a local editor) that has access to the kernel/back end of notebook, I would like to be able to call an api to append a cell with specified contents, execute the cell, and show the output.

The examples for programmatically doing seem to require running in the notebook itself. I would like to be able to just send code to be executed to the kernel, and have it output the result in the already running jupyter notebook. This should not require refreshing the page or anything like that, ideally.

---

<div class="post-metadata">

**Author:** ![bollwyvl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/bollwyvl/32/904_2.png) [@bollwyvl](https://discourse.jupyter.org/u/bollwyvl)\
**Post date:** [February 11, 2022, 2:24am UTC](https://discourse.jupyter.org/t/from-external-program-append-cell-execute-and-show-output-in-a-running-jupyter-notebook/12983/2 "2022-02-11T02:24:55Z")

</div>

This might be a good post to peruse:

> [@External API calls to trigger JupyterLab features](https://discourse.jupyter.org/t/external-api-calls-to-trigger-jupyterlab-features/12425):
>
> Hi, I am currently working on integrating JupyterLab to our internal software ecosystem and I have a requirement where I need to use an external API call, made by one of our internal services to open a notebook within JupyterLab and populate it with specific content. Does anyone know if this kind of feature is supported? i.e. the UI interaction is outside the realm of the JupyterLab UI. I have looked into some online material on using event handlers for this purpose, but I still don’t know how …

tl: dr; this is not (and probably won’t be) a built-in feature, due to security considerations, but would make a _fine_ discretionary plugin.

---

<div class="post-metadata">

**Author:** ![Hiro\_Protagonist](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/hiro_protagonist/32/5736_2.png) [@Hiro\_Protagonist](https://discourse.jupyter.org/u/Hiro_Protagonist)\
**Post date:** [February 12, 2022, 8:42am UTC](https://discourse.jupyter.org/t/from-external-program-append-cell-execute-and-show-output-in-a-running-jupyter-notebook/12983/3 "2022-02-12T08:42:15Z")

</div>

I am going to test this, but I found:

> <https://github.com/jupyterlab/jupyterlab/issues/9936>
>
> \<!--
> Welcome! Thanks for thinking of a way to improve JupyterLab. If this solve…s a problem for you, then it probably solves that problem for lots of people! So the whole community will benefit from this request.
> 
> 
> Before creating a new feature request please search the issues for relevant feature requests.
> \--\>
> 
> \### Problem
> 
> \<!-- Provide a clear and concise description of what problem this feature will solve. For example:
> 
> 
> \--\>
> I use the \[jupyter-vim\](https://github.com/jupyter-vim/jupyter-vim/) plugin which allows a jupyter notebook like experience where you can submit cells or lines of code within vim and have those run on the jupyter kernel and output to a \`console\` or \`qtconsole\`. 
> 
> Some output of libraries like fastai are designed to be run in a jupyter notebook-like environment because it outputs tables in HTML form. These objects don't display correctly in \`console\` or \`qtconsole\`, instead they are displayed as \`\<IPython.core.display.HTML object\>\`. 
> 
> I tried using a jupyter lab code console for my output window, which seems like the perfect solution. However, the code console doesn't show any output when code is sent to the kernel. So I opened an \[issue with jupyter-vim\](https://github.com/jupyter-vim/jupyter-vim/issues/58) but they said that the problem lies with the jupyter-vim code console allowing code sent to the kernel to display. Furthermore, they identified this line, https://github.com/jupyterlab/jupyterlab/blob/b4db7f03d2ad0b91a9c8c252c56ae5e4a9408fbb/packages/console/src/foreign.ts#L94, as the culprit for why this problem is occurring.
> 
> \### Proposed Solution
> 
> \<!-- Provide a clear and concise description of a way to accomplish what you want. For example:
> 
> \* Add an option so that when \[...\] \[...\] will happen
> --\>
> 
> Please allow for code sent to the jupyterlab kernel to display on the jupyterlab code console. I believe that this has something to do with this line of code:
> 
> https://github.com/jupyterlab/jupyterlab/blob/b4db7f03d2ad0b91a9c8c252c56ae5e4a9408fbb/packages/console/src/foreign.ts#L94
> 
> if there are questions regarding this please ask me or comment on \[my jupyter-vim issue.\](https://github.com/jupyter-vim/jupyter-vim/issues/58)
> 
> While Jupyter is an amazing tool I think that many of us prefer the vim/emacs editor experience, (that we have usually put a lot of time into customizing), for writing code and tools like jupyter-vim allow for the best of both of these worlds.
> 
> \### Additional context
> 
> \<!-- Add any other context or screenshots about the feature request here. You can also include links to examples of other programs that have something similar to your request. For example:
> 
> \* Another project \[...\] solved this by \[...\]
> \--\>

It seems to indicate that you can connect to the same kernel from another program and send it commands, which will then be displayed in the code console, if you choose a particular configuration.

---

<div class="post-metadata">

**Author:** ![bollwyvl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/bollwyvl/32/904_2.png) [@bollwyvl](https://discourse.jupyter.org/u/bollwyvl)\
**Post date:** [February 12, 2022, 2:21pm UTC](https://discourse.jupyter.org/t/from-external-program-append-cell-execute-and-show-output-in-a-running-jupyter-notebook/12983/4 "2022-02-12T14:21:05Z")

</div>

Yes, acting as _kernel_ client works fine, there’s just a number of keys that need to passed around, etc. However, the state of a _notebook_ client is separate from that of the _kernel_… there _is_ [set\_next\_input](https://github.com/ipython/ipython/blob/325262d1dfdaf65a95a2a94a8b70a7b8a8e119ce/IPython/core/interactiveshell.py#L2000) but it won’t _run_ anything, and not all clients implement it.

An approach that might work for your use case is [ipylab](https://github.com/jtpio/ipylab) which can run [commands](https://jupyterlab.readthedocs.io/en/stable/extension/extension_points.html#commands), which are probably the most stable way to interact with the lab client. Again, you’d need a handle to the kernel, but you’re probably going to need that anyway.
