Skip to main content

Nanoservices for notebooks

Project description

notebook_nanoservice

Nanoservices for notebooks.

Quickly and effortlessly expose your notebook's capabilities 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.

Installation

pip install notebook-nanoservice

Usage

Initialize a server

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

View the manifest

Open http://localhost:5001 to see a JSON manifest of your API (the global functions in your notebook). You may also see it in markdown at http://localhost:5001/?format=md

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.

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

notebook_nanoservice-0.1.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

notebook_nanoservice-0.1.2-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file notebook_nanoservice-0.1.2.tar.gz.

File metadata

  • Download URL: notebook_nanoservice-0.1.2.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for notebook_nanoservice-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4e110a021ec66d08472f896037ae9fb70b97072d27023bb9f8ca1763d0db5cd0
MD5 4295d3bb9ace517e2b37d219a7ce1453
BLAKE2b-256 6b49ad588c416105f99be15ad6d49718630dd6a41b02ccef96c55d46976d24c5

See more details on using hashes here.

File details

Details for the file notebook_nanoservice-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for notebook_nanoservice-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 36bf6ca0255944000457688cd8217bddc83ae80946d01535fb5c9e3c85ed6bdf
MD5 fba57d6beadfdd36266d092b8ce9ac37
BLAKE2b-256 4bf6a2eaf9617cf1359a3e2613887ba04035399fd7e3788f6d875d6068f6dbf5

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