datalayer
jupyter-nbmodel-client
Python

🪐 Jupyter NbModel Client.

Last updated Jun 4, 2026
15
Stars
6
Forks
5
Issues
0
Stars/day
Attention Score
52
Language breakdown
Python 97.5%
Makefile 1.6%
Jupyter Notebook 0.9%
Files click to expand
README

Datalayer

Become a Sponsor

🪐 Jupyter NbModel Client

Github Actions Status PyPI - Version

Jupyter NbModel Client is a python library to interact with a live Jupyter Notebooks.

To install the library, run the following command.

pip install jupyternbmodelclient

Usage with Jupyter

  • Ensure you have the needed packages in your environment to run the example here after.
pip install jupyterlab jupyter-collaboration matplotlib
  • Start a JupyterLab server, setting a port and a token to be reused by the agent, and create a notebook test.ipynb.
# make jupyterlab
jupyter lab --port 8888 --ServerApp.portretries 0 --IdentityProvider.token MYTOKEN --ServerApp.root_dir ./dev
  • Open a IPython (needed for async functions) REPL in a terminal with ipython (or jupyter console). Execute the following snippet to add a cell in the test.ipynb notebook.
from jupyternbmodelclient import NbModelClient, getjupyternotebookwebsocketurl

wsurl = getjupyternotebookwebsocket_url( server_url="http://localhost:8888", token="MY_TOKEN", path="test.ipynb" )

async with NbModelClient(ws_url) as nbmodel: nbmodel.addcodecell("print('hello world')")

Check test.ipynb in JupyterLab, you should see a cell with content print('hello world') appended to the notebook.
  • The previous example does not involve kernels. Put that now in the picture, adding a cell and executing the cell code within a kernel process.
from jupyterkernelclient import KernelClient
from jupyternbmodelclient import NbModelClient, getjupyternotebookwebsocketurl

with KernelClient(serverurl="http://localhost:8888", token="MYTOKEN") as kernel: wsurl = getjupyternotebookwebsocket_url( server_url="http://localhost:8888", token="MY_TOKEN", path="test.ipynb" ) async with NbModelClient(ws_url) as notebook: cellindex = notebook.addcode_cell("print('hello world')") results = notebook.executecell(cellindex, kernel) print(results) assert results["status"] == "ok" assert len(results["outputs"]) > 0

Check test.ipynb in JupyterLab. You should see an additional cell with content print('hello world') appended to the notebook, but this time the cell is executed, so the output should show hello world.

You can go further and create a plot with eg matplotlib.

from jupyterkernelclient import KernelClient
from jupyternbmodelclient import NbModelClient, getjupyternotebookwebsocketurl

CODE = """import matplotlib.pyplot as plt

fig, ax = plt.subplots()

fruits = ['apple', 'blueberry', 'cherry', 'orange'] counts = [40, 100, 30, 55] barlabels = ['red', 'blue', 'red', 'orange'] bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange']

ax.bar(fruits, counts, label=barlabels, color=barcolors)

ax.set_ylabel('fruit supply') ax.set_title('Fruit supply by kind and color') ax.legend(title='Fruit color')

plt.show() """

with KernelClient(serverurl="http://localhost:8888", token="MYTOKEN") as kernel: wsurl = getjupyternotebookwebsocket_url( server_url="http://localhost:8888", token="MY_TOKEN", path="test.ipynb" ) async with NbModelClient(ws_url) as notebook: cellindex = notebook.addcode_cell(CODE) results = notebook.executecell(cellindex, kernel) print(results) assert results["status"] == "ok" assert len(results["outputs"]) > 0

Check test.ipynb in JupyterLab for the cell with the matplotlib.
[!NOTE]
>
Instead of using the nbmodel clients as context manager, you can call the start() and stop() methods.
from jupyternbmodelclient import NbModelClient, getjupyternotebookwebsocketurl

kernel = KernelClient(serverurl="http://localhost:8888", token="MYTOKEN") kernel.start()

try: wsurl = getjupyternotebookwebsocket_url( server_url="http://localhost:8888", token="MY_TOKEN", path="test.ipynb" ) notebook = NbModelClient(ws_url) await notebook.start() try: cellindex = notebook.addcode_cell("print('hello world')") results = notebook.executecell(cellindex, kernel) finally: await notebook.stop() finally: kernel.stop()

Usage with Datalayer

To connect to a Datalayer collaborative room, you can use the helper function getdatalayernotebookwebsocketurl:

  • The server is https://prod1.datalayer.run for the Datalayer production SaaS.
  • The room_id is the id of your notebook shown in the URL browser bar.
  • The token is the assigned token for the notebook.
All those details can be retrieved from a Notebook sidebar on the Datalayer SaaS.
from jupyternbmodelclient import NbModelClient, getdatalayernotebookwebsocketurl

wsurl = getdatalayernotebookwebsocket_url( server_url=server, roomid=roomid, token=token )

async with NbModelClient(ws_url) as notebook: notebook.addcodecell("1+1")

Uninstall

To remove the library, run the following.

pip uninstall jupyternbmodelclient

Contributing

Development install

# Clone the repo to your local environment

Change directory to the jupyternbmodelclient directory

Install package in development mode - will automatically enable

The server extension.

pip install -e ".[test,lint,typing]"

Running Tests

Install dependencies:

pip install -e ".[test]"

To run the python tests, use:

pytest

Development uninstall

pip uninstall jupyternbmodelclient

Packaging the library

See RELEASE

🔗 More in this category

© 2026 GitRepoTrend · datalayer/jupyter-nbmodel-client · Updated daily from GitHub