Skip to content

Python Build

The build action builds a container image for a Python function. A Task is created by calling run() on the Function; task parameters are passed through that call.

Overview

The build action creates a container image containing the Python function and its dependencies. This image can then be used for deployment or distribution.

Quick example

function = dh.new_function(
    name="my-python-function",
    kind="python",
    code_src="handler.py",
    handler="main",
    python_version="PYTHON3_10"
)

run = function.run(
    action="build",
    inputs={"data": dataitem.key},
    parameters={"threshold": 0.5}
)

Parameters

Function Parameters

Must be specified when creating the function.

Name Type Description
project str Project name. Required only when creating from the library; otherwise MUST NOT be set.
name str Name that identifies the object. Required.
kind str Function kind. Must be python. Required.
uuid str Object ID in UUID4 format.
description str Description of the object.
labels list[str] List of labels.
embedded bool Whether the object should be embedded in the project.
code_src str URI pointing to the source code.
code str Source code provided as plain text.
base64 str Source code encoded as base64.
handler str Function entrypoint.
init_function str Init function name for remote (Nuclio) execution.
python_version str Python version to use. Required.
lang str Source code language (informational).
image str Container image used to execute the function.
base_image str Base image (name:tag) used to build the execution image.
requirements list List of pip requirements to install into the execution image.

Python Versions

The Python runtime supports versions 3.9, 3.10, 3.11, and 3.12 expressed as:

  • PYTHON3_9
  • PYTHON3_10
  • PYTHON3_11
  • PYTHON3_12

Init Function

The init function is the entrypoint used by the Nuclio init wrapper. Specify the init function name via the init_function parameter.

Base Image

The base image is the image (name:tag) used as the foundation when building the execution image for the function.

Warning

Deploying jobs built from certain base images may be restricted by cluster security policies. Confirm allowed base images with your cluster administrator.

Requirements

Requirements are a list of strings representing packages to be installed by pip in the image where the function will be executed.

requirements = ["numpy", "pandas>1,<3", "scikit-learn==1.2.0"]

Task Parameters

Can only be specified when calling function.run().

Shared Parameters

Name Type Description
action str Task action. Required. Must be build
node_selector list[dict] Node selector configuration.
volumes list[dict] List of volumes.
resources dict Resource limits/requests.
affinity dict Affinity configuration.
tolerations list[dict] Tolerations.
envs list[dict] Environment variables.
secrets list[str] List of secret names.
profile str Profile template.

Run Parameters

Can only be specified when calling function.run().

Run Shared Parameters

Name Type Description
local_execution bool Execute the run locally instead of remotely.
inputs dict Mapping of function argument names to entity keys.
parameters dict Extra parameters passed to the function.
init_parameters dict Parameters supplied to the init function.

Entity methods

Run methods

Once the run is created, you can access its attributes and methods through the run object.

output

Get run's output by name.

Parameters:

Name Type Description Default
output_name str

Key of the result.

required
as_key bool

If True, return result as key.

False
as_dict bool

If True, return result as dictionary.

False

Returns:

Type Description
Entity | dict | str | None

Result.

outputs

Get run's outputs.

Parameters:

Name Type Description Default
as_key bool

If True, return results as keys.

False
as_dict bool

If True, return results as dictionaries.

False

Returns:

Type Description
dict

List of output objects.

result

Get result by name.

Parameters:

Name Type Description Default
result_name str

Name of the result.

required

Returns:

Type Description
Any

The result.

results

Get results.

Returns:

Type Description
dict

The results.