Python Extension Development (Labs)
These classes can be used by developers to implement their own Python nodes for KNIME. For a more detailed description see the Pure Python Node Extensions Guide
Note
Before KNIME AP 4.7, the module used to access KNIME functionality was called knime_extension. This module has been renamed
to knime.extension.
Nodes
- class knime.extension.PythonNode
Extend this class to provide a pure Python based node extension to KNIME Analytics Platform.
Users can either use the decorators
@knext.input_table,@knext.input_binary,@knext.output_table,@knext.output_binary, and@knext.output_view, or populate theinput_ports,output_ports, andoutput_viewattributes.Use the Python logging facilities and its
.warningand.errormethods to write warnings and errors to the KNIME console..infoand.debugwill only show up in the KNIME console if the log level in KNIME is configured to show these.Examples
>>> import logging ... import knime.extension as knext ... ... LOGGER = logging.getLogger(__name__) ... ... category = knext.category("/community", "mycategory", "My Category", "My category described", icon="icons/category.png") ... ... @knext.node(name="Pure Python Node", node_type=knext.NodeType.LEARNER, icon_path="icons/icon.png", category=category) ... @knext.input_table(name="Input Data", description="We read data from here") ... @knext.output_table(name="Output Data", description="Whatever the node has produced") ... class TemplateNode(knext.PythonNode): ... # A Python node has a description. ... ... def configure(self, configure_context, table_schema): ... LOGGER.info(f"Configuring node") ... return table_schema ... ... def execute(self, exec_context, table): ... return table
- abstract configure(config_context: ConfigurationContext, *inputs)
Configure this Python node.
- Parameters:
config_context (ConfigurationContext) – The ConfigurationContext providing KNIME utilities during execution
*inputs – Each input table spec or binary port spec will be added as parameter, in the same order that the ports were defined.
- Returns:
Either a single spec, or a tuple or list of specs. The number of specs must match the number of defined output ports, and they must be returned in this order. Alternatively, instead of a spec, a knext.Column can be returned (if the spec shall only consist of one column).
- Return type:
Union[Spec, List[Spec], Tuple[Spec, …], Column]
- Raises:
InvalidParametersError – If the current input parameters do not satisfy this node’s requirements.
- abstract execute(exec_context: ExecutionContext, *inputs)
Execute this Python node.
- Parameters:
exec_context (ExecutionContext) – The ExecutionContext providing KNIME utilities during execution.
*inputs (tuple) – Each input table or binary port object will be added as a parameter, in the same order that the ports were defined. Tables will be provided as a kn.Table, while binary data will be a plain Python bytes object.
- Returns:
Either a single output object (table or binary), or a tuple or list of objects. The number of output objects must match the number of defined output ports, and they must be returned in this order. Tables must be provided as a kn.Table or kn.BatchOutputTable, while binary data should be returned as plain Python bytes object.
- Return type:
object or tuple/list of objects
A node is part of a category:
- knime.extension.category(path: str, level_id: str, name: str, description: str, icon: str, after: str = '', locked: bool = True)
Register a new node category.
A node category must be created only once. Use the string that represents the absolute category path to add nodes to an existing category.
Examples
>>> node_category = knext.category( ... "/testing", "python-nodes", "Python Nodes", "Python testing nodes", "icon.png" ... ) ... ... @knext.node( ... name="Simple Python Node", ... node_type=knext.NodeType.MANIPULATOR, ... icon_path="icon.png", ... category=node_category ... ) ... class SimpleNode(knext.PythonNode): ... pass
- Parameters:
path (str) – The absolute path that leads to this category, e.g., “/io/read”. The segments are the category level-IDs, separated by a slash (“/”). Categories that contain community nodes should be placed in the “/community” category.
level_id (str) – The identifier of the level which is used as a path-segment and must be unique at the level specified by “path”.
name (str) – The name of this category, e.g., “File readers”.
description (str) – A short description of the category.
icon (str) – File path to a 16x16 pixel PNG icon for this category. The path must be relative to the root of the extension.
after (str, optional) – Specifies the level-id of the category after which this category should be sorted. Defaults to “”.
locked (bool, optional) – Set this to False to allow extensions from other vendors to add sub-categories or nodes to this category. Defaults to True.
- Returns:
The full path of the category, which can be used to create nodes inside this category.
- Return type:
str
A node has a type:
- class knime.extension.NodeType(value)
Defines the different node types that are available for Python based nodes.
- LEARNER = 'Learner'
A node learning a model that is typically consumed by a PREDICTOR.
- MANIPULATOR = 'Manipulator'
A node that manipulates data.
- OTHER = 'Other'
A node that doesn’t fit one of the other node types.
- PREDICTOR = 'Predictor'
A node that predicts something typically using a model provided by a LEARNER.
- SINK = 'Sink'
A node consuming data.
- SOURCE = 'Source'
A node producing data.
- VISUALIZER = 'Visualizer'
A node that visualizes data.
A node’s configure method receives a configuration context that lets you interact with KNIME
- class knime.extension.ConfigurationContext(java_ctx, flow_variables, input_ports=None, output_ports=None)
The ConfigurationContext provides utilities to communicate with KNIME during a node’s configure() method.
- property flow_variables: Dict[str, Any]
The flow variables coming in from KNIME as a dictionary with string keys.
Notes
The dictionary can be edited and supports flow variables of the following types:
bool
list[bool]
float
list[float]
int
list[int]
str
list[str]
- get_connected_input_port_numbers() List[int]
Gets the number of connected input ports for each port.
This method can be used to know how many input ports are connected to the node for each port.
- Returns:
A list of the number of connected input ports for each port type.
- Return type:
list of int
- get_connected_output_port_numbers() List[int]
Gets the number of connected output ports for each port.
This method can be used to know how many output ports are connected to the node for each port. This is relevant when using PortGroups to determine which ports have to be populated with data.
Examples
>>> @node(name="Example Node with Group Ports") ... @input_table(name="Input Data", description="The data to process in my node") ... @output_table_group(name="Output Data", description="Multiple outputs from my node") ... class ExampleNode(PythonNode): ... # ... ... def execute(self, exec_context, table)->List[knext.Schema]: ... output_table = table ... connected_output_ports = exec_context.get_connected_output_port_numbers() ... # When 2 output ports are connected, this will return [2]. ... output_tables = [output_table] * connected_output_ports[0] ... return output_tables # Thus, one list with two tables has to be returned.
- Returns:
A list of the number of connected output ports for each port type.
- Return type:
list of int
- get_credential_names()
Returns the identifier (flow variable name) for each credential.
- Returns:
A list of credential names.
- Return type:
list
- get_credentials(identifier: str) Credential
Returns the credentials dataclass for the given identifier.
- Parameters:
identifier (str) – The identifier of the credentials to retrieve.
- Returns:
A dataclass containing the credentials.
- Return type:
Credential
- set_warning(message: str) None
Sets a warning on the node.
- Parameters:
message (str) – The warning message to display on the node.
A node’s execute method receives an execution context that lets you interact with KNIME and e.g. check whether the user has cancelled the execution of your Python node.
- class knime.extension.ExecutionContext(java_ctx, flow_variables, input_ports=None, output_ports=None)
The ExecutionContext provides utilities to communicate with KNIME during a node’s execute() method.
- property flow_variables: Dict[str, Any]
The flow variables coming in from KNIME as a dictionary with string keys.
Notes
The dictionary can be edited and supports flow variables of the following types:
bool
list[bool]
float
list[float]
int
list[int]
str
list[str]
- get_connected_input_port_numbers() List[int]
Gets the number of connected input ports for each port.
This method can be used to know how many input ports are connected to the node for each port.
- Returns:
A list of the number of connected input ports for each port type.
- Return type:
list of int
- get_connected_output_port_numbers() List[int]
Gets the number of connected output ports for each port.
This method can be used to know how many output ports are connected to the node for each port. This is relevant when using PortGroups to determine which ports have to be populated with data.
Examples
>>> @node(name="Example Node with Group Ports") ... @input_table(name="Input Data", description="The data to process in my node") ... @output_table_group(name="Output Data", description="Multiple outputs from my node") ... class ExampleNode(PythonNode): ... # ... ... def execute(self, exec_context, table)->List[knext.Schema]: ... output_table = table ... connected_output_ports = exec_context.get_connected_output_port_numbers() ... # When 2 output ports are connected, this will return [2]. ... output_tables = [output_table] * connected_output_ports[0] ... return output_tables # Thus, one list with two tables has to be returned.
- Returns:
A list of the number of connected output ports for each port type.
- Return type:
list of int
- get_credential_names()
Returns the identifier (flow variable name) for each credential.
- Returns:
A list of credential names.
- Return type:
list
- get_credentials(identifier: str) Credential
Returns the credentials dataclass for the given identifier.
- Parameters:
identifier (str) – The identifier of the credentials to retrieve.
- Returns:
A dataclass containing the credentials.
- Return type:
Credential
- get_knime_home_dir() str
Returns the local absolute path to the directory in which KNIME stores its configuration as well as log files.
- Returns:
The local absolute path to the KNIME directory.
- Return type:
str
- get_workflow_data_area_dir() str
Returns the local absolute path to the current workflow’s data area folder. This folder is meant to be part of the workflow, so its contents are included whenever the workflow is shared.
- get_workflow_temp_dir() str
Returns the local absolute path where temporary files for this workflow should be stored. Files created in this folder are not automatically deleted by KNIME.
By default, this folder is located in the operating system’s temporary folder. In that case, the contents will be cleaned by the OS.
- is_canceled() bool
Returns true if this node’s execution has been canceled from KNIME. Nodes can check for this property and return early if the execution does not need to finish. Raising a RuntimeError in that case is encouraged.
- set_progress(progress: float, message: str | None = None)
Set the progress of the execution.
Note that the progress that can be set here is 80% of the total progress of a node execution. The first and last 10% are reserved for data transfer and will be set by the framework.
- Parameters:
progress (float) – A floating point number between 0.0 and 1.0.
message (str, optional) – An optional message to display in KNIME with the progress.
- set_warning(message: str) None
Sets a warning on the node.
- Parameters:
message (str) – The warning message to display on the node.
The dialog creation context is used to create dialogs for the configuration of the node. It can be accessed indirectly, by passing its method’s as arguments to specific parameters (see the example below).
- class knime.extension.DialogCreationContext(java_ctx, flow_variables, specs_to_python_converter)
The DialogCreationContext provides utilities to communicate with KNIME during the dialog creation phase. It enables access to the flow variables, the specs of the input tables and the credentials. These can be used to create the dialog elements, by passing the respective method as lambda function to the constructor of the string parameter class. The lambdas will receive the dialog creation context as parameter which should be passed as first parameter to the fully qualified method calls of DialogCreationContext as below:
Examples
>>> class ExampleNode: ... # This dialog element displays a dropdown with all available credentials ... string_param = knext.StringParameter(label="Credential parameter", description="Choices is a callable", ... choices=lambda a: knext.DialogCreationContext.get_credential_names(a))
- property flow_variables: Dict[str, Any]
The flow variables coming in from KNIME as a dictionary with string keys.
Notes
The dictionary can be edited and supports flow variables of the following types:
bool
list[bool]
float
list[float]
int
list[int]
str
list[str]
- get_credential_names()
Returns the identifier (flow variable name) for each credential.
- Returns:
A list of credential names.
- Return type:
list
- get_credentials(identifier: str) Credential
Returns the credentials dataclass for the given identifier.
- Parameters:
identifier (str) – The identifier of the credentials to retrieve.
- Returns:
A dataclass containing the credentials.
- Return type:
Credential
- get_flow_variables()
Returns the flow variables coming in from KNIME as a dictionary with string keys. The dictionary cannot be edited.
- Returns:
The flow variables dictionary with string keys.
- Return type:
dict
Notes
The supported flow variable types are: - bool - list(bool) - float - list(float) - int - list(int) - str - list(str)
- get_input_specs() List[PortObjectSpec]
Returns the specs for all input ports of the node.
- Returns:
A list of specs for all input ports.
- Return type:
List
Decorators
These decorators can be used to easily configure your Python node.
- knime.extension.node(name: str, node_type: NodeType, icon_path: str, category: str, after: str | None = None, keywords: List[str] | None = None, id: str | None = None, is_deprecated: bool = False, is_hidden: bool = False) Callable
Use this decorator to annotate a PythonNode class or function that creates a PythonNode instance that should correspond to a node in KNIME.
- Parameters:
name (str) – The name of the node
node_type (NodeType) – Type can be Source, Sink, Learner, Predictor, Manipulator, Visualizer or Other.
icon_path (str) – String to icon if no path is given it has no icon.
category (str) – Category to which the node will belongs to. The node will appear under this category. E.g. community.
after (Optional[str]) – If given the node will be listed after the specified node.
keywords (Optional[List[str]]) – Keywords describing your node which will help finding the node during search.
id (str) – Id of your node.
is_deprecated (bool) – Default is false.
is_hidden (bool) – Default is false. If true your node will not shown during search and not be listed in it’s category.
- Returns:
Returns a function which will decorate your node implementation with the set parameters.
- Return type:
Callable
Port Decorators
Port decorators in the KNIME Python extension allow you to define input and output ports for your nodes. Each port is of a specific type, such as “Table” or “Binary”, and is used for nodes that require a fixed number of inputs or outputs. We recommend using unique and descriptive names for each port.
- knime.extension.input_table(name: str, description: str, optional: bool | None = False)
Use this decorator to define an input port of type “Table” of a node.
- Parameters:
name (str) – The name of the input port.
description (str) – A description of the input port.
optional (bool) – Whether the port is optional i.e. can be added by the user
- knime.extension.input_binary(name: str, description: str, id: str, optional: bool | None = False)
Use this decorator to define a bytes-serialized port object input of a node.
- Parameters:
name (str) – The name of the input port.
description (str) – A description of the input port.
id (str) – A unique ID identifying the type of the Port. Only Ports with equal ID can be connected in KNIME.
- knime.extension.input_port(name: str, description: str, port_type: PortType, optional: bool | None = False)
Use this decorator to add an input port of the provided type to a node.
- Parameters:
name (str) – The name of the input port.
description (str) – A description of the input port.
port_type (PortType) – The type of the input port.
optional (bool) – Whether the port is optional i.e. can be added by the user
- knime.extension.output_table(name: str, description: str)
Use this decorator to define an output port of type “Table” of a node.
- Parameters:
name (str) – The name of the port.
description (str) – Description of what the port is used for.
- knime.extension.output_image(name: str, description: str)
Use this decorator to define an output port of the type “Image” of a node.
The configure method must return specs of the type ImagePortObjectSpec. The execute method must return a bytes object containing the image data. Note that the image data must be valid for the format defined in configure.
- Parameters:
name (str) – The name of the image output port
description (str) – Description of the image output port
Examples
>>> @knext.node(...) ... @knext.output_image( ... name="PNG Output Image", ... description="An example PNG output image") ... @knext.output_image( ... name="SVG Output Image", ... description="An example SVG output image") ... class ImageNode: ... def configure(self, config_context): ... return ( ... knext.ImagePortObjectSpec(knext.ImageFormat.PNG), ... knext.ImagePortObjectSpec(knext.ImageFormat.SVG), ... ) ... ... def execute(self, exec_context): ... # create a plot ... ... buffer_png = io.BytesIO() ... plt.savefig(buffer_png, format="png") ... ... buffer_svg = io.BytesIO() ... plt.savefig(buffer_svg, format="svg") ... ... return ( ... buffer_png.getvalue(), ... buffer_svg.getvalue(), ... )
- knime.extension.output_binary(name: str, description: str, id: str)
Use this decorator to define a bytes-serialized port object output of a node.
- Parameters:
name (str) – The name of the port.
description (str) – The description of the port.
id (str) – A unique ID identifying the type of the Port. Only Ports with equal ID can be connected in KNIME.
- knime.extension.output_port(name: str, description: str, port_type: PortType)
Use this decorator to add an output port of the provided type to a node.
- Parameters:
name (str) – The name of the port.
description (str) – Description of what the port is used for.
port_type (type) – The type of the port to add.
- knime.extension.output_view(name: str, description: str, static_resources: str | None = None, index_html_path: str | None = None)
Use this decorator to specify that this node produces a view.
- Parameters:
name (str) – The name of the view.
description (str) – Description of the view.
static_resources (str, optional) – Path (relative to the extension root) to a folder of static resources (e.g., JS, CSS, images, HTML files) that will be made available to the view. All text-based resources must be UTF-8 encoded.
index_html_path (str, optional) – Path (relative to the static_resources folder) to the HTML file that should be used as the entry point for the view (e.g., “index.html”). If specified, the view will be rendered using this static HTML file and its assets from static_resources. If not specified, the node’s execute method must return a view object.
Notes
Use static_resources to provide all static files needed for your view (including HTML, JS, CSS, images, etc.).
Use index_html_path to specify which HTML file inside static_resources should be used as the entry point for a static view.
If index_html_path is not provided, the node is expected to return a view object from its execute method.
Examples
Dynamic view (no static resources, returns a view object from execute): >>> @knext.output_view( … name=”Dynamic View”, … description=”Shows a dynamic view generated in Python.” … )
Static HTML/JS view with resources: >>> @knext.output_view( … name=”Custom Static View”, … description=”Shows a static HTML/JS visualization.”, … static_resources=”my_view_assets”, … index_html_path=”index.html” … )
Port Group Decorators
The group decorators allow you to define input and output port groups for your nodes. A port group means that you can dynamically add and remove ports in KNIME.
- knime.extension.input_table_group(name: str, description: str)
Use this decorator to define an input port group of type “Table” of a node.
- Parameters:
name (str) – The name of the input port group. Must be unique for all input port groups.
description (str) – A description of the input port group.
Examples
>>> @knext.node( ... name="Example Node with Multiple Input Table Groups", ... node_type=knext.NodeType.MANIPULATOR, ... icon_path="icon.png", ... category="community/example" ... ) ... @knext.input_table_group( ... name="First Input Tables", ... description="The first group of input tables for processing" ... ) ... @knext.input_table_group( ... name="Second Input Tables", ... description="The second group of input tables for processing" ... ) ... @knext.output_table_group( ... name="Output Tables", ... description="Combined results of the input tables" ... ) ... class ConcatenateNode(knext.PythonNode): ... def configure(self, config_context, first_table_specs: List[knext.Schema], second_table_specs: List[knext.Schema]) -> Iterable[List[knext.Schema]]: ... return first_table_specs + second_table_specs ... ... def execute(self, exec_context, first_tables: List[knext.Table], second_tables: List[knext.Table]) -> Iterable[List[knext.Table]]: ... return first_tables + second_tables
- knime.extension.input_binary_group(name: str, description: str, id: str | None = None)
Use this decorator to define an input port group of type “Binary” for a node.
- Parameters:
name (str) – The name of the input port group. Must be unique for all input port groups.
description (str) – A description of the input port group.
id (Optional[str]) – A unique ID identifying the type of the PortGroup. Only PortGroups with equal ID can be connected in KNIME.
- knime.extension.output_table_group(name: str, description: str)
Use this decorator to define an output port group of type “Table” of a node.
- Parameters:
name (str) – The name of the output port. Must be unique for all output port groups.
description (str) – A description of the output port.
- knime.extension.output_image_group(name: str, description: str)
Use this decorator to define an output port group of the type “Image” of a node.
The configure method must return specs of the type ImagePortObjectSpec. The execute method must return a bytes object containing the image data. Note that the image data must be valid for the format defined in configure.
- Parameters:
name (str) – The name of the image output port. Must be unique for all output port groups.
description (str) – Description of the image output port
Examples
>>> @knext.node(...) ... @knext.output_image_group( ... name="PNG Output Image Group", ... description="An example PNG output image") ... @knext.output_image_group( ... name="SVG Output Image Group", ... description="An example SVG output image") ... class ImageNode: ... def configure(self, config_context): ... # if 2 ports are connected per group, we will have 2 PNG and 2 SVG outputs ... port_numbers = config_context.get_connected_output_port_numbers() ... return ( ... [knext.ImagePortObjectSpec(knext.ImageFormat.PNG]*port_numbers[0], ... [knext.ImagePortObjectSpec(knext.ImageFormat.SVG)]*port_numbers[1], ... ) ... ... def execute(self, exec_context): ... # create a plot ... ... buffer_png = io.BytesIO() ... plt.savefig(buffer_png, format="png") ... ... buffer_svg = io.BytesIO() ... plt.savefig(buffer_svg, format="svg") ... ... return ( ... [buffer_png.getvalue()]*2, ... [buffer_svg.getvalue()]*2, ... )
- knime.extension.output_binary_group(name: str, description: str, id: str | None = None)
Use this decorator to define an output port group of type “Binary” for a node.
- Parameters:
name (str) – The name of the output port group. Must be unique for all output port groups.
description (str) – A description of the output port group.
id (Optional[str]) – A unique ID identifying the type of the PortGroup. Only PortGroups with equal ID can be connected in KNIME.
Parameters
To add parameterization to your nodes, the configuration dialog can be defined and customized. Each parameter can be
used in the nodes execution by accessing self.param_name. These parameters can be set up by using
the following parameter types. For a more detailed description see
Defining the node’s configuration dialog.
- class knime.extension.IntParameter(label: str | None = None, description: str | None = None, default_value: int | Callable[[Version], int] = 0, validator: Callable[[int], None] | None = None, min_value: int | None = None, max_value: int | None = None, since_version: Version | str | None = None, is_advanced: bool = False)
Parameter class for primitive integer types.
- class knime.extension.DoubleParameter(label: str | None = None, description: str | None = None, default_value: float | Callable[[Version], float] = 0.0, validator: Callable[[float], None] | None = None, min_value: float | None = None, max_value: float | None = None, since_version: Version | str | None = None, is_advanced: bool = False)
Parameter class for primitive float types.
- class knime.extension.BoolParameter(label: str | None = None, description: str | None = None, default_value: bool | Callable[[Version], bool] = False, validator: Callable[[bool], None] | None = None, since_version: Version | str | None = None, is_advanced: bool = False)
Parameter class for primitive boolean types.
- class knime.extension.StringParameter(label: str | None = None, description: str | None = None, default_value: str | Callable[[Version], str] = '', enum: List[str] | None = None, validator: Callable[[str], None] | None = None, since_version: Version | str | None = None, is_advanced: bool = False, choices: Callable | None = None)
Parameter class for primitive string types.
Dynamic Choices
The optional
choicescallable may return either a list/tuple of plain strings orStringParameter.Choiceobjects. Plain strings are interpreted as choices with identical id and label and an empty description.Choiceobjects allow specifying a user-facing label and an optional description while persisting only the stableid. Descriptions (if any) are aggregated into the parameter description under an “Available options” section. The section is included only if at least one dynamic choice supplies a non-empty description.Examples
>>> def basic(ctx): ... return ["A", "B"] ... >>> def rich(ctx): ... return [ ... StringParameter.Choice("A", "Option A", "Description for A"), ... StringParameter.Choice("B", "Option B"), # no description ... ] ... >>> param = knext.StringParameter( ... label="Example", ... description="Pick an option.", ... choices=rich, # alternatively: choices=basic ... default_value="A", ... )
- class Choice(id: str, label: str, description: str = '')
- class knime.extension.ColumnParameter(label: str | None = None, description: str | None = None, port_index: int = 0, column_filter: Callable[[Column], bool] | None = None, include_row_key: bool = False, include_none_column: bool = False, since_version: str | None = None, is_advanced: bool = False, schema_provider=None, default_value: str | Callable[[Version], str] | None = None)
Parameter class for single columns.
- class knime.extension.MultiColumnParameter(label: str | None = None, description: str | None = None, port_index: int | None = 0, column_filter: Callable[[Column], bool] | None = None, since_version: Version | str | None = None, is_advanced: bool = False)
Parameter class for multiple columns.
- class knime.extension.ColumnFilterParameter(label: str | None = None, description: str | None = None, port_index: int | None = 0, default_value: ColumnFilterConfig | Callable[[Version], ColumnFilterConfig] | None = None, column_filter: Callable[[Column], bool] | None = None, since_version: Version | str | None = None, is_advanced: bool = False, schema_provider=None)
Parameter class that supports full column filtering for columns.
- class knime.extension.ColumnFilterConfig(mode=ColumnFilterMode.MANUAL, pattern_filter: PatternFilterConfig | None = None, type_filter: TypeFilterConfig | None = None, manual_filter: ManualFilterConfig | None = None, included_column_names: List[str] | None = None, pre_filter: Callable[[Column], bool] | None = None)
The value of a ColumnFilterParameter is a ColumnFilterConfig instance with a mode as well as configuration for the different modes.
Use the apply method to filter schemas and tables according to this filter config
Examples
>>> @knext.node( ... name="Python Column Filter", ... node_type=knext.NodeType.MANIPULATOR, ... icon_path=..., ... category=..., ... ) ... @knext.input_table("Input Table", "Input table.") ... @knext.output_table("Output Table", "Output table.") ... class ColumnFilterNode: ... column_filter = knext.ColumnFilterParameter("Column Filter", "Column Filter") ... ... def configure(self, config_context, input_schema: knext.Schema): ... return self.column_filter.apply(input_schema) ... ... def execute(self, exec_context, input_table): ... return self.column_filter.apply(input_table)
- apply(columnar: _Columnar) _Columnar
Filter a table schema or a table according to this column filter configuration.
- class knime.extension.EnumParameter(label: str | None = None, description: str | None = None, default_value: str | EnumParameterOptions | Callable[[Version], str | EnumParameterOptions] | None = None, enum: EnumParameterOptions | None = None, validator: Callable[[str], None] | None = None, since_version: Version | str | None = None, is_advanced: bool = False, style: Style | None = None, hidden_choices: Callable[[Any | None], List[EnumParameterOptions]] | None = None)
Parameter class for multiple-choice parameter types. Replicates and extends the enum functionality previously implemented as part of StringParameter.
A subclass of EnumParameterOptions should be provided as the enum parameter, which should contain class attributes of the form OPTION_NAME = (OPTION_LABEL, OPTION_DESCRIPTION). The corresponding option attributes can be accessed via MyOptions.OPTION_NAME.name, .label, and .description respectively.
The .name attribute of each option is used as the selection constant, e.g. MyOptions.OPTION_NAME.name == “OPTION_NAME”.
Examples
>>> class CoffeeOptions(EnumParameterOptions): ... CLASSIC = ("Classic", "The classic chocolatey taste, with notes of bitterness and wood.") ... FRUITY = ("Fruity", "A fruity taste, with notes of berries and citrus.") ... WATERY = ("Watery", "A watery taste, with notes of water and wetness.") ... ... coffee_selection_param = knext.EnumParameter( ... label="Coffee Selection", ... description="Select the type of coffee you like to drink.", ... default_value=CoffeeOptions.CLASSIC.name, ... enum=CoffeeOptions, ... )
Dynamic Filtering
The optional
hidden_choicescallable allows filtering which enum members are hidden in the dialog and node description based on the runtime context. This is useful for hiding options that are not applicable given the current input data.The callable receives a
DialogCreationContext(orNoneif the node is not connected or during node description generation at startup) and must return a list of enum members to hide. IfNoneor an empty list is returned, all options are shown. If the callable returns only invalid members or all members, a warning is logged.Important: Validation accepts any enum member regardless of filtering. This ensures that saved workflows remain valid even when the context changes and different options are filtered.
The
DialogCreationContextparameter can beNonein two scenarios:During node description generation at KNIME startup
When the node has no input connections
Your callable should handle
Nonegracefully. By returning different options based on whether the context isNone, you can control what appears in the node description versus the dialog. For example, returning items to hide whenctx is Noneeffectively provides static filtering in the description while still allowing dynamic filtering in the dialog based on actual input data.>>> class ModelOptions(EnumParameterOptions): ... LINEAR = ("Linear Regression", "Fits a linear model") ... RANDOM_FOREST = ("Random Forest", "Ensemble tree model") ... NEURAL_NET = ("Neural Network", "Deep learning model") ... ... def hide_by_model_support(context): ... # Handle None context (no connection or description generation) ... if context is None: ... # Hide advanced option in description ... return [ModelOptions.NEURAL_NET] ... ... # Get input specifications for dialog filtering ... specs = context.get_input_specs() ... if not specs: ... return [] # Hide nothing, show all ... ... # Filter based on model capabilities from input ... model_spec = specs[0] # Assuming model is first input ... supported = model_spec.get_supported_options() # Hypothetical method ... ... return [opt for opt in ModelOptions if opt.name not in supported] ... ... model_param = knext.EnumParameter( ... label="Model Type", ... description="Select the model to use.", ... default_value=ModelOptions.LINEAR, # Can use enum member directly ... enum=ModelOptions, ... hidden_choices=hide_by_model_support, ... )
- class Style(value)
An enumeration.
- class knime.extension.EnumParameterOptions(value)
A helper class for creating EnumParameter options, based on Python’s Enum class.
Developers should subclass this class, and provide enumeration options as class attributes of the subclass, of the form
OPTION_NAME = (OPTION_LABEL, OPTION_DESCRIPTION).Enum option objects can be accessed as attributes of the EnumParameterOptions subclass, e.g.
MyEnum.OPTION_NAME. Each option object has the following attributes:name: the name of the class attribute, e.g. “OPTION_NAME”, which is used as the selection constant;
label: the label of the option, displayed in the configuration dialogue of the node;
description: the description of the option, used along with the label to generate a list of the available options in the Node Description and in the configuration dialogue of the node.
Examples
>>> class CoffeeOptions(EnumParameterOptions): ... CLASSIC = ("Classic", "The classic chocolatey taste, with notes of bitterness and wood.") ... FRUITY = ("Fruity", "A fruity taste, with notes of berries and citrus.") ... WATERY = ("Watery", "A watery taste, with notes of water and wetness.")
- classmethod get_all_options()
Returns a list of all options defined in the EnumParameterOptions subclass.
- class knime.extension.EnumSetParameter(label: str | None = None, description: str | None = None, default_value: List[str] | Callable[[Version], List[str]] | None = None, enum: EnumParameterOptions | None = None, validator: Callable[[str], None] | None = None, since_version: Version | str | None = None, is_advanced: bool = False)
Parameter class for multiple-choice parameter types. Expands the EnumParameter by enabling to select multiple enum constants.
A subclass of EnumParameterOptions should be provided as the enum parameter, which should contain class attributes of the form OPTION_NAME = (OPTION_LABEL, OPTION_DESCRIPTION). The corresponding option attributes can be accessed via MyOptions.OPTION_NAME.name, .label, and .description respectively.
The .name attribute of each option is used as the selection constant, e.g. MyOptions.OPTION_NAME.name == “OPTION_NAME”.
Examples
>>> class CoffeeOptions(EnumParameterOptions): ... CLASSIC = ("Classic", "The classic chocolatey taste, with notes of bitterness and wood.") ... FRUITY = ("Fruity", "A fruity taste, with notes of berries and citrus.") ... WATERY = ("Watery", "A watery taste, with notes of water and wetness.") ... ... coffee_selection_param = knext.EnumSetParameter( ... label="Coffee Selection", ... description="Select the types of coffee you like to drink.", ... default_value=[CoffeeOptions.CLASSIC.name, CoffeeOptions.FRUITY.name], ... enum=CoffeeOptions, ... )
- class knime.extension.DateTimeParameter(label: str | None = None, description: str | None = None, default_value: str | date | None = None, validator=None, min_value: str | date | None = None, max_value: str | date | None = None, since_version=None, is_advanced: bool = False, show_date: bool = True, show_time: bool = False, show_seconds: bool = False, show_milliseconds: bool = False, timezone: str | None = None, date_format: str | None = None)
- class knime.extension.LocalPathParameter(label: str | None = None, description: str | None = None, placeholder_text: str = '', validator: Callable[[str], None] | None = None, since_version: Version | str | None = None, is_advanced: bool = False)
Parameter class for local file path types. The path is represented as string.
- Raises:
TypeError – If the value is not a string.
ValueError – If the value is not a valid file path.
- class Choice(id: str, label: str, description: str = '')
- rule(condition: Condition, effect: Effect)
Add a rule that conditionally sets whether this parameter is visible or enabled in the dialog. This can be useful if this parameter should only be accessible if another parameter has a certain value.
Note
Rules can only depend on parameters on the same level, not in a child or parent parameter group.
Examples
>>> @knext.node(args) ... class MyNode: ... string_param = knext.StringParameter( ... "String Param Title", ... "String Param Title Description", ... "default value" ... ) ... ... # this parameter gets disabled if string_param is "foo" or "bar" ... int_param = knext.IntParameter( ... "Int Param Title", ... "Int Param Description", ... ).rule(knext.OneOf(string_param, ["foo", "bar"]), knext.Effect.DISABLE)
- validator(func)
To be used as a decorator for setting a validator function for a parameter. Note that ‘func’ will be encapsulated in ‘_validator’ and will not be available in the namespace of the class.
Examples
>>> @knext.node(args) ... class MyNode: ... num_repetitions = knext.IntParameter( ... label="Number of repetitions", ... description="How often to repeat an action", ... default_value=42 ... ) ... @num_repetitions.validator ... def validate_reps(value): ... if value > 100: ... raise ValueError("Too many repetitions!") ... ... def configure(args): ... pass ... ... def execute(args): ... pass
- class knime.extension.ParameterArray(parameters, label: str | None = None, description: str | None = None, validator: Callable[[Any], None] | None = None, since_version: Version | str | None = None, is_advanced: bool = False, allow_reorder: bool = True, layout_direction: LayoutDirection = LayoutDirection.VERTICAL, button_text: str | None = None, array_title: str | None = None)
A parameter that represents an array of parameters. Each element in the array is an instance of a parameter group.
Example
>>> @knext.parameter_group(label="Coffee Selections") ... class CoffeeSelections: ... coffee_options = knext.StringParameter( ... "Coffee Options", ... "Enter the type of coffee you like to drink.", ... default_value="Watery", ... ) ... ... number_of_cups = knext.IntParameter( ... "Number of Cups", ... "Enter the number of cups of coffee you usually drink.", ... default_value=5, ... ) ... ... coffee_selection = knext.ParameterArray( ... label="Coffee Selections", ... description="Select the type of coffee you like to drink.", ... parameters=CoffeeSelections(), ... since_version="5.3.1", ... button_text="Add new selection", ... array_title="Selections", ... )
- rule(condition: Condition, effect: Effect)
Add a rule that conditionally sets whether this parameter is visible or enabled in the dialog. This can be useful if this parameter should only be accessible if another parameter has a certain value.
Note
Rules can only depend on parameters on the same level, not in a child or parent parameter group.
Examples
>>> @knext.node(args) ... class MyNode: ... string_param = knext.StringParameter( ... "String Param Title", ... "String Param Title Description", ... "default value" ... ) ... ... # this parameter gets disabled if string_param is "foo" or "bar" ... int_param = knext.IntParameter( ... "Int Param Title", ... "Int Param Description", ... ).rule(knext.OneOf(string_param, ["foo", "bar"]), knext.Effect.DISABLE)
- validator(func)
To be used as a decorator for setting a validator function for a parameter. Note that ‘func’ will be encapsulated in ‘_validator’ and will not be available in the namespace of the class.
Examples
>>> @knext.node(args) ... class MyNode: ... num_repetitions = knext.IntParameter( ... label="Number of repetitions", ... description="How often to repeat an action", ... default_value=42 ... ) ... @num_repetitions.validator ... def validate_reps(value): ... if value > 100: ... raise ValueError("Too many repetitions!") ... ... def configure(args): ... pass ... ... def execute(args): ... pass
Validation
While each parameter type listed above has default type validation (eg checking if the IntParameter contains only Integers), they also support custom validation via a property-like decorator notation. For instance, this can be used to verify that the parameter value matches a certain criteria (see example below). The validator should be placed below the definition of the corresponding parameter.
- class knime.extension.IntParameter(label: str | None = None, description: str | None = None, default_value: int | Callable[[Version], int] = 0, validator: Callable[[int], None] | None = None, min_value: int | None = None, max_value: int | None = None, since_version: Version | str | None = None, is_advanced: bool = False)
Parameter class for primitive integer types.
- validator(func)
To be used as a decorator for setting a validator function for a parameter. Note that ‘func’ will be encapsulated in ‘_validator’ and will not be available in the namespace of the class.
Examples
>>> @knext.node(args) ... class MyNode: ... num_repetitions = knext.IntParameter( ... label="Number of repetitions", ... description="How often to repeat an action", ... default_value=42 ... ) ... @num_repetitions.validator ... def validate_reps(value): ... if value > 100: ... raise ValueError("Too many repetitions!") ... ... def configure(args): ... pass ... ... def execute(args): ... pass
Parameter Visibility Rules
By default, each parameter of a node is visible in the node’s configuration dialog. Parameters can be
marked as advanced by setting is_advanced=True, which will only show them once the user has clicked
“Show advanced settings” in the configuration dialog.
Sometimes a parameter should only be visible to the user if another parameter has a certain value. For this,
each parameter type listed above has a method rule. In this method, one can specify a condition based on
another parameter, which we call subject, and the effect that should be applied to this parameter when
the condition becomes true.
- class knime.extension.IntParameter(label: str | None = None, description: str | None = None, default_value: int | Callable[[Version], int] = 0, validator: Callable[[int], None] | None = None, min_value: int | None = None, max_value: int | None = None, since_version: Version | str | None = None, is_advanced: bool = False)
Parameter class for primitive integer types.
- rule(condition: Condition, effect: Effect)
Add a rule that conditionally sets whether this parameter is visible or enabled in the dialog. This can be useful if this parameter should only be accessible if another parameter has a certain value.
Note
Rules can only depend on parameters on the same level, not in a child or parent parameter group.
Examples
>>> @knext.node(args) ... class MyNode: ... string_param = knext.StringParameter( ... "String Param Title", ... "String Param Title Description", ... "default value" ... ) ... ... # this parameter gets disabled if string_param is "foo" or "bar" ... int_param = knext.IntParameter( ... "Int Param Title", ... "Int Param Description", ... ).rule(knext.OneOf(string_param, ["foo", "bar"]), knext.Effect.DISABLE)
- class knime.extension.Condition
Abstract base class for all condition types of parameter visibility rules.
- class knime.extension.And(*conditions: Condition)
A Condition that combines other Conditions with AND.
- class knime.extension.Contains(subject: Any, value: Any)
A Condition that evaluates to true if one of the values of the
subjectparameter is equal to the expectedvalue.
- class knime.extension.OneOf(subject: Any, values: List[Any])
A Condition that evaluates to true if the value of the
subjectparameter is equal to one of the expectedvalues.
- class knime.extension.Or(*conditions: Condition)
A Condition that combines other Conditions with OR.
- class knime.extension.Effect(value)
Encodes the effect a rule may cause.
- DISABLE = 'DISABLE'
Disable the parameter if the condition is true
- ENABLE = 'ENABLE'
Enable the parameter if the condition is true
- HIDE = 'HIDE'
Hide the parameter if the condition is true
- SHOW = 'SHOW'
Show the parameter if the condition is true
Parameter Groups
Additionally these parameters can be combined in parameter_groups. These groups are visualized as sections in the
configuration dialog. Another benefit of defining parameter groups is the ability to provide group validation.
As opposed to only being able to validate a single value when attaching a validator to a parameter, group validators
have access to the values of all parameters contained in the group, allowing for more complex validation routines.
- knime.extension.parameter_group(label: str, since_version: Version | str | None = None, is_advanced: bool = False, layout_direction: LayoutDirection = LayoutDirection.VERTICAL)
Decorator for classes implementing parameter groups. Parameter group classes can define parameters and other parameter groups both as class-level attributes and as instance-level attributed inside the __init__ method.
Parameter group classes can set values for their parameters inside the __init__ method during the constructor call (e.g. from the node containing the group, or another group). Note: when declaring the keyword arguments for the __init__ method of your parameter group class, you should refrain from using keywords from the following list of reserved keywords: since_version, is_advanced, and validator. These are used by the wrapper class in order to enable the backend functionality.
Group validators need to raise an exception if a values-based condition is violated, where values is a dictionary of parameter names and values. Group validators can be set using either of the following methods:
By implementing the “validate(self, values)” method inside the class definition of the group.
Examples
>>> def validate(self, values): ... assert values['first_param'] + values['second_param'] < 100
By using the “@group_name.validator” decorator notation inside the class definition of the “parent” of the group. The decorator has an optional ‘override’ parameter, set to True by default, which overrides the “validate” method. If ‘override’ is set to False, the “validate” method, if defined, will be called first.
>>> @hyperparameters.validator(override=False) ... def validate_hyperparams(values): ... assert values['first_param'] + values['second_param'] < 100
or
>>> @knext.parameter_group(label="My Settings") ... class MySettings: ... name = knext.StringParameter("Name", "The name of the person", "Bario") ... num_repetitions = knext.IntParameter("NumReps", "How often do we repeat?", 1, min_value=1) ... ... @num_repetitions.validator ... def reps_validator(value): ... if value == 2: ... raise ValueError("I don't like the number 2") ... ... @knext.node(args) ... class MyNodeWithSettings: ... settings = MySettings() ... def configure(args): ... pass ... ... def execute(args): ... pass
Tables
Table and Schema are the two classes that are used to communicate tabular data (Table) during execute,
or the table structure (Schema) in configure between Python and KNIME.
- class knime.extension.Table
This class serves as public API to create KNIME tables either from pandas or pyarrow. These tables can than be sent back to KNIME. This class has to be instantiated by calling either
from_pyarrow()orfrom_pandas()- __getitem__(slicing: slice | List[int] | List[str] | Tuple[slice | List[int] | List[str], slice]) _TabularView
Creates a view of this Table by slicing rows and columns. The slicing syntax is similar to that of numpy arrays, but columns can also be addressed as index lists or via a list of column names.
Notes
The syntax is [column_slice, row_slice]. Note that this is the exact opposite order than in the deprecated scripting API’s ReadTable.
- Parameters:
column_slice (int, str, slice, list) – A column index, a column name, a slice object, a list of column indices, or a list of column names.
row_slice (slice, optional) – A slice object describing which rows to use.
- Returns:
A _TabularView representing a slice of the original Table.
- Return type:
TabularView
Examples
>>> row_sliced_table = table[:, :100] # Get the first 100 rows ... column_sliced_table = table[["name", "age"]] # Get all rows of the columns "name" and "age" ... row_and_column_sliced_table = table[1:5, :100] # Get the first 100 rows of columns 1,2,3,4
- append(other: _Columnar | Sequence[_Columnar]) _ColumnarView
Append another _Columnar object (e.g. Table, Schema) or a sequence of _Columnar objects to the current _Columnar object.
- Parameters:
other (Union["_Columnar", Sequence["_Columnar"]]) – The _Columnar object or a sequence of _Columnar objects to be appended.
- Returns:
A _ColumnarView object representing the current _Columnar object after the append operation.
- Return type:
_ColumnarView
- batches() Iterator[Table]
Returns a generator over the batches in this table. A batch is part of the table with all columns, but only a subset of the rows. A batch should always fit into memory (max size currently 64mb). The table being passed to execute() is already present in batches, so accessing the data this way is very efficient.
- Returns:
A generator object that yields batches of the table.
- Return type:
generator
Examples
>>> output_table = BatchOutputTable.create() ... for batch in my_table.batches(): ... input_batch = batch.to_pandas() ... # process the batch ... output_table.append(Table.from_pandas(input_batch))
- abstract property column_names: list
Get the names of the columns in a dataset.
- static from_pandas(data: pandas.DataFrame, sentinel: str | int | None = None, row_ids: str = 'auto')
Factory method to create a Table given a pandas.DataFrame. The index of the data frame will be used as RowKey by KNIME.
Examples
>>> Table.from_pandas(my_pandas_df, sentinel="min")
- Parameters:
data (pandas.DataFrame) – A pandas DataFrame.
sentinel (str, optional) –
Interpret the following values in integral columns as missing value:
"min": min int32 or min int64 depending on the type of the column"max": max int32 or max int64 depending on the type of the columna special integer value that should be interpreted as missing value
row_ids ({'keep', 'generate', 'auto'}, optional) –
Defines what RowID should be used. Must be one of the following values:
"keep": Keep theDataFrame.indexas the RowID. Convert the index to strings if necessary."generate": Generate new RowIDs of the formatf"Row{i}"whereiis the position of the row (from0tolength-1)."auto": If theDataFrame.indexis of type int or unsigned int, usef"Row{n}"wherenis the index of the row. Else, use “keep”.
- Returns:
The created Table object.
- Return type:
Table
- static from_pyarrow(data: pyarrow.Table, sentinel: str | int | None = None, row_ids: str = 'auto')
Factory method to create a Table given a pyarrow.Table.
All batches of the table must have the same number of rows. Only the last batch can have less rows than the other batches.
Examples
>>> Table.from_pyarrow(my_pyarrow_table, sentinel="min")
- Parameters:
data (pyarrow.Table) – A pyarrow.Table
sentinel (str) –
Interpret the following values in integral columns as missing value:
"min"min int32 or min int64 depending on the type of the column"max"max int32 or max int64 depending on the type of the columna special integer value that should be interpreted as missing value
row_ids (str) –
Defines what RowID should be used. Must be one of the following values:
"keep": Use the first column of the table as RowID. The first column must be of type string."generate": Generate new RowIDs of the formatf"Row{i}"whereiis the position of the row (from0tolength-1)."auto": Use the first column of the table if it has the name “<RowID>” and is of type string or integer.If the “<RowID>” column is of type string, use it directly
If the “<RowID>” column is of an integer type use
f"Row{n}wherenis the value of the integer column.Generate new RowIDs (
"generate") if the first column has another type or name.
- Returns:
The created Table instance.
- Return type:
pyarrow.Table
- insert(other: _Columnar, at: int) _Columnar
Insert a column or another _Columnar object (e.g. Table, Schema) into the current _Columnar object at a specific position.
- Parameters:
other (_Columnar or Column) – The column or _Columnar object to be inserted.
at (int) – The index at which the insertion should occur.
- Returns:
The _Columnar object after the insertion.
- Return type:
_Columnar
- Raises:
TypeError – If other is not of type _Columnar or Column.
Notes
The insertion is done in-place, meaning the current _Columnar object is modified.
- abstract property num_columns: int
Get the number of columns in the dataset.
- remove(slicing: str | int | List[str])
Implements remove method for Columnar data structures. The input can be a column index, a column name or a list of column names.
If the input is a column index, the column with that index will be removed. If it is a column name, then the first column with matching name is removed. Passing a list of column names will filter out all (including duplicate) columns with matching names.
- Parameters:
slicing (int | list | str) – Can be of type integer representing the index in column_names to remove. Or a list of strings removing every column matching from that list. Or a string of which first occurrence is removed from the column_names.
- Returns:
_ColumnarView
- Return type:
A View missing the columns to be removed.
- Raises:
ValueError – If no matching column is found given a list or str.:
IndexError – If column is accessed by integer and is out of bounds.:
TypeError – If the key is neither an integer nor a string or list of strings.:
- abstract property schema: Schema
The schema of this table, containing column names, types, and potentially metadata
- to_batches() Iterator[Table]
Alias for Table.batches()
- to_pandas(sentinel: str | int | None = None) pandas.DataFrame
Access this table as a pandas.DataFrame.
- Parameters:
sentinel (str or int) –
Replace missing values in integral columns by the given value. It can be one of the following:
"min"min int32 or min int64 depending on the type of the column"max"max int32 or max int64 depending on the type of the columnAn integer value that should be inserted for each missing value
- to_pyarrow(sentinel: str | int | None = None) pyarrow.Table
Access this table as a pyarrow.Table.
- Parameters:
sentinel (str or int) –
Replace missing values in integral columns by the given value, which can be one of the following:
”min”: minimum value of int32 or int64 depending on the type of the column
”max”: maximum value of int32 or int64 depending on the type of the column
An integer value that should be inserted for each missing value
- class knime.extension.BatchOutputTable
An output table generated by combining smaller tables (also called batches).
Notes
All batches must have the same number, names and types of columns.
All batches except the last batch must have the same number of rows.
The last batch can have fewer rows than the other batches.
This object does not provide means to continue to work with the data but is meant to be used as a return value of a Node’s execute() method.
- abstract append(batch: Table | pandas.DataFrame | pyarrow.Table | pyarrow.RecordBatch) None
Append a batch to this output table. The first batch defines the structure of the table, and all subsequent batches must have the same number of columns, column names and column types.
Notes
Keep in mind that the RowID will be handled according to the “row_ids” mode chosen in BatchOutputTable.create.
- static create(row_ids: str = 'keep')
Create an empty BatchOutputTable
- Parameters:
row_ids (str) –
Defines what RowID should be used. Must be one of the following values:
”keep”:
For appending DataFrames: Keep the DataFrame.index as the RowID. Convert the index to strings if necessary.
For appending Arrow tables or record batches: Use the first column of the table as RowID. The first column must be of type string.
”generate”: Generate new RowIDs of the format “Row{i}”
- static from_batches(generator, row_ids: str = 'generate')
Create output table where each batch is provided by a generator
- Parameters:
row_ids (object) – See BatchOutputTable.create.
- abstract property num_batches: int
The number of batches written to this output table.
- class knime.extension.Schema(ktypes: List[KnimeType | Type], names: List[str], metadata: List | None = None)
A schema defines the data types and names of the columns inside a table. Additionally, it can hold metadata for the individual columns.
- __getitem__(slicing: slice | List[int] | List[str]) _ColumnarView
Creates a view of this Table or Schema by slicing columns. The slicing syntax is similar to that of numpy arrays, but columns can also be addressed as index lists or via a list of column names.
- Parameters:
slicing (int, str, slice, list) – A column index, a column name, a slice object, a list of column indices, or a list of column names. For single indices, the view will create a “Column” object. For slices or lists of indices, a new Schema will be returned.
- Returns:
A representation of a slice of the original Schema or Table.
- Return type:
_ColumnarView
Examples
>>> # Get columns 1,2,3,4 ... sliced_schema = schema[1:5]
>>> # Get the columns "name" and "age" ... sliced_schema = schema[["name", "age"]]
- append(other: _Columnar | Sequence[_Columnar]) _ColumnarView
Append another _Columnar object (e.g. Table, Schema) or a sequence of _Columnar objects to the current _Columnar object.
- Parameters:
other (Union["_Columnar", Sequence["_Columnar"]]) – The _Columnar object or a sequence of _Columnar objects to be appended.
- Returns:
A _ColumnarView object representing the current _Columnar object after the append operation.
- Return type:
_ColumnarView
- property column_names: List[str]
Get the names of the columns in a dataset.
- classmethod deserialize(table_schema: dict) Schema
Construct a Schema from a dict that was retrieved from KNIME in JSON encoded form as the input to a node’s configure() method. KNIME provides table information with a RowKey column at the beginning, which we drop before returning the created schema.
- classmethod from_columns(columns: Sequence[Column] | Column)
Create a schema from a single column or a list of columns.
- Parameters:
columns (Union[Sequence[Column], Column]) – A single column or a list of columns.
- Returns:
The constructed schema.
- Return type:
Schema
- classmethod from_types(ktypes: List[KnimeType | Type], names: List[str], metadata: List | None = None)
Create a schema from a list of column data types, names and metadata.
- Parameters:
ktypes (List[Union[KnimeType, Type]]) – A list of KNIME types or types known to KNIME.
names (List[str]) – A list of column names.
metadata (List, optional)
- Returns:
The constructed schema.
- Return type:
Schema
- insert(other: _Columnar, at: int) _Columnar
Insert a column or another _Columnar object (e.g. Table, Schema) into the current _Columnar object at a specific position.
- Parameters:
other (_Columnar or Column) – The column or _Columnar object to be inserted.
at (int) – The index at which the insertion should occur.
- Returns:
The _Columnar object after the insertion.
- Return type:
_Columnar
- Raises:
TypeError – If other is not of type _Columnar or Column.
Notes
The insertion is done in-place, meaning the current _Columnar object is modified.
- property num_columns
Get the number of columns in the dataset.
- remove(slicing: str | int | List[str])
Implements remove method for Columnar data structures. The input can be a column index, a column name or a list of column names.
If the input is a column index, the column with that index will be removed. If it is a column name, then the first column with matching name is removed. Passing a list of column names will filter out all (including duplicate) columns with matching names.
- Parameters:
slicing (int | list | str) – Can be of type integer representing the index in column_names to remove. Or a list of strings removing every column matching from that list. Or a string of which first occurrence is removed from the column_names.
- Returns:
_ColumnarView
- Return type:
A View missing the columns to be removed.
- Raises:
ValueError – If no matching column is found given a list or str.:
IndexError – If column is accessed by integer and is out of bounds.:
TypeError – If the key is neither an integer nor a string or list of strings.:
- serialize() Dict
Convert this Schema into dict which can then be JSON encoded and sent to KNIME as result of a node’s configure() method. Because KNIME expects a row key column as first column of the schema, but we don’t include this in the KNIME Python table schema, we insert a row key column here.
- Raises:
RuntimeError – if duplicate column names are detected:
- class knime.extension.Column(ktype: KnimeType | Type, name: str, metadata: dict | None = None)
A column inside a table schema consists of the KNIME datatype, a column name, and optional metadata.
- __init__(ktype: KnimeType | Type, name: str, metadata: dict | None = None)
Construct a Column from type, name and optional metadata.
- Parameters:
ktype (Union[KnimeType, Type]) – The KNIME type of the column or a type which can be converted via knime.api.schema.logical(ktype) to a KNIME type. Raises a TypeError if the type is not a KNIME type or cannot be converted to a KNIME type.
name (str) – The name of the column. May not be empty. Raises a ValueError if the name is empty.
metadata (dict, optional) – Metadata of this column.
- Returns:
The constructed column.
- Return type:
Column
- Raises:
TypeError – If the type is not a KNIME type or cannot be converted to a KNIME type.
ValueError – If the name is empty.
Data Types
These are helper functions to create KNIME compatible datatypes. For instance, if a new column is created.
- knime.extension.int32()
Create a KNIME integer type with 32 bits.
- knime.extension.int64()
Create a KNIME integer type with 64 bits
- knime.extension.double()
Create a KNIME floating point type with double precision (64 bits).
- knime.extension.bool_()
Create a KNIME boolean type.
- knime.extension.string(dict_encoding_key_type: DictEncodingKeyType | None = None)
Create a KNIME string type.
- Parameters:
dict_encoding_key_type (DictEncodingKeyType, optional) – The key type to use for dictionary encoding. If this is None (the default), no dictionary encoding will be used. Dictionary encoding helps to reduce storage space and read/write performance for columns with repeating values such as categorical data.
- knime.extension.blob(dict_encoding_key_type: DictEncodingKeyType | None = None)
Create a KNIME blob type for binary data of variable length.
- Parameters:
dict_encoding_key_type (DictEncodingKeyType, optional) – The key type to use for dictionary encoding. If this is None (the default), no dictionary encoding will be used. Dictionary encoding helps to reduce storage space and read/write performance for columns with repeating values such as categorical data.
- knime.extension.list_(inner_type: KnimeType)
Create a KNIME type that is a list of the given inner types
- Parameters:
inner_type (KnimeType) – The type of the elements in the list. Must be a KnimeType.
- knime.extension.struct(*inner_types)
Create a KNIME structured data type where each given argument represents a field of the struct.
- Parameters:
inner_types (list) – The argument list of this method defines the fields in this structured data type. Each inner type must be a KNIME type
- knime.extension.logical(value_type) LogicalType
Create a KNIME logical data type of the given Python value type.
- Parameters:
value_type (type) – The type of the values inside this column. A knime.api.types.PythonValueFactory must be registered for this type.
- Raises:
TypeError – If no PythonValueFactory has been registered for this value type with knime.api.types.register_python_value_factory.
- knime.extension.datetime(date: bool | None = True, time: bool | None = True, timezone: bool | None = False) LogicalType
- Currently, KNIME supports the following date/time formats:
Local DateTime (date=True, time=True, timezone=False)
Local Date (date=True, time=False, timezone=False)
Local Time (date=False, time=True, timezone=False)
Zoned DateTime (date=True, time=True, timezone=True)
- Parameters:
date (Optional[bool]) – Whether the column contains a date.
time (Optional[bool]) – Whether the column contains a time.
timezone (Optional[bool]) – Whether the column contains a timezone.
- Returns:
A LogicalType representing the given date/time format.
- Return type:
LogicalType
- Raises:
ValueError – If the combination of date, time and timezone is not supported or the datetime types are not registered in KNIME.
Views
- knime.scripting.io.view(obj) NodeView
Return a best-effort
NodeViewfor obj.The function attempts, in order, to
match obj to one of the dedicated helper functions listed below, or
fall back to the object’s IPython
_repr_*_methods (_repr_html_,_repr_svg_,_repr_png_,_repr_jpeg_).
Images - SVG, PNG, JPEG, matplotlib, seaborn - are exported with a static snapshot and therefore show up in Reports of the KNIME Reporting Extension outputs automatically. All other supported objects (HTML strings, Plotly figures, etc.) are not supported in reports by default. To enable them in reports, use the
view_html()function and set thecan_be_used_in_reportflag.Special view implementations
The input must match one of the following patterns:
HTML
strstarting with"<!DOCTYPE html>". Must be self-contained; external links open in a browser.SVG
strcontaining valid<svg … xmlns="…">markup.PNG
bytesbeginning with the PNG magic number.JPEG
bytesbeginning0xFFD8FFand ending0xFFD9.Matplotlib
matplotlib.figure.Figureinstance.Plotly
plotly.graph_objects.Figureinstance.
- param obj:
The object to visualise.
- type obj:
Any
- raises ValueError:
If no suitable helper or
_repr_*_method is found.
- knime.scripting.io.view_matplotlib(fig=None, format='png') NodeView
Create a
NodeViewthat displays a matplotlib figure and is report-ready out of the box.The figure is exported as a PNG or SVG (controlled by format). If fig is None, the current active figure is used. The figure is then closed so it should not be modified afterwards.
Because a static image is always supplied, the helper sets
can_be_used_in_report=Trueautomatically, allowing the view to appear in reports generated by the KNIME Reporting Extension without additional JavaScript.- Parameters:
fig (matplotlib.figure.Figure, optional) – Figure to render. Defaults to the current figure.
format ({"png", "svg"}, default "png") – Output format embedded in the HTML snippet.
- Raises:
ImportError – If matplotlib is not available.
TypeError – If the figure is not a matplotlib figure.
- knime.scripting.io.view_seaborn() NodeView
Create a
NodeViewthat shows the current active seaborn figure and is report-ready out of the box.The function simply forwards to
view_matplotlib()because seaborn charts are regular matplotlib figures under the hood.As a static image is always embedded, the helper sets
can_be_used_in_report=Trueautomatically, so the view can be included in reports generated by the KNIME Reporting Extension without any extra JavaScript.- Raises:
ImportError – If matplotlib is not available.
- knime.scripting.io.view_plotly(fig) NodeView
Create a view showing the given plotly figure.
The figure is displayed by exporting it as an HTML document.
To be able to synchronize the selection between the view and other KNIME views the customdata of the figure traces must be set to the RowID.
- Parameters:
fig (plotly.graph_objects.Figure) – A plotly figure object which should be displayed.
- Raises:
ImportError – If plotly is not available.
TypeError – If the figure is not a plotly figure.
Examples
>>> fig = px.scatter(df, x="my_x_col", y="my_y_col", color="my_label_col", ... custom_data=[df.index]) ... node_view = view_plotly(fig)
- knime.scripting.io.view_html(html: str, svg_or_png: str | bytes | None = None, render_fn: Callable[[], str | bytes] | None = None, can_be_used_in_report=False) NodeView
Create a NodeView that displays the given HTML document.
The document must be self-contained and must not reference external resources. Links to external resources will be opened in an external browser.
- Parameters:
html (str) – A string containing the HTML document.
svg_or_png (str or bytes) – A rendered representation of the HTML page. Either a string containing an SVG or a bytes object containing a PNG image.
render_fn (callable) – A callable that returns an SVG or PNG representation of the page.
can_be_used_in_report (bool, default
False) – Indicates whether this view will appear in a report generated by the KNIME Reporting Extension. Only set the flag toTruewhen you provide the view to the knime-ui-extension-service ReportingService.
- knime.scripting.io.view_svg(svg: str) NodeView
Create a
NodeViewthat shows an SVG and is report-ready out of the box.- Parameters:
svg (str) – SVG markup (must include the
<svg ...>root element with the XML namespace declaration).
- knime.scripting.io.view_png(png: bytes) NodeView
Create a
NodeViewthat shows a PNG image and is report-ready out of the box.- Parameters:
png (bytes) – Raw PNG data.
- knime.scripting.io.view_jpeg(jpeg: bytes) NodeView
Create a
NodeViewthat shows a JPEG image and is report-ready out of the box.- Parameters:
jpeg (bytes) – Raw JPEG data.
- knime.scripting.io.view_ipy_repr(obj) NodeView
Create a NodeView by using the IPython _repr_*_ function of the object.
Tries to use:
_repr_html_
_repr_svg_
_repr_png_
_repr_jpeg_
in this order.
- Parameters:
obj (object) – The object which should be displayed.
- Raises:
ValueError – If no view could be created for the given object.
- class knime.scripting.io.NodeView(html: str, svg_or_png: str | bytes | None = None, render_fn: Callable[[], str | bytes] | None = None, can_be_used_in_report: bool = False)
A wrapper that embeds a visualisation in KNIME Analytics Platform.
Notes
Do not instantiate NodeView directly—use one of the helper functions (
view(),view_html(),view_svg(),view_png(),view_jpeg(), …).- Parameters:
html (str) – Self-contained HTML snippet that renders the interactive view inside KNIME AP.
svg_or_png (str | bytes | None, optional) – Static SVG or PNG/JPEG bytes handed to the Reporting engine when a report is generated. If None, the value is produced lazily via render_fn.
render_fn (Callable[[], str | bytes] | None, optional) – Callback that returns svg_or_png on-demand.
can_be_used_in_report (bool, default
False) –Set True to signal that this view can be embedded in a report produced by the KNIME Reporting Extension.
Image helpers (view_svg, view_png, view_jpeg, :pydata:`view_matplotlib`, …) turn the flag on automatically because they always provide the image to the Reporting engine.
HTML helpers leave the flag False by default. Only enable it when the view’s JavaScript sends a static representation to the ReportingService using
reportingService.setReportingContent(...). If you are not in control of the JavaScript or cannot ensure this, keep the flag False so the view is omitted from the report instead of breaking it.
Port Objects
Port Object Specs
- class knime.extension.PortObjectSpec
Base protocol for port object specs.
A PortObjectSpec must support conversion from/to a dictionary which is then encoded as JSON and sent to/from KNIME.
- abstract classmethod deserialize(data: Dict, java_callback=None)
Deserialize the port object.
- Parameters:
data (Dict) – The data dict created by serialize
java_callback (any) – A callback handler that allows to make callbacks to Java. Was introduced in 5.3 and can be omitted in derived classes.
- Returns:
The deserialized PortObjectSpec
- Return type:
PortObjectSpec
- class knime.extension.BinaryPortObjectSpec(id: str)
Port object spec for simple binary port objects.
BinaryPortObjectSpecs have an ID that is used to ensure that only ports with equal ID can be connected.
- classmethod deserialize(data)
Deserialize the port object.
- Parameters:
data (Dict) – The data dict created by serialize
java_callback (any) – A callback handler that allows to make callbacks to Java. Was introduced in 5.3 and can be omitted in derived classes.
- Returns:
The deserialized PortObjectSpec
- Return type:
PortObjectSpec
- class knime.extension.ImagePortObjectSpec(format: str | Enum)
Port object spec for image port objects.
ImagePortObjectSpec objects require the format specified via knext.ImageFormat.PNG or knext.ImageFormat.SVG.
- classmethod deserialize(data)
Deserialize the port object.
- Parameters:
data (Dict) – The data dict created by serialize
java_callback (any) – A callback handler that allows to make callbacks to Java. Was introduced in 5.3 and can be omitted in derived classes.
- Returns:
The deserialized PortObjectSpec
- Return type:
PortObjectSpec
- class knime.extension.ImageFormat(value)
The image formats available for image ports.
- PNG = 'png'
The PNG format.
- SVG = 'svg'
The SVG format.
Custom Port Object Types
- class knime.extension.PortObject(spec: PortObjectSpec)
Base class for custom port objects. They must have a corresponding PortObjectSpec and support serialization from and to bytes.
Examples
>>> class MyPortObjectSpec(knext.PortObjectSpec): ... def __init__(self, my_info: str) -> None: ... self._my_info = my_info ... ... def serialize(self) -> dict: ... return { ... "my_info": self._my_info, ... } ... ... @classmethod ... def deserialize(cls, data: dict) -> "MyPortObjectSpec": ... return cls(data["my_info"]) ... ... @property ... def my_info(self) -> str: ... return self._my_info ... ... class MyPortObject(knext.PortObject): ... def __init__(self, spec: MyPortObjectSpec, my_data: str) -> None: ... super().__init__(spec) ... self._my_data = my_data ... ... def serialize(self) -> bytes: ... return self._my_data.encode() ... ... @property ... def spec(self) -> MyPortObjectSpec: ... return super().spec ... ... @classmethod ... def deserialize(cls, spec: MyPortObjectSpec, storage: bytes) -> "MyPortObject": ... return cls(spec, storage.decode()) ... ... @property ... def my_data(self) -> str: ... return self._my_data ... ... ... my_port_type = knext.port_type("My Port Type", MyPortObject, MyPortObjectSpec) ... ... @knext.node( ... name="My PortObject Creator", ... node_type=knext.NodeType.SOURCE, ... icon_path="icon.png", ... category=node_category, ... ) ... @knext.output_port("MyPortType output", "MyPortType output", my_port_type) ... class NodeWithMyOutputPort(knext.PythonNode): ... value = knext.StringParameter( ... "port object value", ... "Value that will be put in the port object", ... "Foo", ... ) ... ... def configure(self, config_context: knext.ConfigurationContext): ... return MyPortObjectSpec(f"port object will contain value {self.value}") ... ... def execute(self, exec_context: knext.ExecutionContext): ... return MyPortObject(MyPortObjectSpec(f"port object will contain value {self.value}"), self.value) ... ... ... @knext.node( ... name="My PortObject Applier", ... node_type=knext.NodeType.MANIPULATOR, ... icon_path="icon.png", ... category=node_category, ... ) ... @knext.input_port("MyPortType input", "MyPortType input", my_port_type) ... @knext.input_table("Input table", "Table to apply some action to.") ... @knext.output_table("Output table", "Table with the applied action.") ... class NodeWithTestInputPort(knext.PythonNode): ... ''' ... This node adds a column containing some info from the port object ... ''' ... def configure( ... self, ... config_context: knext.ConfigurationContext, ... spec: MyPortObjectSpec, ... schema: knext.Schema, ... ): ... return schema.append(knext.Column(knext.string(), "NewCol")) ... ... def execute( ... self, ... exec_context: knext.ExecutionContext, ... port_object: MyPortObject, ... table: knext.Table, ... ): ... df = table.to_pandas() ... df["NewCol"] = [port_object.my_data] * len(df) ... ... return knext.Table.from_pandas(df)
- abstract classmethod deserialize(spec: PortObjectSpec, storage: bytes) PortObject
Creates the port object from its spec and storage.
- abstract serialize() bytes
Serialize the object to bytes.
- property spec: PortObjectSpec
Provides access to the spec of the PortObject.
- class knime.extension.ConnectionPortObject(spec: PortObjectSpec)
Connection port objects are a special type of port objects which support dealing with non-serializable objects such as database connections or web sessions.
Connection port objects are passed downstream by ensuring that the same Python process is used to execute subsequent nodes. ConnectionPortObjects must provide the data in the to_connection_data and create new instances from the same data in from_connection_data. A reference to the data Python object is maintained and handed to downstream nodes. So the data does not need to be serializable/picklable.
- abstract classmethod from_connection_data(spec: PortObjectSpec, data: Any) ConnectionPortObject
Construct a ConnectionPortObject from spec and data. The data is the data that has been returned by the to_connection_data method of the ConnectionPortObject by the upstream node.
The data should not be tempered with, as it is a Python object that is handed to all nodes using this ConnectionPortObject.
- property spec: PortObjectSpec
Provides access to the spec of the PortObject.
- abstract to_connection_data() Any
Provide the data that makes up this ConnectionPortObject such that it can be used by downstream nodes in the from_connection_data method.
Environment Variables
This module provides access to the different environment variables that are available in KNIME.
- class knime.extension.ProxySettings(protocol_name: str | None = None, host_name: str | None = None, port_number: str | None = None, exclude_hosts: str | None = None, user_name: str | None = None, password: str | None = None)
Proxy settings for a KNIME node
The proxy settings are used to set the proxy environment variables for the KNIME Python integration.
- protocol_name
The lowercase protocol name.
- Type:
str or None
- host_name
The host name.
- Type:
str or None
- port_number
The port number.
- Type:
str or None
- exclude_hosts
List of hosts to exclude.
- Type:
str or None
- user_name
The username.
- Type:
str or None
- password
The password.
- Type:
str or None
- has_credentials
True if both username and password are provided, False otherwise.
- Type:
bool
- Parameters:
protocol_name (str, optional) – The name of the protocol. Default is None.
host_name (str, optional) – The name of the host. Default is None.
port_number (str, optional) – The port number. Default is None.
exclude_hosts (str, optional) – List of hosts to exclude. Default is None.
user_name (str, optional) – The username. Default is None.
password (str, optional) – The password. Default is None.
- create_proxy_environment_key_value_pair()
Create the proxy environment variable strings.
- set_as_environment_variable()
Set the proxy settings as environment variables.
- from_string(proxy_string, exclude_hosts=None)
Parse the proxy settings from a string.
- supported_proxy_protocols()
Return the supported proxy protocols for KNIME proxy settings in Python.
- create_proxy_environment_key_value_pair() Tuple[str, str]
Create the proxy environment variable strings.
- Returns:
Tuple[str, str]
- Return type:
The proxy environment variable name and value
- classmethod from_dict(proxy_dict)
Create a ProxySettings object from a dictionary.
- Parameters:
proxy_dict (dict) – The dictionary containing the proxy settings.
- Returns:
The proxy settings object.
- Return type:
ProxySettings
- classmethod from_string(proxy_string, exclude_hosts: str | None = None)
Parse the proxy settings from a string
- Parameters:
proxy_string (str) – The string is in the format of: protocol://user:password@host:port or protocol://host:port e.g. http://user:password@localhost:8080 or http://localhost:8080
exclude_hosts (str) – The hosts that should be excluded from the proxy, e.g. localhost, separated by a comma
- Returns:
The proxy settings object
- Return type:
ProxySettings
- set_as_environment_variable()
Set the proxy settings as environment variables.
- static supported_proxy_protocols() str
Return the supported proxy protocols for KNIME proxy settings in Python.
- Returns:
A string containing the list of supported proxy protocols.
- Return type:
str
- class knime.extension.get_proxy_settings(protocol_name: str | None = None)
Get the proxy settings from the environment variables.
Get the proxy settings as configured either in KNIME’s preferences or via environment variables. Even if the proxy settings were configured in KNIME’s preferences only, they are already set as environment variables for this Python process, so they are in effect for everything you do.
- Parameters:
protocol_name (str) – The protocol name, e.g. ‘http’ or ‘https’. To see all supported protocols, call ProxySettings.supported_proxy_protocols(). If not provided, the function will return the first proxy settings it finds.
- Returns:
The proxy settings object.
- Return type:
ProxySettings
Testing (Experimental)
Note
Experimental Feature: The testing functionality provided in this module
is currently experimental. It does not support all data types and port types, and only works
with Table._to_pandas() and not Table.to_pyarrow(). Use with caution and expect
potential API modifications or behavioral changes.
Experimental unit testing support for Python based KNIME nodes.
This module provides mock implementations for some KNIME internals as
well as an TestingExecutionContext and a TestingConfigureContext which you
can instantiate, e.g. set some flow variables, or overwrite the behavior
of some methods if needed.
With that, you can build unit tests using Python’s unittest framework that instanciate
a node and run configure or execute, using Pandas DataFrames as inputs and outputs
(Arrow is not implemented for testing).
If you need any extension types like e.g. Chemistry or Geospatial types, we provide
a method register_extension(plugin_xml) that you can point to the source of the
plugin that registers these extensions. It will load load the Python type definition of
this plugin so that it will be available in the test as usual
(e.g. import knime.types.chemistry).
Examples
If you want to test that your node produces the output schema you expect,
you can run configure of your node:
>>> import knime.extension as knext
... import knime.extension.testing as ktest
...
... # set up test input schema
... schema = knext.Schema.from_columns(
... [
... knext.Column(knext.int64(), "Integers"),
... knext.Column(knext.double(), "Doubles"),
... ]
... )
...
... # assume TestNode is your Python node with one input and one output table
... node = TestNode()
... node.column = "Test" # configure parameter "column" to the value "Test"
...
... # create testing configuration context
... config_context = ktest.TestingConfigurationContext()
...
... # configure node, passing the testing configuration context
... output_schema = node.configure(config_context, input_schema)
...
... # check that the output schema matches your expectations
...
... expected_schema = knext.Schema.from_columns(
... [
... knext.Column(knext.int64(), "Integers"),
... knext.Column(knext.double(), "Doubles"),
... knext.Column(knext.int64(), "Integers_copy"),
... ]
... )
...
... self.assertEqual(expected_schema, output_schema)
To test execution of a node that uses to_pandas():
>>> import knime.extension as knext
... import knime.extension.testing as ktest
...
... # set up test input
... input_df = pd.DataFrame({"Test": [1, 2, 3, 4]})
... input = knext.Table.from_pandas(input_df)
...
... # assume TestNode is your Python node with one input and one output table
... node = TestNode()
... node.column = "Test" # configure parameter "column" to the value "Test"
...
... # create testing execution context
... exec_context = ktest.TestingExecutionContext()
...
... # execute node
... output = node.execute(exec_context, input)
... output_df = output.to_pandas()
...
... # TODO: check that the output DataFrame matches your expectations
In case you are working with a batched table, it makes sense to test with multiple batches:
>>> # assume BatchTestNode is your Python node with one input and one output table
... node = BatchTestNode()
... node.column = "Test"
...
... input = knext.BatchOutputTable.create()
... input.append(pd.DataFrame({"Test": [1, 2]}))
... input.append(pd.DataFrame({"Test": [3, 4]}))
...
... exec_context = ktest.TestingExecutionContext()
... output = node.execute(exec_context, input)
...
... output_df = output.to_pandas()
... expected_df = pd.DataFrame({"Test": [1, 2, 3, 4]})
... expected_df["Test_copy"] = expected_df["Test"]
... for column in expected_df.columns:
... self.assertEqual(
... expected_df[column].to_list(),
... output_df[column].to_list(),
... f"Values not equal in {column}",
... )
- class knime.extension.testing.TestingConfigurationContext
This is a mock implementation of a configuration context for testing. In unit tests, create an instance of the
TestConfigurationContextand pass it to your node when you testconfigureExamples
>>> import knime.extension as knext ... import knime.extension.testing as ktest ... ... # set up test input schema ... schema = knext.Schema.from_columns( ... [ ... knext.Column(knext.int64(), "Integers"), ... knext.Column(knext.double(), "Doubles"), ... ] ... ) ... ... # assume TestNode is your Python node with one input and one output table ... node = TestNode() ... node.column = "Test" # configure parameter "column" to the value "Test" ... ... # create testing configuration context ... config_context = ktest.TestingConfigurationContext() ... ... # configure node, passing the testing configuration context ... output_schema = node.configure(config_context, input_schema) ... ... # TODO: check that the output schema matches your expectations
- class knime.extension.testing.TestingExecutionContext
This is a mock implementation of an execution context for testing. In unit tests, create an instance of the
TestExecutionContextand pass it to your node when testingexecute.Examples
>>> import knime.extension as knext ... import knime.extension.testing as ktest ... ... # set up test input ... input_df = pd.DataFrame({"Test": [1, 2, 3, 4]}) ... input = knext.Table.from_pandas(input_df) ... ... # assume TestNode is your Python node with one input and one output table ... node = TestNode() ... node.column = "Test" # configure parameter "column" to the value "Test" ... ... # create testing execution context ... exec_context = ktest.TestingExecutionContext() ... ... # execute node, passing the testing execution context ... output = node.execute(exec_context, input) ... output_df = output.to_pandas() ... ... # TODO: check that the output DataFrame matches your expectations
- knime.extension.testing.register_extension(plugin_xml: str, value_factory_to_type_name_map: dict | None = None)
Registers the Python extension types found in the given plugin.xml file and puts all modules with extension types on the Pythonpath.
- Parameters:
plugin_xml (str) – Provide the path to a plugin.xml that registers Python types at the extension point.
value_factory_to_type_name_map (dict = None) – An optional mapping from value factory to readable type names e.g.
org.knime.chem.types.SmilesAdapterCellValueFactory->Smiles