Skip to content

Cognite Functions

Introduction

After you have built your logic with the newly generated data model SDK, you might want to deploy it to Cognite Functions.

Cognite Functions enables Python code to be hosted and executed in the cloud, on demand or by using a schedule.

A few useful links for Cognite Functions:

Deploying your generated SDK to Cognite Functions

Cognite Functions supports private packages in your function deployment, and pygen can generate a wheel package of your data model SDK. To generate a wheel package, use the build_wheel function.

For example, given the data model (power-models, Windmill, 1) we generated in the Generation guide, we can build a wheel package with the following code:

from cognite.client import CogniteClient
from cognite.pygen import build_wheel

client = CogniteClient()

build_wheel(("power-models", "Windmill", 1), client)

This will output a wheel package, windmill-1.0.0-py3-none-any.whl, in the dist folder of the current working directory. Note The version of the package will always be 1.0.0 as the version of a data model can be an arbitrary string, while the version of a package must be a valid semantic version.

To deploy generated SDK to Cognite Functions, you can use the cognite-toolkit CLI with the following structure,

📦functions/
┣ 📂my_function - The function folder
┃ ┣ 📦windmill-1.0.0-py3-none-any.whl - The generated wheel package
┃ ┣ 📜__init__.py - Empty file (required to make the function into a package)
┃ ┣ 📜handler.py -  Module with script inside a handle function
┃ ┗ 📜requirements.txt - Explicitly states the dependencies needed to run the handler.py script.
┣ 📜my_function.Function.yaml - The configuration file for the function
┣ 📜my_schedule.Schedule.yaml - The configuration file for the function schedule(s)

from cognite.client import CogniteClient
from windmill WindmillClient


def handle(client: CogniteClient, data: dict):
    windmill_client = WindmillClient(client)
    windmills = windmill_client.windmill.list(limit=10)
    print(windmills)
    return "Success"
# The Cognite SDK should be specified and match the version used to generate the SDK.
cognite-sdk==7.54.4
# This is the syntax for adding a private wheel in a Cognite Function.
# Note the 'function' prefix is not a placeholder, it is a required prefix for private packages.
function/windmill-1.0.0-py3-none-any.whl
name: my_function
externalId: fn_my_function
owner: Anonymous
description: 'Demo of a function with a pygen generated SDK'
runtime: 'py311'
- name: "daily-8am-utc"
  functionExternalId: fn_my_function
  description: "Run every day at 8am UTC"
  cronExpression: "0 8 * * *"
  data:
    some: "data"

After you have the folder structure set up, you can deploy the function using the cognite-toolkit CLI, go to Deploy for more information on how to use the CLI.