Skip to main content

Creating a new Base Component

What is a Base Component?

Base Components are the default components. All components in the component_library directory will be automatically listed as Base Components and will be available to all users in your deployment.

Creating a new Base Component

  1. Create a new package Add your component package to the lunarverse/components directory

  2. Extend BaseComponent Your new component must extend the BaseComponent class from lunarverse.components.base and you have to set these following mandatory arguments:

ArgumentTypeDescription
component_namestrThe name of your component
component_descriptionstrThe decription of your component. It is important to clearly specify what your component does because this field will be used for auto workflow generation
input_typesa dict of DataTypesThe inputs your component receive. The keys of the dictionary will be interpreted as the input labels and you can check the available data types here
output_typeDataTypeThe output of the component

You can also specify custom arguments in addition to the mandatory ones. These will be interpreted as component configurations.

Example:

class MyCustomComponent(
BaseComponent,
component_name="Component Name",
component_description="Describe what the component does",
input_types={"Custom JSON": DataType.JSON},
output_types=DataType.JSON,
custom_argument="Deault custom_argument value",
...
):
...

The run method

When a workflow with your component runs, this is the method called. So you need to implement it.

Parameters

NameTypeDescription
inputsUnion[List[ComponentInput], ComponentInput]Either a list of component inputs or a component input (if there's only one)

Example:

def run(self, inputs: ComponentInput, **kwargs: Any):
data = dict()
images = [self.__class__.plot_bar_chart(inputs.value)]
data.update(inputs.value)
return {"data": data, "images": images}

Creating a new component video

Dependencies

It is possible to add python packages dependencies to your component, and in order to do that, you just have to add a requirements.txt file in the root of your component's package, following the classical PIP requirements. All dependencies will be installed and be available at workflow running time.

File Connector

The FileConnector class provides an interface for file operations in a controlled environment. It allows you to create, read, list, and delete files relatively to the workflow that is executing the component. You can access a FileConnector instance through the _file_connector property of the component.

Methods

Create file

create_file(self, file_name: str, content: Union[AnyStr, Generator[AnyStr, None, None]]) Creates a new file with the specified content. Raises a FileExistsError if the file already exists.

Parameters
NameTypeDescription
file_namestrThe name of the file to be created.
contentUnion[AnyStr, Generator[AnyStr, None, None]]The content to be written to the file.

Get file by name

get_file_by_name(self, file_name: str, chunk_size: int = -1) Reads the content of a file in chunks.

Parameters
NameTypeDescription
file_namestrThe name of the file to read.
chunk_sizeintThe size of each chunk to read. Defaults to -1, which reads the entire file at once.
Returns

Generator[str, None, None]: A generator yielding file content in chunks.

List all files

list_all_files(self) Lists all files with extensions (.txt, .png, .pdf, etc) in the base directory and its subdirectories.

Returns

list[File]: A list of File objects representing the files with extensions in the directory.

Delete file

delete_file(self, file_name: str) Deletes a specified file

Parameters
NameTypeDescription
file_namestrThe name of the file to be deleted
warning

Be careful, deleted files cannot be recovered.

Get absolute path

get_absolute_path(self, relative_path: str) Returns the absolute path of the file matching the relative path pattern

Parameters
NameTypeDescription
relative_pathstrThe relative path pattern. It should follow the rules used by the Unix shell.
Returns

str: The absolute path of the matching file, or None if no match is found.