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.
- 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.
- Share your REST API via a tunnel such as Ngrok or Microsoft DevTunnels.
- Stand up an MCP server over your REST API.
pip install notebook-nanoservicefrom notebook_nanoservice import NanoService
service = NanoService(globals())
service.start()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
If you have a function such as:
def concat(a, b):
return a + bIt can be invoked at http://localhost:5001/api/concat?a=value1&b=value2
See test/sample.ipynb for examples of type casting the parameters.
service.stop()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)global_context(dict, required): The global context to expose. Typically passed asglobals()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 to5001.include_source(bool, optional): Whether to include the source code of functions in the metadata. Defaults toFalse.
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()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")Contributions are welcome! Please submit a pull request or open an issue on the GitHub repository.
Install development dependencies:
pip install -r requirements.txtInstall the package locally: Open test/local_install.ipynb and follow instructions within.
This project is licensed under the MIT License. See the LICENSE file for details.