Python Script API

This section lists the API of the module knime.scripting.io that functions as the main contact point between KNIME and Python in the KNIME Python Script node. Please refer to the KNIME Python Integration Guide for more details on how to set up and use the node.

Note

Before KNIME AP 4.7, the module used to interact with KNIME from Python was called knime_io and provided a slightly different API. Since KNIME AP 4.7 the new Python Script node is no longer in Labs status and uses the knime.scripting.io module for interaction between KNIME and Python. It uses the same Table and Batch classes as can be used in KNIME Python Extensions. The previous API is described in Deprecated Python Script API

Inputs and outputs

These properties can be used to retrieve data from or pass data back to KNIME Analytics Platform. The length of the input and output lists depends on the number of input and output ports of the node.

Example: If you have a Python Script node configured with two input tables and one input object, you can access the two tables via knime.scripting.io.input_tables[0] and knime.scripting.io.input_tables[1], and the input object via knime.scripting.io.input_objects[0].

Input and output variables used to communicate with KNIME from within KNIME’s Python Scripting nodes

knime.scripting.io.flow_variables: Dict[str, Any] = {}

A dictionary of flow variables provided by the KNIME workflow. New flow variables can be added to the output of the node by adding them to the dictionary. Supported flow variable types are numbers, strings, booleans and lists thereof.

knime.scripting.io.input_objects: List = <knime.scripting._io_containers._FixedSizeListView object>

A list of input objects of this script node using zero-based indices. This list has a fixed size, which is determined by the number of input object ports configured for this node. Input objects are Python objects that are passed in from another Python script node’s``output_object`` port. This can, for instance, be used to pass trained models between Python nodes. If no input is given, the list exists but is empty.

knime.scripting.io.input_tables: List[Table] = <knime.scripting._io_containers._FixedSizeListView object>

The input tables of this script node. This list has a fixed size, which is determined by the number of input table ports configured for this node. Tables are available in the same order as the port connectors are displayed alongside the node (from top to bottom), using zero-based indexing. If no input is given, the list exists but is empty.

knime.scripting.io.output_images: List = <knime.scripting._io_containers._FixedSizeListView object>

The output images of this script node. This list has a fixed size, which is determined by the number of output images configured for this node. The value passed to the output port should be a bytes-like object encoding an SVG or PNG image.

Example:

import knime.scripting.io as knio

data = knio.input_tables[0].to_pandas()
buffer = io.BytesIO()

pyplot.figure()
pyplot.plot('x', 'y', data=data)
pyplot.savefig(buffer, format='svg')

knio.output_images[0] = buffer.getvalue()
knime.scripting.io.output_objects: List = <knime.scripting._io_containers._FixedSizeListView object>

The output objects of this script node. This list has a fixed size, which is determined by the number of output object ports configured for this node. Each output object can be an arbitrary Python object as long as it can be pickled. Use this to, for example, pass a trained model to another Python script node.

Example:

model = torchvision.models.resnet18()
...
# train/finetune model
...
knime.scripting.io.output_objects[0] = model
knime.scripting.io.output_tables: List[Table | BatchOutputTable] = <knime.scripting._io_containers._FixedSizeListView object>

The output tables of this script node. This list has a fixed size, which is determined by the number of output table ports configured for this node. You should assign a Table or BatchOutputTable to each output port of this node.

Example:

import knime.scripting.io as knio
knio.output_tables[0] = knio.Table.from_pandas(my_pandas_df)
knime.scripting.io.output_view: NodeView | None = None

The output view of the script node. This variable must be populated with a NodeView when using the Python View node. Views can be created by calling the view(obj) method with a viewable object. See the documentation of view(obj) to understand how views are created from different kinds of objects.

Example:

import knime.scripting.io as knio
import plotly.express as px

fig = px.scatter(x=data_x, y=data_y)
knio.output_view = knio.view(fig)

Classes

class knime.scripting.io.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() or from_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.

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 – A column index, a column name, a slice object, a list of column indices, or a list of column names.

  • row_slice – Optional: A slice object describing which rows to use.

Returns:

A _TabularView representing a slice of the original Table

Example:

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
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.

Example:

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))
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.

Example:

Table.from_pandas(my_pandas_df, sentinel="min")
Parameters:
  • data – A pandas.DataFrame

  • sentinel

    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 column

    • a special integer value that should be interpreted as missing value

  • row_ids

    Defines what RowID should be used. Must be one of the following values:

    • "keep": Keep the DataFrame.index as the RowID. Convert the index to strings if necessary.

    • "generate": Generate new RowIDs of the format f"Row{i}" where i is the position of the row (from 0 to length-1).

    • "auto": If the DataFrame.index is of type int or unsigned int, use f"Row{n}" where n is the index of the row. Else, use “keep”.

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.

Example:

Table.from_pyarrow(my_pyarrow_table, sentinel="min")
Parameters:
  • data – A pyarrow.Table

  • sentinel

    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 column

    • a special integer value that should be interpreted as missing value

  • row_ids

    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 format f"Row{i}" where i is the position of the row (from 0 to length-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} where n is the value of the integer column.

      • Generate new RowIDs ("generate") if the first column has another type or name.

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 – 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 occurence is removed from the column_names.

Returns:

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 a 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

Replace missing values in integral columns by the given value, one of:

  • "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 column

  • An 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

Replace missing values in integral columns by the given value, one of:

  • "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 column

  • An integer value that should be inserted for each missing value

class knime.scripting.io.BatchOutputTable

An output table generated by combining smaller tables (also called batches).

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 less rows than the other batches.

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.

Note

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

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 f"Row{i}"

static from_batches(generator, row_ids: str = 'generate')

Create output table where each batch is provided by a generator

Parameters:

row_ids – See BatchOutputTable.create.

abstract property num_batches: int

The number of batches written to this output table

Views

knime.scripting.io.view(obj) NodeView

Create an NodeView for the given object.

This method tries to find out the best option to display the given object. First, the method checks if a special view implementation (listed below) exists for the given object. Next, IPython _repr_html_, _repr_svg_, _repr_png_, or _repr_jpeg_ are used.

Special view implementations:

  • HTML: The obj must be of type str and start with “<!DOCTYPE html>”. The document must be self-contained and must not reference external resources. Links to external resources will be opened in an external browser.

  • SVG: The obj must be of type str and contain a valid SVG

  • PNG: The obj must be of type bytes and contain a PNG image file

  • JPEG: The obj must be of type bytes and contain a JPEG image file

  • Matplotlib: The obj must be a matplotlib.figure.Figure

  • Plotly: The obj must be a plotly.graph_objects.Figure

Parameters:

obj – The object which should be displayed

Raises:

ValueError – If no view could be created for the given object

knime.scripting.io.view_matplotlib(fig=None, format='png') NodeView

Create a view showing the given matplotlib figure.

The figure is displayed by exporting it as an SVG. If no figure is given the current active figure is displayed. Note that the figure is closed and should not be used after calling this method.

Parameters:
  • fig – A matplotlib.figure.Figure which should be displayed.

  • format – Format of the view inside the HTML document. Either “png” or “svg”.

Raises:
  • ImportError – If matplotlib is not available.

  • TypeError – If the figure is not a matplotlib figure.

knime.scripting.io.view_seaborn() NodeView

Create a view showing the current active seaborn figure.

This fuction just calls view_matplotlib() because seaborn plots are just matplotlib figures under the hood.

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.

Example:

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)
Parameters:

fig – A plotly.graph_objects.Figure object which should be displayed.

Raises:
  • ImportError – If plotly is not available.

  • TypeError – If the figure is not a plotly figure.

knime.scripting.io.view_html(html: str, svg_or_png: str | bytes | None = None, render_fn: Callable[[], str | bytes] | None = None) 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 – A string containing the HTML document.

  • svg_or_png – A rendered representation of the HTML page. Either a string containing an SVG or a bytes object containing an PNG image

  • render_fn – A callable that returns an SVG or PNG representation of the page

knime.scripting.io.view_svg(svg: str) NodeView

Create a NodeView that displays the given SVG.

Parameters:

svg – A string containing the SVG.

knime.scripting.io.view_png(png: bytes) NodeView

Create a NodeView that displays the given PNG image.

Parameters:

png – The bytes of the PNG image

knime.scripting.io.view_jpeg(jpeg: bytes) NodeView

Create a NodeView that displays the given JPEG image.

Parameters:

jpeg – The bytes of the JPEG image

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 – 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)

A view of a KNIME node that can be displayed for the user.

Do not create a NodeView directly but use the utility functions view, view_html, view_svg, view_png, and view_jpeg.

Utility functions

knime.scripting.io.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.

knime.scripting.io.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.