Use Cognite Functions
Cognite Functions lets you deploy Python code to Cognite Data Fusion (CDF), call the code on-demand, or schedule the code to run at regular intervals.
Authentication with OpenID Connect
With OpenID Connect, you can instantiate your CogniteClient
with either an OpenID Connect user token or client credentials.
Authenticate with an OpenID Connect user token
client = CogniteClient(project="my-project", token=my_token)
my_token
is a string or a callable.
Authenticate with client credentials
For more information see authenticate with client credentials on Python. To instantiate your CogniteClient
with client credentials:
from cognite.client import CogniteClient, ClientConfig
from cognite.client.credentials import OAuthClientCredentials
# This value may change depending on the cluster your CDF project runs on
base_url = "https://api.cognitedata.com"
tenant_id = "my-tenant-id"
creds = OAuthClientCredentials(
token_url=f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token",
client_id="my-client-id",
client_secret="my-client-secret",
scopes=[f"{base_url}/.default"]
)
cnf = ClientConfig(
client_name="custom-client-name",
project="cdf-project-name",
credentials=creds,
base_url=base_url
)
client = CogniteClient(cnf)
Create a function
You can create a function from a Jupyter Notebook, a folder, an uploaded zip file, or from CDF. The examples below assume that you have the Python SDK installed in your environment.
Create a function from a Jupyter Notebook
To create a function from a Jupyter Notebook, refer directly to the Python function named handle
. This is the easiest way to get started but it's only suitable for short and simple functions.
The function_handle
argument points to the handle
function defined in the example below, and the name in the definition must be handle
.
When you deploy with the function_handle
argument, you must import within handle()
. In other cases (with the folder
or file_id
arguments), the imports can be outside handle()
.
To create a Cognite Function with dependencies, see the Python SDK documentation.
Example:
- Define the following function in your notebook:
def handle(client, data):
asset_no = data["assetNo"]
print("Returning asset number {}".format(asset_no))
assets = client.assets.list()
return {"assetName": assets[asset_no].name}
- Deploy it to CDF:
from cognite.client import CogniteClient, ClientConfig
from cognite.client.credentials import OAuthClientCredentials
# This value would change depending on the cluster where your CDF projects runs on
base_url = "https://api.cognitedata.com"
tenant_id = "my-tenant-id"
creds = OAuthClientCredentials(
token_url=f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token",
client_id="my-client-id",
client_secret="my-client-secret",
scopes=[f"{base_url}/.default"]
)
cnf = ClientConfig(
client_name="custom-client-name",
project="cdf-project-name",
credentials=creds,
base_url=base_url
)
client = CogniteClient(cnf)
# Create the function
func = client.functions.create(
name="my-function",
external_id="my-function",
function_handle=handle
)
The function_handle
argument points to the handle
function defined above, and the name in the definition must be handle
.
When you deploy with the function_handle
argument, you must import within handle()
. In other cases (with the folder
or file_id
arguments), the imports can be outside handle()
.