feat: add new node Input type for data in table format (#2635)

* feat: add Table component and related functionality

This commit adds the Table component and related functionality to the codebase. The Table component is used to display tabular data and includes features such as pagination, row deletion, row duplication, and adding new rows. The TableOptions component is also added to provide options for resetting the grid and adding new rows. Additionally, the necessary types and interfaces are updated to support the Table component. This feature enhances the user experience by allowing them to interact with tabular data in a more intuitive way.

* feat: add Edit Data trigger to TableNodeComponent

* [autofix.ci] apply automated fixes

* feat: add TableSchema class for defining table structure

* feat: add TableMixin class for table-related functionality

* feat: add TableInput class for table-related functionality

* feat: add TableInput to io module

* feat: update Column model in table schema

This commit updates the `Column` model in the table schema to include the `display_name` and `name` fields instead of `header` and `field`. It also adds validation for the `formatter` field to accept either a `FormatterType` enum value or a string. This change improves the clarity and flexibility of the table schema.

* feat: add displayEmptyAlert prop to TableComponent

This commit adds the `displayEmptyAlert` prop to the `TableComponent` in order to control whether an alert is displayed when the table has no data. By default, the alert will be shown, but it can be disabled by setting `displayEmptyAlert` to `false`. This feature enhances the flexibility of the table component by allowing users to customize the behavior when there are no rows in the table.

* This commit improves the TableAutoCellRender component by adding support for a custom formatter. The formatter can be specified as a prop and allows for rendering the cell value in different formats, such as JSON. This enhancement enhances the flexibility and customization options of the TableAutoCellRender component.

* feat: add FormatColumns function to utils.ts

This commit adds the `FormatColumns` function to `utils.ts` file. The function takes an array of `ColumnField` objects and returns an array of `ColDef` objects. It maps each `ColumnField` to a `ColDef` with properties like `headerName`, `field`, `sortable`, and `filter`. If a `ColumnField` has a `formatter` property, it sets the `cellDataType` or `cellRendererParams` accordingly. This function enhances the flexibility and customization options for formatting columns in the table.

* feat: enhance TableNodeComponent with FormatColumns function

This commit enhances the TableNodeComponent by utilizing the FormatColumns function from utils.ts. The FormatColumns function takes an array of ColumnField objects and returns an array of ColDef objects, allowing for flexible and customizable column formatting in the table. By integrating this function, the TableNodeComponent now has improved column handling capabilities.

* chore: Update TableNodeComponent and TableComponent

This commit updates the TableNodeComponent and TableComponent to improve column handling and customization options. The TableNodeComponent now utilizes the FormatColumns function from utils.ts, allowing for flexible and customizable column formatting in the table. The TableComponent now has a new prop, displayEmptyAlert, which controls whether an alert is displayed when the table has no data. These enhancements enhance the flexibility and customization options of the table components.

* [autofix.ci] apply automated fixes

* feat: Update TableNodeComponent and TableComponent

This commit updates the TableNodeComponent and TableComponent to improve column handling and customization options. It utilizes the FormatColumns function from utils.ts for flexible and customizable column formatting in the table. The TableComponent now has a new prop, displayEmptyAlert, to control the display of an alert when the table has no data. These enhancements enhance the flexibility and customization options of the table components.

* feat: initialize table field values as DataFrame

* feat: Enhance TableNodeComponent with duplicateRow function

This commit enhances the TableNodeComponent by adding the duplicateRow function. This function allows users to duplicate selected rows in the table. When called, it clones the selected nodes and adds the duplicated rows to the table. This feature enhances the flexibility and customization options of the TableNodeComponent.

* [autofix.ci] apply automated fixes

* feat: Remove "text" from basic_types in FormatColumns function

This commit removes the "text" value from the basic_types set in the FormatColumns function in utils.ts. The basic_types set is used to determine the column type for formatting in the table. By removing "text", we ensure that only "date" and "number" types are considered as basic types. This change improves the accuracy and consistency of column formatting in the table.

* fix: alingment bug on AgGrid cell

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes

* Styled the Open Table button on TableNodeComponent

* Fixed type of ref on tableComponent

* Creaed a TableModal component, that receives the props that are passed to the Table, as well as a title, and creates a modal

* Used the TableModal on the TableNodeComponent

* Fixed looks of TableModal

* Added description set on tableModal

* Add description field to TableNodeComponent

* Fixed text of description if info is not provided

* Added TableComponent in tableNodeCellRenderer

* Added styling based on editNode

* Added Auto Size to table modal

* refactor: update TableOptions component styling and behavior

- Update TableOptions component to dynamically apply text color based on selection
- Remove unnecessary console.log statement
- Improve hover behavior for the Trash2 icon

* chore: Remove unnecessary imports and initialize empty columns array in TableNodeComponent

* feat: Add default values for sortable and filterable in Column model

The code changes in `table.py` modify the `Column` model in the `langflow.schema` module. The `sortable` and `filterable` attributes of the `Column` model now have default values of `True`. This change ensures that new instances of the `Column` model will have these attributes set to `True` by default.

Based on the recent user commits and repository commits, the commit message follows the established convention of using a prefix to indicate the type of change (`feat` for a new feature) and provides a clear and concise description of the changes made.

* feat(utils.ts): add check for empty columns array in FormatColumns function to prevent errors

* feat: Add validation for TableInput value in inputs.py

The code changes in `inputs.py` add a validation function for the `value` attribute of the `TableInput` class. The function checks if the value is a list of dictionaries and raises a `ValueError` if it is not. This ensures that the `TableInput` instances have a valid value that is a list of dictionaries.

Based on the recent user commits and repository commits, the commit message follows the established convention of using a prefix to indicate the type of change (`feat` for a new feature) and provides a clear and concise description of the changes made.

* [autofix.ci] apply automated fixes

* feat: extend editable field to json field

* [autofix.ci] apply automated fixes

* feat: Add validation for TableInput value in inputs.py

* feat(validate.py): add exception handling to catch and re-raise ValidationError with a more informative error message

* chore: Refactor error message in build_custom_component_template function

* fix(validate.py): improve error message formatting in create_class function

Refactor the error message formatting in the `create_class` function in `validate.py` to improve readability and clarity. Instead of using a list comprehension to extract the error messages, the code now uses a nested list comprehension to split the error messages and extract the relevant information. This change ensures that the error message is properly formatted and provides more informative details about the validation errors.

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
Co-authored-by: Lucas Oliveira <lucas.edu.oli@hotmail.com>

* feat: Update TableMixin to support TableSchema or list of Columns

The TableMixin class in input_mixin.py has been updated to support either a TableSchema object or a list of Columns for the table_schema attribute. This change allows for more flexibility in defining the table schema for input validation.

* feat: Update TableNodeComponent to generate backend columns from value

Refactor the TableNodeComponent to generate backend columns from the value when the columns prop is not provided. This change ensures that the component can handle dynamic column generation based on the value, improving flexibility and usability.

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
Co-authored-by: Lucas Oliveira <lucas.edu.oli@hotmail.com>

* Refactor extractColumnsFromRows function to return only ColDef objects

The extractColumnsFromRows function in utils.ts has been refactored to return only ColDef objects instead of a combination of ColDef and ColGroupDef objects. This change simplifies the function's return type and improves consistency in the codebase.

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
Co-authored-by: Lucas Oliveira <lucas.edu.oli@hotmail.com>

* [autofix.ci] apply automated fixes

* refactor: Generate backend columns from value in TableNodeComponent

Refactor the TableNodeComponent to generate backend columns from the value when the columns prop is not provided. This change ensures that the component can handle dynamic column generation based on the value, improving flexibility and usability.

* feat: Update TableNodeComponent to handle number and date properly

* fix bug that delete all rows on modal close

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
Co-authored-by: Lucas Oliveira <lucas.edu.oli@hotmail.com>
This commit is contained in:
anovazzi1 2024-07-24 10:20:58 -03:00 • committed by GitHub
commit 007c38afed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 496 additions and 54 deletions

View file

@ -421,7 +421,7 @@ def build_custom_component_template(
raise HTTPException(
status_code=400,
detail={
"error": (f"Something went wrong while building the custom component. Hints: {str(exc)}"),
"error": (f"Error building Component: {str(exc)}"),
"traceback": traceback.format_exc(),
},
) from exc

View file

@ -7,6 +7,7 @@ import types
from enum import Enum
from typing import TYPE_CHECKING, Any, AsyncIterator, Callable, Dict, Iterator, List, Mapping, Optional, Set
import pandas as pd
from loguru import logger
from langflow.exceptions.component import ComponentBuildException
@ -373,6 +374,13 @@ class Vertex:
params[field_name] = val
elif isinstance(val, str):
params[field_name] = val != ""
elif field.get("type") == "table" and val is not None:
# check if the value is a list of dicts
# if it is, create a pandas dataframe from it
if isinstance(val, list) and all(isinstance(item, dict) for item in val):
params[field_name] = pd.DataFrame(val)
else:
raise ValueError(f"Invalid value type {type(val)} for field {field_name}")
elif val is not None and val != "":
params[field_name] = val

View file

@ -15,6 +15,7 @@ from .inputs import (
PromptInput,
SecretStrInput,
StrInput,
TableInput,
)
__all__ = [
@ -34,4 +35,5 @@ __all__ = [
"SecretStrInput",
"StrInput",
"MessageTextInput",
"TableInput",
]

View file

@ -5,6 +5,7 @@ from pydantic import BaseModel, ConfigDict, Field, PlainSerializer, field_valida
from langflow.field_typing.range_spec import RangeSpec
from langflow.inputs.validators import CoalesceBool
from langflow.schema.table import Column, TableSchema
class FieldTypes(str, Enum):
@ -18,6 +19,7 @@ class FieldTypes(str, Enum):
FILE = "file"
PROMPT = "prompt"
OTHER = "other"
TABLE = "table"
SerializableFieldTypes = Annotated[FieldTypes, PlainSerializer(lambda v: v.value, return_type=str)]
@ -136,3 +138,16 @@ class DropDownMixin(BaseModel):
class MultilineMixin(BaseModel):
multiline: CoalesceBool = True
class TableMixin(BaseModel):
table_schema: Optional[TableSchema | list[Column]] = None
@field_validator("table_schema")
@classmethod
def validate_table_schema(cls, v):
if isinstance(v, list) and all(isinstance(column, Column) for column in v):
return TableSchema(columns=v)
if isinstance(v, TableSchema):
return v
raise ValueError("table_schema must be a TableSchema or a list of Columns")

View file

@ -19,9 +19,29 @@ from .input_mixin import (
MultilineMixin,
RangeMixin,
SerializableFieldTypes,
TableMixin,
)
class TableInput(BaseInputMixin, MetadataTraceMixin, TableMixin, ListableInputMixin):
field_type: Optional[SerializableFieldTypes] = FieldTypes.TABLE
is_list: bool = True
@field_validator("value")
@classmethod
def validate_value(cls, v: Any, _info):
# Check if value is a list of dicts
if not isinstance(v, list):
raise ValueError(f"TableInput value must be a list of dictionaries or Data. Value '{v}' is not a list.")
for item in v:
if not isinstance(item, (dict, Data)):
raise ValueError(
f"TableInput value must be a list of dictionaries or Data. Item '{item}' is not a dictionary or Data."
)
return v
class HandleInput(BaseInputMixin, ListableInputMixin, MetadataTraceMixin):
"""
Represents an Input that has a Handle to a specific type (e.g. BaseLanguageModel, BaseRetriever, etc.)
@ -338,4 +358,5 @@ InputTypes = Union[
StrInput,
MessageTextInput,
MessageInput,
TableInput,
]

View file

@ -15,6 +15,7 @@ from langflow.inputs import (
PromptInput,
SecretStrInput,
StrInput,
TableInput,
)
from langflow.template import Output
@ -36,4 +37,5 @@ __all__ = [
"StrInput",
"MessageTextInput",
"Output",
"TableInput",
]

View file

@ -0,0 +1,31 @@
from enum import Enum
from typing import List, Optional
from pydantic import BaseModel, Field, field_validator
class FormatterType(str, Enum):
date = "date"
text = "text"
number = "number"
json = "json"
class Column(BaseModel):
display_name: str
name: str
sortable: bool = Field(default=True)
filterable: bool = Field(default=True)
formatter: Optional[FormatterType | str] = None
@field_validator("formatter")
def validate_formatter(cls, value):
if isinstance(value, str):
return FormatterType(value)
if isinstance(value, FormatterType):
return value
raise ValueError("Invalid formatter type")
class TableSchema(BaseModel):
columns: List[Column]

View file

@ -4,6 +4,8 @@ import importlib
from types import FunctionType
from typing import Dict, List, Optional, Union
from pydantic import ValidationError
from langflow.field_typing.constants import CUSTOM_COMPONENT_SUPPORTED_TYPES
@ -168,8 +170,12 @@ def create_class(code, class_name):
class_code = extract_class_code(module, class_name)
compiled_class = compile_class_code(class_code)
return build_class_constructor(compiled_class, exec_globals, class_name)
try:
return build_class_constructor(compiled_class, exec_globals, class_name)
except ValidationError as e:
messages = [error["msg"].split(",", 1) for error in e.errors()]
error_message = "\n".join([message[1] if len(message) > 1 else message[0] for message in messages])
raise ValueError(error_message) from e
def create_type_ignore_class():