Create function
- async AsyncCogniteClient.functions.create(
- name: str | FunctionWrite,
- folder: str | None = None,
- file_id: int | None = None,
- function_path: str = 'handler.py',
- function_handle: FunctionHandle | None = None,
- external_id: str | None = None,
- description: str | None = None,
- owner: str | None = None,
- secrets: dict[str, str] | None = None,
- env_vars: dict[str, str] | None = None,
- cpu: float | None = None,
- memory: float | None = None,
- runtime: Literal['py310', 'py311', 'py312'] | None = None,
- metadata: dict[str, str] | None = None,
- index_url: str | None = None,
- extra_index_urls: list[str] | None = None,
- skip_folder_validation: bool = False,
- data_set_id: int | None = None,
When creating a function, the source code can be specified in one of three ways:
Via the folder argument, which is the path to the folder where the source code is located. function_path must point to a python file in the folder within which a function named handle must be defined.
Via the file_id argument, which is the ID of a zip-file uploaded to the files API. function_path must point to a python file in the zipped folder within which a function named handle must be defined.
Via the function_handle argument, which is a reference to a function object, which must be named handle.
The function named handle is the entrypoint of the created function. Valid arguments to handle are data, client, secrets and function_call_info: - If the user calls the function with input data, this is passed through the data argument. - If the user gives one or more secrets when creating the function, these are passed through the secrets argument. - Data about the function call can be accessed via the argument function_call_info, which is a dictionary with keys function_id, call_id, and, if the call is scheduled, schedule_id and scheduled_time.
By default, the function is deployed with the latest version of cognite-sdk. If a specific version is desired, it can be specified either in a requirements.txt file when deploying via the folder argument or between [requirements] tags when deploying via the function_handle argument (see example below).
For help with troubleshooting, please see this page.
- Parameters:
name (str | FunctionWrite) – The name of the function or a FunctionWrite object. If a FunctionWrite object is passed, all other arguments are ignored.
folder (str | None) – Path to the folder where the function source code is located.
file_id (int | None) – File ID of the code uploaded to the Files API.
function_path (str) – Relative path from the root folder to the file containing the handle function. Defaults to handler.py. Must be on POSIX path format.
function_handle (FunctionHandle | None) – Reference to a function object, which must be named handle.
external_id (str | None) – External id of the function.
description (str | None) – Description of the function.
owner (str | None) – Owner of this function. Typically used to know who created it.
secrets (dict[str, str] | None) – Additional secrets as key/value pairs. These can e.g. password to simulators or other data sources. Keys must be lowercase characters, numbers or dashes (-) and at most 15 characters. You can create at most 30 secrets, all keys must be unique.
env_vars (dict[str, str] | None) – Environment variables as key/value pairs. Keys can contain only letters, numbers or the underscore character. You can create at most 100 environment variables.
cpu (float | None) – Number of CPU cores per function. Allowed range and default value are given by the limits endpoint., and None translates to the API default. On Azure, only the default value is used.
memory (float | None) –
Memory per function measured in GB. Allowed range and default value are given by the limits endpoint., and None translates to the API default. On Azure, only the default value is used.
runtime (RunTime | None) – The function runtime. Valid values are [“py310”, “py311”, “py312”, None], and None translates to the API default which will change over time. The runtime “py312” resolves to the latest version of the Python 3.12 series.
metadata (dict[str, str] | None) – Metadata for the function as key/value pairs. Key & values can be at most 32, 512 characters long respectively. You can have at the most 16 key-value pairs, with a maximum size of 512 bytes.
index_url (str | None) – Index URL for Python Package Manager to use. Be aware of the intrinsic security implications of using the index_url option. More information can be found on official docs,
extra_index_urls (list[str] | None) –
Extra Index URLs for Python Package Manager to use. Be aware of the intrinsic security implications of using the extra_index_urls option. More information can be found on official docs,
skip_folder_validation (bool) – When creating a function using the ‘folder’ argument, pass True to skip the extra validation step that attempts to import the module. Skipping can be useful when your function requires several heavy packages to already be installed locally. Defaults to False.
data_set_id (int | None) – Data set to upload the function code to. Note: Does not affect the function itself.
- Returns:
The created function.
- Return type:
Examples
Create function with source code in folder:
>>> from cognite.client import CogniteClient, AsyncCogniteClient >>> client = CogniteClient() >>> # async_client = AsyncCogniteClient() # another option >>> function = client.functions.create( ... name="myfunction", folder="path/to/code", function_path="path/to/function.py" ... )
Create function with file_id from already uploaded source code:
>>> function = client.functions.create( ... name="myfunction", file_id=123, function_path="path/to/function.py" ... )
Create function with predefined function object named handle:
>>> function = client.functions.create(name="myfunction", function_handle=handle)
Create function with predefined function object named handle with dependencies:
>>> def handle(client, data): >>> ''' >>> [requirements] >>> numpy >>> [/requirements] >>> ''' >>> pass >>> >>> function = client.functions.create(name="myfunction", function_handle=handle)