From 22839ee270151fd6b93cd290d4eab943bcc59a42 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sun, 23 Jun 2024 01:52:55 -0300 Subject: [PATCH] refactor: Update post_code_processing method in CustomComponent class Refactor the post_code_processing method in the CustomComponent class to improve code readability and maintainability. The changes include: - Import the update_frontend_node_with_template_values function from langflow.template.utils. - Update the function signature to accept new_build_config and current_build_config as parameters. - Call the update_frontend_node_with_template_values function to update the frontend_node with any values set in the current_build_config. - Remove unnecessary comments and whitespace. These changes enhance the overall quality of the code and make it easier to understand and maintain. --- src/backend/base/langflow/api/v1/endpoints.py | 4 ++-- .../base/langflow/custom/custom_component/component.py | 9 --------- .../custom/custom_component/custom_component.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/backend/base/langflow/api/v1/endpoints.py b/src/backend/base/langflow/api/v1/endpoints.py index 93cdf2ce3..b4054fd3d 100644 --- a/src/backend/base/langflow/api/v1/endpoints.py +++ b/src/backend/base/langflow/api/v1/endpoints.py @@ -466,8 +466,8 @@ async def custom_component( component = Component(code=raw_code.code) built_frontend_node, component_instance = build_custom_component_template(component, user_id=user.id) - - built_frontend_node = component_instance.post_code_processing(built_frontend_node, raw_code.frontend_node) + if raw_code.frontend_node is not None: + built_frontend_node = component_instance.post_code_processing(built_frontend_node, raw_code.frontend_node) return built_frontend_node diff --git a/src/backend/base/langflow/custom/custom_component/component.py b/src/backend/base/langflow/custom/custom_component/component.py index b945f491f..592d8e2b3 100644 --- a/src/backend/base/langflow/custom/custom_component/component.py +++ b/src/backend/base/langflow/custom/custom_component/component.py @@ -225,12 +225,3 @@ class Component(CustomComponent): def build(self, **kwargs): self.set_attributes(kwargs) - - def post_code_processing(self, new_build_config: dict, current_build_config: dict): - """ - This function is called after the code validation is done. - """ - frontend_node = update_frontend_node_with_template_values( - frontend_node=new_build_config, raw_frontend_node=current_build_config - ) - return frontend_node diff --git a/src/backend/base/langflow/custom/custom_component/custom_component.py b/src/backend/base/langflow/custom/custom_component/custom_component.py index 369199148..5505ae132 100644 --- a/src/backend/base/langflow/custom/custom_component/custom_component.py +++ b/src/backend/base/langflow/custom/custom_component/custom_component.py @@ -17,6 +17,7 @@ from langflow.schema.schema import OutputLog from langflow.services.deps import get_storage_service, get_variable_service, session_scope from langflow.services.storage.service import StorageService from langflow.services.tracing.schema import Log +from langflow.template.utils import update_frontend_node_with_template_values from langflow.type_extraction.type_extraction import ( extract_inner_type_from_generic_alias, extract_union_types_from_generic_alias, @@ -495,3 +496,12 @@ class CustomComponent(BaseComponent): self._logs.append(log) if self.vertex: self._tracing_service.add_log(trace_name=self.vertex.id, log=log) + + def post_code_processing(self, new_build_config: dict, current_build_config: dict): + """ + This function is called after the code validation is done. + """ + frontend_node = update_frontend_node_with_template_values( + frontend_node=new_build_config, raw_frontend_node=current_build_config + ) + return frontend_node