1
+ import json
1
2
from typing import Any
2
3
3
4
from langflow .custom import Component
7
8
MessageTextInput ,
8
9
MultilineInput ,
9
10
Output ,
11
+ TabInput ,
10
12
)
11
13
from langflow .schema import Data , DataFrame
12
14
from langflow .schema .message import Message
@@ -19,18 +21,18 @@ class ParserComponent(Component):
19
21
"Enable 'Stringify' to convert input into a readable string instead."
20
22
)
21
23
icon = "braces"
22
- beta = True
23
24
24
25
inputs = [
25
- BoolInput (
26
- name = "stringify" ,
27
- display_name = "Stringify" ,
28
- info = "Enable to convert input to a string instead of using a template." ,
29
- value = False ,
26
+ TabInput (
27
+ name = "mode" ,
28
+ display_name = "Mode" ,
29
+ options = ["Parser" , "Stringify" ],
30
+ value = "Parser" ,
31
+ info = "Convert into raw string instead of using a template." ,
30
32
real_time_refresh = True ,
31
33
),
32
34
MultilineInput (
33
- name = "template " ,
35
+ name = "pattern " ,
34
36
display_name = "Template" ,
35
37
info = (
36
38
"Use variables within curly brackets to extract column values for DataFrames "
@@ -69,9 +71,9 @@ class ParserComponent(Component):
69
71
70
72
def update_build_config (self , build_config , field_value , field_name = None ):
71
73
"""Dynamically hide/show `template` and enforce requirement based on `stringify`."""
72
- if field_name == "stringify " :
73
- build_config ["template " ]["show" ] = not field_value
74
- build_config ["template " ]["required" ] = not field_value
74
+ if field_name == "mode " :
75
+ build_config ["pattern " ]["show" ] = self . mode == "Parser"
76
+ build_config ["pattern " ]["required" ] = self . mode == "Parser"
75
77
if field_value :
76
78
clean_data = BoolInput (
77
79
name = "clean_data" ,
@@ -118,18 +120,18 @@ def _clean_args(self):
118
120
def parse_combined_text (self ) -> Message :
119
121
"""Parse all rows/items into a single text or convert input to string if `stringify` is enabled."""
120
122
# Early return for stringify option
121
- if self .stringify :
123
+ if self .mode == "Stringify" :
122
124
return self .convert_to_string ()
123
125
124
126
df , data = self ._clean_args ()
125
127
126
128
lines = []
127
129
if df is not None :
128
130
for _ , row in df .iterrows ():
129
- formatted_text = self .template .format (** row .to_dict ())
131
+ formatted_text = self .pattern .format (** row .to_dict ())
130
132
lines .append (formatted_text )
131
133
elif data is not None :
132
- formatted_text = self .template .format (text = data .get_text () )
134
+ formatted_text = self .pattern .format (** data .data )
133
135
lines .append (formatted_text )
134
136
135
137
combined_text = self .sep .join (lines )
@@ -144,10 +146,7 @@ def _safe_convert(self, data: Any) -> str:
144
146
if isinstance (data , Message ):
145
147
return data .get_text ()
146
148
if isinstance (data , Data ):
147
- if data .get_text () is None :
148
- msg = "Empty Data object"
149
- raise ValueError (msg )
150
- return data .get_text ()
149
+ return json .dumps (data .data )
151
150
if isinstance (data , DataFrame ):
152
151
if hasattr (self , "clean_data" ) and self .clean_data :
153
152
# Remove empty rows
@@ -170,4 +169,7 @@ def convert_to_string(self) -> Message:
170
169
else :
171
170
result = self ._safe_convert (self .input_data )
172
171
self .log (f"Converted to string with length: { len (result )} " )
173
- return Message (text = result )
172
+
173
+ message = Message (text = result )
174
+ self .status = message
175
+ return message
0 commit comments