Skip to content

Ops

API for implementing LynxKite operations.

LongStr module-attribute ¤

LongStr = Annotated[str, {'format': 'textarea'}]

LongStr is a string type for parameters that will be displayed as a multiline text area in the UI.

Op ¤

Bases: BaseConfig

convert_params ¤

convert_params(params: dict[str, Any])

Returns the parameters converted to the expected type.

get_input ¤

get_input(name: str)

Returns the input with the given name.

get_output ¤

get_output(name: str)

Returns the output with the given name.

Parameter ¤

Bases: BaseConfig

Defines a parameter for an operation.

ParameterGroup ¤

Bases: BaseConfig

Defines a group of parameters for an operation.

Position ¤

Bases: str, Enum

Defines the position of an input or output in the UI.

from_dir staticmethod ¤

from_dir(dir: str) -> tuple[Position, Position]

Returns the input and output positions based on the direction.

Result dataclass ¤

Represents the result of an operation.

The output attribute is what will be used as input for other operations. The display attribute is used to send data to display on the UI. The value has to be JSON-serializable. input_metadata is a list of JSON objects describing each input.

get_optional_type ¤

get_optional_type(type)

For a type like int | None, returns int. Returns None otherwise.

input_position ¤

input_position(**positions)

Decorator for specifying unusual positions for the inputs.

Example usage:

@input_position(a="bottom", b="bottom")
@op("test", "maybe add")
def maybe_add(a: list[int], b: list[int] | None = None):
    return [a + b for a, b in zip(a, b)] if b else a

load_user_scripts ¤

load_user_scripts(workspace: str)

Reloads the *.py in the workspace's directory and higher-level directories.

make_async ¤

make_async(func)

Decorator for slow, blocking operations. Turns them into separate threads.

matplotlib_to_image ¤

matplotlib_to_image(func)

Decorator for converting a matplotlib figure to an image.

op ¤

op(env: str, *names: str, view: str = 'basic', outputs: list[str] | None = None, params: list[Parameter] | None = None, slow: bool = False, color: str | None = None, cache: bool | None = None, dir: str = 'left-to-right')

Decorator for defining an operation.

PARAMETER DESCRIPTION
env

The environment (workspace type) to which the operation belongs.

TYPE: str

names

The list of categories this operation belongs to, followed by the name of the operation.

TYPE: str DEFAULT: ()

view

How the operation will be displayed in the UI. One of "basic", "visualization", "table_view", "graph_creation_view", "image", "molecule", "matplotlib".

TYPE: str DEFAULT: 'basic'

outputs

A list of output names. If not provided, defaults to ["output"] for "basic" view.

TYPE: list[str] | None DEFAULT: None

params

Normally the parameters are taken from the function signature. Use "params" to override this.

TYPE: list[Parameter] | None DEFAULT: None

slow

If True, the operation results will be cached. If the function is not async, it will be run in a separate thread.

TYPE: bool DEFAULT: False

color

The color of the operation in the UI. Defaults to "orange".

TYPE: str | None DEFAULT: None

cache

Set to False to disable caching for a slow operation. You may need this for slow operations with parameters/outputs that can't be serialized.

TYPE: bool | None DEFAULT: None

dir

Sets the default input and output positions. The default is "left-to-right", meaning inputs are on the left and outputs are on the right. Other options are "right-to-left", "top-to-bottom", and "bottom-to-top".

TYPE: str DEFAULT: 'left-to-right'

op_registration ¤

op_registration(env: str, *categories: str, **kwargs)

Returns a decorator that can be used for registering functions as operations.

output_position ¤

output_position(**positions)

Decorator for specifying unusual positions for the outputs.

Example usage:

@output_position(output="top")
@op("test", "maybe add")
def maybe_add(a: list[int], b: list[int] | None = None):
    return [a + b for a, b in zip(a, b)] if b else a

parse_doc cached ¤

parse_doc(func)

Griffe is an optional dependency. When available, we return the parsed docstring.

passive_op_registration ¤

passive_op_registration(env: str, *categories: str, **kwargs)

Returns a function that can be used to register operations without associated code.

register_executor ¤

register_executor(env: str)

Decorator for registering an executor.

The executor is a function that takes a workspace and executes the operations in it. When it starts executing an operation, it should call node.publish_started() to indicate the status on the UI. When the execution is finished, it should call node.publish_result(). This will update the UI with the result of the operation.

register_passive_op ¤

register_passive_op(env: str, *names: str, inputs=[], outputs=['output'], params=[], dir='left-to-right', **kwargs)

A passive operation has no associated code.