Package: kgrid-python-runtime
KGrid Python RuntimeA KGrid runtime for Knowledge Objects in a native python environment that connects to an activator using the proxy adapter.Prerequisites:Python3.8 or higherpipInstallation from the python package:Runpython -m pip install kgrid-python-runtimeto download the latest packageRunpython -m kgrid_python_runtimeto start the runtimeInstallation from an image:Download the latest image from docker hub:docker pull kgrid/kgrid-python-runtimeUse the following command to run the image on Linux:docker run --network host kgrid/kgrid-python-runtimeOrUse the following command to run the image on Windows:docker run -it -p :5000:5000 -e KGRID_PROXY_ADAPTER_URL=http://host.docker.internal:8080 kgrid/kgrid-python-runtimeThis starts the runtime pointed to an activator running on the same system at localhost:8080EndpointsThe runtime exposes two endpoints which can be used to see the details of the runtime and what has been activatedGET /infoDisplays details about the runtime such as the running version and status.GET /endpointsDisplays a list of the activated endpoints in the engine.Configuration:Set these environment variables to customize your runtime's settings.KGRID_PYTHON_ENV_URLThe address of this environment that the activator will use to communicate with it.Default value:http://localhostKGRID_PYTHON_ENV_PORTThe port this environment is available on.Default value:5000KGRID_PROXY_ADAPTER_URLThe url of the adapter this runtime will communicate withDefault value:http://localhost:8080By default, the python runtime will tell the Kgrid Activator that it is started athttp://localhost:5000.KGRID_PYTHON_CACHE_STRATEGYThe caching strategy of this runtime. It can take three values:never,always, oruse_checksumnever- existing objects from the activator are overwritten on every activation call.always- existing objects stored in the runtime will never be re-downloaded from the activator and the local pyshelf and context.json files must be deleted and the runtime restarted for the objects to be replaced.use_checksum- objects will look for a checksum in the deployment descriptor sent over during activation and only re-download the object if that checksum has changed.Default value:neverKGRID_PROXY_HEARTBEAT_INTERVALThe frequency (in seconds) at which the runtime will ping the activator and attempt to reconnect if the connection has been broken. Can be set to any value above 5 seconds or 0 to disable the heartbeat.Default value:30DEBUGChanges the logging level to debug, takes a booleantrue/falseDefault:falseCreating a python Knowledge Object:Just like other knowledge objects, python objects have 4 basic parts:
service.yaml, deployment.yaml, metadata.txt,
and a payload that can be any number of python files.The packaging spec for knowledge objects can be foundhere.An example KO with naan ofhello, a name ofneighbor, api version of1.0, and endpointwelcome,
a Deployment Specification might look like this:/welcome:post:artifact:-"src/hello.js"engine:"python"function:"main"entry:"src/hello.js"Wherefunctionis the name of the main javascript entry function.You would then execute this endpoint to see the code work:POST <activator url>/<naan>/<name>/<api version>/<endpoint>In this example:POST <activator url>/hello/neighbor/1.0/welcomeThe Service Specification for this object would likewise then beopenapi:3.0.2info:version:'1.0'title:'Helloneighbor'description:An example of simple Knowledge Objectlicense:name:GNU General Public License v3 (GPL-3)url:>-https://tldrlegal.com/license/gnu-general-public-license-v3-(gpl-3)#fulltextcontact:name:KGrid Teamemail:kgrid-developers@umich.eduurl:'http://kgrid.org'servers:-url:/js/neighbordescription:Hello worldtags:-name:KO Endpointsdescription:Hello world Endpointspaths:/welcome:post:...In the Service Specification the servers.url must match the naan and name of the object (/js/neighbor) and the path must match the path in Deployment Specification (/welcome).
The service spec conforms to the swaggerOpenAPI spec.If your python package requires other python packages,
simply specify them in a file calledrequirements.txtat the root of your object:package-name=0.1.5
other-package-name=1.3.5
third-package-name=1.5.4Right now the python runtime only supports requirements.txt using pip, see therequirements.txt documentation.That's it! as long as the payload is written in valid python,
and the object is built to the spec, you're ready to go.
Example python objects can be found in theexample collectionor in the example-objects directory in this repository.For DevelopersTo run the app:Clone this project and set the environment variable:PYTHONPATHto the project root.Example (Ubuntu):export PYTHONPATH=~/Projects/kgrid-python-runtimeRunpython kgrid_python_runtime/app.py runserverfrom the top level of the project.Important NotesEditing the cache directly from the runtime's shelf will
not propagate changes to the endpoints in the runtime. New
KOs must come from the activator.The runtime will attempt to load any Knowledge Objects that
were previously loaded onto its shelf before registering with
the activator and acquiring its objects. The shelf directory can
be deleted if there is a need to get all objects fresh from the activator.