From a23c53dd14b11e3989389bbf8b17580ac48395cf Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 23 Jun 2023 09:13:22 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20feat(base.py):=20add=20sorting?= =?UTF-8?q?=20of=20fields=20based=20on=20DIRECT=5FTYPES=20The=20`sort=5Ffi?= =?UTF-8?q?elds`=20method=20has=20been=20added=20to=20the=20`Template`=20c?= =?UTF-8?q?lass=20to=20sort=20fields=20based=20on=20the=20`DIRECT=5FTYPES`?= =?UTF-8?q?=20constant.=20Fields=20that=20have=20a=20`field=5Ftype`=20in?= =?UTF-8?q?=20`DIRECT=5FTYPES`=20are=20sorted=20first,=20followed=20by=20t?= =?UTF-8?q?he=20remaining=20fields.=20This=20ensures=20that=20fields=20tha?= =?UTF-8?q?t=20have=20a=20direct=20type=20are=20processed=20first,=20which?= =?UTF-8?q?=20is=20important=20for=20the=20correct=20functioning=20of=20th?= =?UTF-8?q?e=20template.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/template/template/base.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/backend/langflow/template/template/base.py b/src/backend/langflow/template/template/base.py index 52d53007b..8c9073df2 100644 --- a/src/backend/langflow/template/template/base.py +++ b/src/backend/langflow/template/template/base.py @@ -3,6 +3,7 @@ from typing import Callable, Optional, Union from pydantic import BaseModel from langflow.template.field.base import TemplateField +from langflow.utils.constants import DIRECT_TYPES class Template(BaseModel): @@ -18,8 +19,17 @@ class Template(BaseModel): for field in self.fields: format_field_func(field, name) + def sort_fields(self): + # sort fields so that fields that have .field_type in DIRECT_TYPES are first + self.fields.sort( + key=lambda x: DIRECT_TYPES.index(x.field_type) + if x.field_type in DIRECT_TYPES + else 100 + ) + def to_dict(self, format_field_func=None): self.process_fields(self.type_name, format_field_func) + self.sort_fields() result = {field.name: field.to_dict() for field in self.fields} result["_type"] = self.type_name # type: ignore return result