Custom Component with no of outputs depending on length of input size #3288
Unanswered
sohamchatterjee50
asked this question in
Q&A
Replies: 1 comment 3 replies
-
Hey @sohamchatterjee50! I'm here to help you out with any issues or questions you have. Let's work together to solve the problem at hand. To create a custom component where the number of outputs depends on the length of the input size, with the output list length equal to a given integer input, you can modify the from langflow.custom import Component
from langflow.inputs import MessageTextInput, IntInput
from langflow.template import Output
from langflow.schema import Data
class DynamicOutputsComponent(Component):
display_name = "Dynamic Outputs Component"
description = "A component that generates outputs based on an integer input."
icon = "icon-name"
inputs = [
MessageTextInput(
name="input_text",
display_name="Input Text",
info="Text input for the component.",
),
IntInput(
name="number_of_outputs",
display_name="Number of Outputs",
info="Number of outputs to generate.",
),
]
def __init__(self):
super().__init__()
self.outputs = []
def generate_outputs(self):
self.outputs = [
Output(display_name=f"Output {i+1}", name=f"output_{i+1}", method=f"output_method_{i+1}")
for i in range(self.number_of_outputs)
]
def output_method(self, index: int) -> Data:
return Data(data={"result": f"Output {index+1} for input: {self.input_text}"})
def __getattr__(self, name):
if name.startswith("output_method_"):
index = int(name.split("_")[-1]) - 1
return lambda: self.output_method(index)
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")
def process(self):
self.generate_outputs()
return {output.name: getattr(self, output.method)() for output in self.outputs} In this example:
This way, the number of outputs will be equal to the integer input provided [1][2]. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there a way where the output list length of a component is equal to the given integer input ?
Beta Was this translation helpful? Give feedback.
All reactions