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 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()
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file notebook_nanoservice-0.1.1.tar.gz.
File metadata
- Download URL: notebook_nanoservice-0.1.1.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb066dfc275f60949e66ba6cb48a514897432887edc5d6c24be7f7e3fb0de497
|
|
| MD5 |
6cff5b08beac024a33c1d707b87f0388
|
|
| BLAKE2b-256 |
51aa7966c3814fb71228869ce870946d1c204d1d25b0a83933cfe537d93838ac
|
File details
Details for the file notebook_nanoservice-0.1.1-py3-none-any.whl.
File metadata
- Download URL: notebook_nanoservice-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b44e9f18e04e3a3ea750c62c76f59ca7e25db188511303bd27740fa933f20fe
|
|
| MD5 |
ef10e26bf5ce4e3d0d080569246f701b
|
|
| BLAKE2b-256 |
83f3bef7ea6afa85518f1665f585b6d692f4ec69aff6415a2960d778a49a5759
|