Skip to main content

No project description provided

Project description

Datalayer

Become a Sponsor

Jupyter NbModel Client

Github Actions Status PyPI - Version

Client to interact with a Jupyter Notebook model.

To install the library, run the following command.

pip install jupyter_nbmodel_client

Usage

  1. Ensure you have the needed packages in your environment to run the example here after.
pip install jupyterlab jupyter-collaboration ipykernel matplotlib
  1. Start a JupyterLab server, setting a port and a token to be reused by the agent, and create a notebook test.ipynb.
jupyter lab --port 8888 --IdentityProvider.token MY_TOKEN
  1. Open a Python REPL and execute the following snippet to add a cell.
from jupyter_nbmodel_client import NbModelClient

with NbModelClient(server_url="http://localhost:8888", token="MY_TOKEN", path="test.ipynb") as notebook:
    notebook.add_code_cell("print('hello world')")

Check test.ipynb in JupyterLab.

  1. The previous example does not involve kernels. Put that now in the picture, adding a cell and executing within a kernel process.
from jupyter_kernel_client import KernelClient
from jupyter_nbmodel_client import NbModelClient

with KernelClient(server_url="http://localhost:8888", token="MY_TOKEN") as kernel:
    with NbModelClient(server_url="http://localhost:8888", token="MY_TOKEN", path="test.ipynb") as notebook:
        cell_index = notebook.add_code_cell("print('hello world')")
        results = notebook.execute_cell(cell_index, kernel)

        assert results["status"] == "ok"
        assert len(results["outputs"]) > 0

Check test.ipynb in JupyterLab.

You can go further and create a plot with Matplotlib.

from jupyter_kernel_client import KernelClient
from jupyter_nbmodel_client import NbModelClient

CODE = """import matplotlib.pyplot as plt

fig, ax = plt.subplots()

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

ax.bar(fruits, counts, label=bar_labels, color=bar_colors)

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

plt.show()
"""

with KernelClient(server_url="http://localhost:8888", token="MY_TOKEN") as kernel:
    with NbModelClient(server_url="http://localhost:8888", token="MY_TOKEN", path="test.ipynb") as notebook:
        cell_index = notebook.add_code_cell(CODE)
        results = notebook.execute_cell(cell_index, kernel)

        assert results["status"] == "ok"
        assert len(results["outputs"]) > 0

Check test.ipynb in JupyterLab.

[!NOTE]

Instead of using the clients as context manager, you can call the start() and stop() methods.

from jupyter_nbmodel_client import NbModelClient

kernel = KernelClient(server_url="http://localhost:8888", token="MY_TOKEN")
kernel.start()

try:
    notebook = NbModelClient(server_url="http://localhost:8888", token="MY_TOKEN", path="test.ipynb"):
    notebook.start()
    try:
        cell_index = notebook.add_code_cell("print('hello world')")
        results = notebook.execute_cell(cell_index, kernel)
    finally:
        notebook.stop()
finally:
    kernel.stop()

Uninstall

To remove the library, run the following.

pip uninstall jupyter_nbmodel_client

Contributing

Development install

# Clone the repo to your local environment
# Change directory to the jupyter_nbmodel_client 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 jupyter_nbmodel_client

Packaging the library

See RELEASE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jupyter_nbmodel_client-0.4.0.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jupyter_nbmodel_client-0.4.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file jupyter_nbmodel_client-0.4.0.tar.gz.

File metadata

  • Download URL: jupyter_nbmodel_client-0.4.0.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for jupyter_nbmodel_client-0.4.0.tar.gz
Algorithm Hash digest
SHA256 47bdb19db32be8155dc2f953adcc1e71b685d8d8a4b3465ea1ed8c7d1dfe4456
MD5 0cecbb283c8ce4cd2933154d094e4a0a
BLAKE2b-256 ba46d259ea67a8df06904cc8f219a77d19774fb4b5e43f9350fe9485d52fed18

See more details on using hashes here.

File details

Details for the file jupyter_nbmodel_client-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jupyter_nbmodel_client-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1587a014f91de99a919eacb844d210ecd0240f6db5f7a6aa48b4f36a23ca88e3
MD5 65ae5f3f71fc53d33e9bd974e2662fb3
BLAKE2b-256 830dfb8a6ea1c1a77df1d63d142af857fca2ecf3bbef5513516647f35cbde534

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page