Passing Data between Components
There are two ways of passing data between components: through ports and by utilizing the Xircuits context (ctx). The guide below will walk you through a simple example using ports.
Passing Data via Ports
Passing data via ports is as simple as declaring the port variable as InArg
or OutArg
, which will then be rendered by the Xircuits canvas.
Start with a simple component definition that prints Hello.
Code Snippet
from xai_components.base import InArg, OutArg, InCompArg, Component, xai_component
@xai_component(color="red")
class HelloOutComponent(Component):
def __init__(self):
self.done = False
def execute(self, ctx) -> None:
username = "Xircuits"
print("Hello " + username + " from HelloOutComponent!")
self.done = TrueVideo
Declare an
outPort
with typeOutArg
that passes a string. Is it also a good practice to specify it as empty when you initialize in__init__(self)
. Finally to pass the data to the next components, explicitly set the port value in theexecute()
method, shown in#(3)
.Code Snippet
from xai_components.base import InArg, OutArg, InCompArg, Component, xai_component
@xai_component(color="red")
class HelloOutComponent(Component):
outport_example: OutArg[str] #(1)
def __init__(self):
self.outport_example = OutArg.empty() #(2)
self.done = False
def execute(self, ctx) -> None:
username = "Xircuits"
print("Hello " + username + " from HelloOutComponent!")
self.outport_example.value = username #(3)
self.done = TrueVideo
Create another Xircuits component has an
InPort
. As with the previous component, declare the type asInArg
which takes in astr
type. To access the data passed from the port, you can simply call it viaself.port_name.value
as shown in#(4)
.Code Snippet
@xai_component(color="red")
class HelloInComponent(Component):
inport_example: InArg[str]
def __init__(self):
self.inport_example = InArg.empty()
self.done = False
def execute(self, ctx) -> None:
username = self.inport_example.value #(4)
print("Hello " + username + " from HelloInComponent!")
self.done = TrueVideo
Notes:
- An inPort can also be linked to a
Literal
or aHyperparameter
component given the correct data type. - Declaring the port type as
any
will bypass the port type check.
Read More: