-
Notifications
You must be signed in to change notification settings - Fork 8.3k
feat: add convert component with dynamic output support #7773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 51 commits
db7d221
0d8388b
52d667c
a12ef7f
4658c14
bf30499
c3164f9
588fc41
765d40f
e65c60e
87ef657
3dd3307
2c0ba21
6ab11ff
00a4b16
7ff74ae
ec023e2
c02b3ef
c555554
2c97cab
db08285
b4e83cc
5343410
b779bd3
78b0701
d00eb5c
357d1e3
f0ff9df
13a8d70
08136b9
7705c72
bd01612
61370c1
d1a62ef
6e108ae
484faf6
54d66f5
33f467b
8613a67
9475ff6
19dcfdc
f7ef291
5953a6b
9d2ffae
3903d7b
835b7ce
821f9f0
5a2f9f6
b7314f0
53af8db
84ed405
dc498f6
619cd6a
1d8fc2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,105 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from langflow.custom import Component | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from langflow.io import HandleInput, Output, TabInput | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from langflow.schema import Data, DataFrame, Message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def convert_to_message(v) -> Message: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Convert input to Message type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| v: Input to convert (Message, Data, DataFrame, or dict) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Message: Converted Message object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return v if isinstance(v, Message) else v.to_message() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def convert_to_data(v: DataFrame | Data | Message | dict) -> Data: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Convert input to Data type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| v: Input to convert (Message, Data, DataFrame, or dict) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Data: Converted Data object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if isinstance(v, dict): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Data(v) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return v if isinstance(v, Data) else v.to_data() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def convert_to_dataframe(v: DataFrame | Data | Message | dict) -> DataFrame: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Convert input to DataFrame type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| v: Input to convert (Message, Data, DataFrame, or dict) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DataFrame: Converted DataFrame object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return v if isinstance(v, DataFrame) else v.to_dataframe() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class TypeConverterComponent(Component): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| display_name = "Type Convert" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description = "Convert between different types (Message, Data, DataFrame)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| icon = "repeat" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inputs = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HandleInput( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name="input_data", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| display_name="Input", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_types=["Message", "Data", "DataFrame"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info="Accept Message, Data or DataFrame as input", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required=True, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TabInput( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name="output_type", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| display_name="Output Type", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options=["Message", "Data", "DataFrame"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info="Select the desired output data type", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real_time_refresh=True, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value="Message", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| outputs = [Output(display_name="Message Output", name="message_output", method="convert_to_message")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def update_outputs(self, frontend_node: dict, field_name: str, field_value: Any) -> dict: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Dynamically show only the relevant output based on the selected output type.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if field_name == "output_type": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Start with empty outputs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| frontend_node["outputs"] = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Add only the selected output type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if field_value == "Message": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| frontend_node["outputs"].append( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Output(display_name="Message Output", name="message_output", method="convert_to_message").to_dict() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif field_value == "Data": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| frontend_node["outputs"].append( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Output(display_name="Data Output", name="data_output", method="convert_to_data").to_dict() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif field_value == "DataFrame": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| frontend_node["outputs"].append( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Output( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| display_name="DataFrame Output", name="dataframe_output", method="convert_to_dataframe" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ).to_dict() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return frontend_node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+73
to
+95
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for invalid output types. The def update_outputs(self, frontend_node: dict, field_name: str, field_value: Any) -> dict:
"""Dynamically show only the relevant output based on the selected output type."""
if field_name == "output_type":
# Start with empty outputs
frontend_node["outputs"] = []
# Add only the selected output type
if field_value == "Message":
frontend_node["outputs"].append(
Output(
display_name="Message Output",
name="message_output",
method="convert_to_message"
).to_dict()
)
elif field_value == "Data":
frontend_node["outputs"].append(
Output(
display_name="Data Output",
name="data_output",
method="convert_to_data"
).to_dict()
)
elif field_value == "DataFrame":
frontend_node["outputs"].append(
Output(
display_name="DataFrame Output",
name="dataframe_output",
method="convert_to_dataframe"
).to_dict()
)
+ else:
+ # Handle invalid output type by defaulting to Message
+ frontend_node["outputs"].append(
+ Output(
+ display_name="Message Output",
+ name="message_output",
+ method="convert_to_message"
+ ).to_dict()
+ )
return frontend_node📝 Committable suggestion
Suggested change
🧰 Tools🪛 Pylint (3.3.7)[convention] 80-80: Line too long (119/100) (C0301) [convention] 84-84: Line too long (110/100) (C0301) [convention] 89-89: Line too long (111/100) (C0301) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def convert_to_message(self) -> Message: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Convert input to Message type.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return convert_to_message(self.input_data[0] if isinstance(self.input_data, list) else self.input_data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def convert_to_data(self) -> Data: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Convert input to Data type.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return convert_to_data(self.input_data[0] if isinstance(self.input_data, list) else self.input_data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def convert_to_dataframe(self) -> DataFrame: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Convert input to DataFrame type.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return convert_to_dataframe(self.input_data[0] if isinstance(self.input_data, list) else self.input_data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+97
to
+107
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Extract common input processing logic to reduce duplication. All three conversion methods use identical logic to handle list inputs. This violates the DRY principle. + def _get_input_value(self):
+ """Extract the actual input value, handling list inputs."""
+ return self.input_data[0] if isinstance(self.input_data, list) else self.input_data
+
def convert_to_message(self) -> Message:
"""Convert input to Message type."""
- return convert_to_message(self.input_data[0] if isinstance(self.input_data, list) else self.input_data)
+ return convert_to_message(self._get_input_value())
def convert_to_data(self) -> Data:
"""Convert input to Data type."""
- return convert_to_data(self.input_data[0] if isinstance(self.input_data, list) else self.input_data)
+ return convert_to_data(self._get_input_value())
def convert_to_dataframe(self) -> DataFrame:
"""Convert input to DataFrame type."""
- return convert_to_dataframe(self.input_data[0] if isinstance(self.input_data, list) else self.input_data)
+ return convert_to_dataframe(self._get_input_value())📝 Committable suggestion
Suggested change
🧰 Tools🪛 Pylint (3.3.7)[convention] 97-97: Line too long (111/100) (C0301) [convention] 101-101: Line too long (108/100) (C0301) [convention] 105-105: Line too long (113/100) (C0301) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import pandas as pd | ||
| import pytest | ||
| from langflow.components.processing.converter import TypeConverterComponent | ||
| from langflow.schema.data import Data | ||
| from langflow.schema.dataframe import DataFrame | ||
| from langflow.schema.message import Message | ||
|
|
||
| from tests.base import ComponentTestBaseWithoutClient | ||
|
|
||
|
|
||
| class TestTypeConverterComponent(ComponentTestBaseWithoutClient): | ||
| @pytest.fixture | ||
| def component_class(self): | ||
| """Return the component class to test.""" | ||
| return TypeConverterComponent | ||
|
|
||
| @pytest.fixture | ||
| def file_names_mapping(self): | ||
| """Return an empty list since this component doesn't have version-specific files.""" | ||
| return [] | ||
|
|
||
| # Message to other types | ||
| def test_message_to_message(self, component_class): | ||
| """Test converting Message to Message.""" | ||
| component = component_class(input_data=Message(text="Hello World"), output_type="Message") | ||
| result = component.convert_to_message() | ||
| assert isinstance(result, Message) | ||
| assert result.text == "Hello World" | ||
|
|
||
| def test_message_to_data(self, component_class): | ||
| """Test converting Message to Data.""" | ||
| component = component_class(input_data=Message(text="Hello"), output_type="Data") | ||
| result = component.convert_to_data() | ||
| assert isinstance(result, Data) | ||
| assert "text" in result.data | ||
| assert result.data["text"] == "Hello" | ||
|
|
||
| def test_message_to_dataframe(self, component_class): | ||
| """Test converting Message to DataFrame.""" | ||
| component = component_class(input_data=Message(text="Hello"), output_type="DataFrame") | ||
| result = component.convert_to_dataframe() | ||
| assert isinstance(result, DataFrame) | ||
| assert "text" in result.columns | ||
| assert result.iloc[0]["text"] == "Hello" | ||
|
|
||
| # Data to other types | ||
| def test_data_to_message(self, component_class): | ||
| """Test converting Data to Message.""" | ||
| component = component_class(input_data=Data(data={"text": "Hello World"}), output_type="Message") | ||
| result = component.convert_to_message() | ||
| assert isinstance(result, Message) | ||
| assert result.text == "Hello World" | ||
|
|
||
| def test_data_to_data(self, component_class): | ||
| """Test converting Data to Data.""" | ||
| component = component_class(input_data=Data(data={"key": "value"}), output_type="Data") | ||
| result = component.convert_to_data() | ||
| assert isinstance(result, Data) | ||
| assert result.data == {"key": "value"} | ||
|
|
||
| def test_data_to_dataframe(self, component_class): | ||
| """Test converting Data to DataFrame.""" | ||
| component = component_class(input_data=Data(data={"text": "Hello World"}), output_type="DataFrame") | ||
| result = component.convert_to_dataframe() | ||
| assert isinstance(result, DataFrame) | ||
| assert "text" in result.columns | ||
| assert result.iloc[0]["text"] == "Hello World" | ||
|
|
||
| # DataFrame to other types | ||
| def test_dataframe_to_message(self, component_class): | ||
| """Test converting DataFrame to Message.""" | ||
| df_data = pd.DataFrame({"col1": ["Hello"], "col2": ["World"]}) | ||
| component = component_class(input_data=DataFrame(data=df_data), output_type="Message") | ||
| result = component.convert_to_message() | ||
| assert isinstance(result, Message) | ||
| assert result.text == "| col1 | col2 |\n|:-------|:-------|\n| Hello | World |" | ||
|
|
||
| def test_dataframe_to_data(self, component_class): | ||
| """Test converting DataFrame to Data.""" | ||
| df_data = pd.DataFrame({"col1": ["Hello"]}) | ||
| component = component_class(input_data=DataFrame(data=df_data), output_type="Data") | ||
| result = component.convert_to_data() | ||
| assert isinstance(result, Data) | ||
| assert isinstance(result.data, dict) | ||
|
|
||
| def test_dataframe_to_dataframe(self, component_class): | ||
| """Test converting DataFrame to DataFrame.""" | ||
| df_data = pd.DataFrame({"col1": ["Hello"], "col2": ["World"]}) | ||
| component = component_class(input_data=DataFrame(data=df_data), output_type="DataFrame") | ||
| result = component.convert_to_dataframe() | ||
| assert isinstance(result, DataFrame) | ||
| assert "col1" in result.columns | ||
| assert "col2" in result.columns | ||
| assert result.iloc[0]["col1"] == "Hello" | ||
| assert result.iloc[0]["col2"] == "World" | ||
|
|
||
| def test_update_outputs(self, component_class): | ||
| """Test the update_outputs method.""" | ||
| component = component_class(input_data=Message(text="Hello"), output_type="Message") | ||
| frontend_node = {"outputs": []} | ||
|
|
||
| # Test with Message output | ||
| updated = component.update_outputs(frontend_node, "output_type", "Message") | ||
| assert len(updated["outputs"]) == 1 | ||
| assert updated["outputs"][0]["name"] == "message_output" | ||
|
|
||
| # Test with Data output | ||
| updated = component.update_outputs(frontend_node, "output_type", "Data") | ||
| assert len(updated["outputs"]) == 1 | ||
| assert updated["outputs"][0]["name"] == "data_output" | ||
|
|
||
| # Test with DataFrame output | ||
| updated = component.update_outputs(frontend_node, "output_type", "DataFrame") | ||
| assert len(updated["outputs"]) == 1 | ||
| assert updated["outputs"][0]["name"] == "dataframe_output" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add class docstring and improve component metadata.
The component class needs documentation and could benefit from better metadata.
📝 Committable suggestion
🧰 Tools
🪛 Pylint (3.3.7)
[convention] 102-102: Line too long (105/100)
(C0301)
[convention] 79-79: Missing class docstring
(C0115)
🤖 Prompt for AI Agents