Skip to main content

notebook_nanoservice

Nanoservices for notebooks.

Quickly and effortlessly expose your notebook's functions via a REST API.

Note: This library is designed to scaffold a small backend service for ease of development and prototyping purposes only. Not for production use.

Features

  • Hot functions - redefine your functions as you go without restarting the service.
  • No decoration required.
  • Auto serialization for primitive types, lists, Pandas dataframes, and images. Extensible for other types.
  • Works with Jupyter & Spark notebooks, probably others.
  • No dependency conflicts.
  • API manifest in JSON or markdown, suitable for use with LLMs.

Scenarios

Installation

pip install notebook-nanoservice

Usage

Initialize a server

from notebook_nanoservice import NanoService
service = NanoService(globals())
service.start()

View the manifest

View your API manifest in multiple formats:

  • JSON: http://localhost:5001 (default lightweight format)
  • Markdown: http://localhost:5001/?format=md
  • OpenAPI v3: http://localhost:5001/?format=openapi

Invoke a function

If you have a function such as:

def concat(a, b):
    return a + b

It can be invoked at http://localhost:5001/api/concat?a=value1&b=value2

See test/sample.ipynb for examples of type casting the parameters.

Stop and free up the port

service.stop()

API

Constructor

The NanoService class is initialized to expose the global context of a Jupyter notebook or Python script via a REST API:

NanoService(global_context, host, port, include_source)

Parameters

  • global_context (dict, required): The global context to expose. Typically passed as globals() from the notebook or script.
  • host (str, optional): The host address for the server. Defaults to "127.0.0.1".
  • port (int, optional): The port number for the server. Defaults to 5001.
  • include_source (bool, optional): Whether to include the source code of functions in the metadata. Defaults to False.

Multiple service instances can run simultaneously by having unique port numbers.

# in a different notebook
from notebook_nanoservice import NanoService
service = NanoService(globals(), port=5002)
service.start()

ignore_functions

By default, all user-defined functions are exposed via the REST API. The ignore_functions property is a list of function names that the server will ignore when exposing the global context. By default, it includes functions like exit, quit, and others that are not meant to be exposed.

You can append to this list to exclude additional functions from being exposed. For example:

service.ignore_functions.append("my_function_to_ignore")

Contributing

Contributions are welcome! Please submit a pull request or open an issue on the GitHub repository.

Development

Install development dependencies:

pip install -r requirements.txt

Install the package locally: Open test/local_install.ipynb and follow instructions within.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Release files for notebook-nanoservice 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for notebook-nanoservice 0.1.3
File Size Uploaded
notebook_nanoservice-0.1.3.tar.gz 11.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for notebook-nanoservice 0.1.3
File Interpreter ABI Platform
notebook_nanoservice-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 21.2 kB

Release files / notebook_nanoservice-0.1.3.tar.gz

Download URL notebook_nanoservice-0.1.3.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d6e2a25798000044476e94f8868210bdfd5532d0907e1ba22f3a5aabb4893bc2
BLAKE2b-256 checksum
How to use checksums
183da7731c4541740f0767c7b33d5cd552d33769271857a28ceb9ca24d561ccb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release files / notebook_nanoservice-0.1.3-py3-none-any.whl

Download URL notebook_nanoservice-0.1.3-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a79adb131945d5cc488f90d3a7a5051fd32be1f06f5971044cd6291ed0f0cbf6
BLAKE2b-256 checksum
How to use checksums
b25859a69881f9d408775b64dc8834b5ddea00f0c1b1466c807311666a2f91bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page