🚀 feat(base.py): add sorting of fields based on DIRECT_TYPES
The `sort_fields` method has been added to the `Template` class to sort fields based on the `DIRECT_TYPES` constant. Fields that have a `field_type` in `DIRECT_TYPES` are sorted first, followed by the remaining fields. This ensures that fields that have a direct type are processed first, which is important for the correct functioning of the template.
This commit is contained in:
parent
9f00421d64
commit
a23c53dd14
1 changed files with 10 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue