Skip to main content

Cortex Axon Python SDK

Project description

Cortex Axon SDK for Python

This is the official Cortex Axon SDK for Python. It provides a simple way to interact with and extend your Cortex instance.

Getting started

To run the Cortex SDK you need to get the Axon Agent via Docker:

docker pull ghcr.io/cortexapps/cortex-axon-agent:latest

Then to scaffold a Python project:

docker run -it --rm -v "$(pwd):/src" ghcr.io/cortexapps/cortex-axon-agent:latest init --language python my-python-axon project

This will create a new directory my-python-axon with a Go project scaffolded in the current directory.

Running locally

To run your project, first start the agent Docker container like:

docker run -it --rm -p "50051:50051" -p "80:80" -e "DRYRUN=true" ghcr.io/cortexapps/cortex-axon-agent:latest serve

This is DRYRUN mode that prints what it would have called, to run against the Cortex API remove the DRYRUN environment variable and add -e "CORTEX_API_TOKEN=$CORTEX_API_TOKEN. Be sure to export your token first, e.g. export CORTEX_API_TOKEN=your-token.

Adding handlers

To add a handler, open main.py and create a function:

@cortex_scheduled(interval="5s")
def my_handler(ctx: HandlerContext):

    payload = {
        "values": {
            "my-service": [
                {
                    "key": "exampleKey1",
                    "value": "exampleValue1",
                },
                {
                    "key": "exampleKey2",
                    "value": "exampleValue2",
                },
            ]
        }
    }

    json_payload = json.dumps(payload)

    response = ctx.cortex_api_call(
        method="PUT",
        path="/api/v1/catalog/custom-data",
        body=json_payload,
    )

    if response.status_code >= 400:
        ctx.log(f"SetCustomTags error: {response.body}", level="ERROR")
        exit(1)

    ctx.log("CortexApi PUT custom-data called successfully!")

@cortex_webhook(id="my-webhook-1")
def my_webhook_handler(context: HandlerContext):
    context.log("Success! Webhook handler called!")
    body = context.args["body"]
    context.log(f"Webhook body: {body}")

Now start the agent in a separate terminal:

make run-agent

And run your project:

python main.py

This will begin executing your handler every 5 seconds. To invoke the webhook handler, you can just send a POST request to http://localhost:80/webhook/my-webhook-1 with a body.

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

cortex_axon_sdk-0.0.3.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

cortex_axon_sdk-0.0.3-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file cortex_axon_sdk-0.0.3.tar.gz.

File metadata

  • Download URL: cortex_axon_sdk-0.0.3.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.13.3 Linux/6.11.0-1012-azure

File hashes

Hashes for cortex_axon_sdk-0.0.3.tar.gz
Algorithm Hash digest
SHA256 44de2ee17bceb9a9de39898e248485b39106919145efd52cbe0736d26d50f0e5
MD5 c86868bb007eb1a8649c7e87e42cfe82
BLAKE2b-256 cc73f2a01f39021a4e7b59b445df112061867afcfc512f942690a252b4014346

See more details on using hashes here.

File details

Details for the file cortex_axon_sdk-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: cortex_axon_sdk-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.13.3 Linux/6.11.0-1012-azure

File hashes

Hashes for cortex_axon_sdk-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a5a2627200dd3eeeade18222dabf32add4073c95738e1255db8a87fb730f1639
MD5 64fe37dbcd2f6c1ba642e2df7a4234a8
BLAKE2b-256 f69afcbae86eaa0db0a55de7185fde486b88894a866b315b8a67f7e6be2705c7

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