🐛 fix(custom.py): add support for parsing annotated assignments in CustomComponent class

🐛 fix(custom_components.py): update import statement for DEFAULT_CUSTOM_COMPONENT_CODE constant
🐛 fix(tools.py): update import statement for DEFAULT_CUSTOM_COMPONENT_CODE constant

The `CustomComponent` class now supports parsing annotated assignments, allowing attributes with annotated values to be added to the `attributes` dictionary. This improves the flexibility and extensibility of the class.

The import statement for the `DEFAULT_CUSTOM_COMPONENT_CODE` constant in `custom_components.py` and `tools.py` has been updated to reflect the correct location of the constant in the `langflow.interface.custom.constants` module. This ensures that the correct value is imported and used in the code.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-07-07 00:42:16 -03:00
commit daad3bb4e7
3 changed files with 8 additions and 2 deletions

View file

@ -46,6 +46,10 @@ class CustomComponent(BaseModel):
if isinstance(target, ast.Name): # A simple variable
# Add the attribute and its value to the dictionary
attributes[target.id] = ast.unparse(inner_node.value)
elif isinstance(inner_node, ast.AnnAssign): # An annotated assignment
if isinstance(inner_node.target, ast.Name) and inner_node.value:
attributes[inner_node.target.id] = ast.unparse(inner_node.value)
elif isinstance(inner_node, ast.FunctionDef):
self._handle_function(inner_node)
@ -134,6 +138,8 @@ class CustomComponent(BaseModel):
template_config["display_name"] = ast.literal_eval(
attributes["display_name"]
)
if "description" in attributes:
template_config["description"] = ast.literal_eval(attributes["description"])
return template_config
def _class_template_validation(self, code: dict):

View file

@ -1,7 +1,7 @@
from langflow.template.field.base import TemplateField
from langflow.template.frontend_node.base import FrontendNode
from langflow.template.template.base import Template
from langflow.utils.constants import DEFAULT_CUSTOM_COMPONENT_CODE
from langflow.interface.custom.constants import DEFAULT_CUSTOM_COMPONENT_CODE
class CustomComponentFrontendNode(FrontendNode):

View file

@ -1,9 +1,9 @@
from langflow.interface.custom.constants import DEFAULT_CUSTOM_COMPONENT_CODE
from langflow.template.field.base import TemplateField
from langflow.template.frontend_node.base import FrontendNode
from langflow.template.template.base import Template
from langflow.utils.constants import (
DEFAULT_PYTHON_FUNCTION,
DEFAULT_CUSTOM_COMPONENT_CODE,
)