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
-
Create a new package Add your component package to the
lunarverse/components
directory -
Extend BaseComponent Your new component must extend the
BaseComponent
class fromlunarverse.components.base
and you have to set these following mandatory arguments:
Argument | Type | Description |
---|---|---|
component_name | str | The name of your component |
component_description | str | The 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_types | a dict of DataType s | The 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_type | DataType | The 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
Name | Type | Description |
---|---|---|
inputs | Union[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
Name | Type | Description |
---|---|---|
file_name | str | The name of the file to be created. |
content | Union[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
Name | Type | Description |
---|---|---|
file_name | str | The name of the file to read. |
chunk_size | int | The 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
Name | Type | Description |
---|---|---|
file_name | str | The name of the file to be deleted |
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
Name | Type | Description |
---|---|---|
relative_path | str | The 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.