From 3bab730e65859545a62b5268119ce643772aef7d Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 3 Jul 2023 21:56:28 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=80=20refactor(base.py):=20import=20fi?= =?UTF-8?q?eld=5Fformatters=20module=20instead=20of=20importing=20specific?= =?UTF-8?q?=20module=20=F0=9F=94=80=20refactor(base.py):=20move=20FieldFor?= =?UTF-8?q?matters=20class=20to=20the=20top=20of=20the=20file=20for=20bett?= =?UTF-8?q?er=20organization=20The=20import=20statement=20in=20the=20base.?= =?UTF-8?q?py=20file=20has=20been=20updated=20to=20import=20the=20field=5F?= =?UTF-8?q?formatters=20module=20instead=20of=20importing=20specific=20mod?= =?UTF-8?q?ules.=20This=20change=20improves=20maintainability=20and=20read?= =?UTF-8?q?ability=20by=20reducing=20the=20number=20of=20import=20statemen?= =?UTF-8?q?ts=20and=20consolidating=20them=20into=20a=20single=20import.?= =?UTF-8?q?=20Additionally,=20the=20FieldFormatters=20class=20has=20been?= =?UTF-8?q?=20moved=20to=20the=20top=20of=20the=20file=20for=20better=20or?= =?UTF-8?q?ganization=20and=20readability.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../langflow/template/frontend_node/base.py | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/backend/langflow/template/frontend_node/base.py b/src/backend/langflow/template/frontend_node/base.py index 31feb638b..0ac19351b 100644 --- a/src/backend/langflow/template/frontend_node/base.py +++ b/src/backend/langflow/template/frontend_node/base.py @@ -3,7 +3,7 @@ from typing import List, Optional from pydantic import BaseModel, Field -from langflow.template.frontend_node.formatter.field_formatters import FieldFormatters +from langflow.template.frontend_node.formatter import field_formatters from langflow.template.frontend_node.constants import FORCE_SHOW_FIELDS from langflow.template.field.base import TemplateField from langflow.template.template.base import Template @@ -12,6 +12,34 @@ from langflow.utils import constants CLASSES_TO_REMOVE = ["Serializable", "BaseModel", "object"] +class FieldFormatters(BaseModel): + formatters = { + "openai_api_key": field_formatters.OpenAIAPIKeyFormatter(), + } + base_formatters = { + "kwargs": field_formatters.KwargsFormatter(), + "optional": field_formatters.RemoveOptionalFormatter(), + "list": field_formatters.ListTypeFormatter(), + "dict": field_formatters.DictTypeFormatter(), + "union": field_formatters.UnionTypeFormatter(), + "multiline": field_formatters.MultilineFieldFormatter(), + "show": field_formatters.ShowFieldFormatter(), + "password": field_formatters.PasswordFieldFormatter(), + "default": field_formatters.DefaultValueFormatter(), + "headers": field_formatters.HeadersDefaultValueFormatter(), + "dict_code_file": field_formatters.DictCodeFileFormatter(), + "model_fields": field_formatters.ModelSpecificFieldFormatter(), + } + + def format(self, field: TemplateField, name: Optional[str] = None) -> None: + for key, formatter in self.base_formatters.items(): + formatter.format(field, name) + + for key, formatter in self.formatters.items(): + if key == field.name: + formatter.format(field, name) + + class FrontendNode(BaseModel): template: Template description: str @@ -41,7 +69,6 @@ class FrontendNode(BaseModel): """Sets the documentation of the frontend node.""" self.documentation = documentation - def to_dict(self) -> dict: """Returns a dict representation of the frontend node.""" self.process_base_classes()