Block
API Reference
The Block class represents a configurable Block
object with inputs, outputs, options, and state management capabilities.
Class barfi.flow.Block
Parameters
- name (str, default=“Block”): The name of the block. If the default is used, a unique name is generated during initialization.
Example
from barfi.flow import Block
my_block = Block("my_block")
Methods
add_input(name: str = None, interface_type: Type[IT] = Any) -> None
Adds an input interface to the block.
Parameters
- name (str, optional): Name of the input interface. If None, a default name is generated. Recommended to provide a name.
- interface_type (Type[IT], optional): The type of the interface. Defaults to Any. Takes in a Python type or a
dataclass
.
Raises
- ValueError: If the name already exists as an interface.
- TypeError: If the interface_type is not valid.
Example
my_block.add_input(name='Input Name', interface_type=int)
add_output(name: str = None, interface_type: Type[IT] = Any) -> None
Adds an output interface to the block.
Parameters
-
name (str, optional): Name of the output interface. If None, a default name is generated.
-
interface_type (Type[IT], optional): The type of the interface. Defaults to Any.
Raises
- ValueError: If the name already exists as an interface.
- TypeError: If the interface_type is not valid.
Example
block.add_output(name='Output Name', interface_type=str)
get_interface(name: str) -> Any
Gets the value of a specified interface.
Parameters
- name (str): The name of the interface.
Returns
The value of the specified interface.
Raises
- ValueError: If no interface with the given name is found.
Example
value = block.get_interface(name='Input Name')
set_interface(name: str, value: Any) -> None
Sets the value of a specified interface.
Parameters
- name (str): The name of the interface.
- value (Any): The value to set for the interface.
Raises
- ValueError: If no interface with the given name is found.
Example
block.set_interface(name='Input Name', value=42)
set_state(key: str, value: Any) -> None
Sets a value in the block’s state.
Parameters
- key (str): The key for the state value.
- value (Any): The value to set.
Raises
- ValueError: If the key is reserved.
Example
block.set_state(key='custom_state', value=123)
get_state(key: str) -> Any
Gets a value from the block’s state.
Parameters
- key (str): The key for the state value.
Returns
The value associated with the given key.
Raises
- ValueError: If the key does not exist in the state.
Example
value = block.get_state(key='custom_state')
add_option(name: str, type: str, **kwargs) -> None
Adds an interactive option to the block. All possible options are detailed here: BlockOption.
Parameters
- name (str): The name of the option.
- type (str): The type of the option. Must be one of: checkbox, input, integer, number, select, slider, display.
- kwargs: Additional properties depending on the type.
Raises
- ValueError: If an option with the given name already exists.
- AssertionError: If arguments are not valid.
Example
block.add_option(name='My Option', type='checkbox', value=True)
set_option(name: str, **kwargs) -> None
Sets the value of an existing option.
Parameters
- name (str): The name of the option.
- kwargs: Additional properties to set.
Raises
- ValueError: If the option name does not exist or an invalid property is set.
Example
block.set_option(name='My Option', value=False)
get_option(name: str) -> BlockOptionValue
Gets the value of an existing option.
Parameters
- name (str): The name of the option.
Returns
The value of the specified option.
Raises
- ValueError: If the option name does not exist.
Example
value = block.get_option(name='My Option')