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.2.tar.gz (12.4 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.2-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cortex_axon_sdk-0.0.2.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.13.3 Linux/6.8.0-1021-azure

File hashes

Hashes for cortex_axon_sdk-0.0.2.tar.gz
Algorithm Hash digest
SHA256 5937ac15e3a42f82b3ddd260b5e217365fe40d6798a0822aabe2dd0a3c1ac7d5
MD5 71b813d529189e2df7f4cef5abd201a6
BLAKE2b-256 637ab6e0dcc31a6e51efdb551936bb86fa76ff1ad4003229ed973bca3996cbf8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for cortex_axon_sdk-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5484eb82982149930cfb54b377516d4e432a4ca2c7bbefe26c7b641d01457adb
MD5 f071fd48bcb3223a6fbb802c17391817
BLAKE2b-256 7aae37713a11c1c937771cf1c4059f4eb087d91b77a74d5722d113a0384920b9

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