MCPcopy Create free account
hub / github.com/dot-agent/nextpy / CustomComponent

Class CustomComponent

nextpy/frontend/components/component.py:1116–1263  ·  view source on GitHub ↗

A custom user-defined component.

Source from the content-addressed store, hash-verified

1114
1115
1116class CustomComponent(Component):
1117 """A custom user-defined component."""
1118
1119 # Use the components library.
1120 library = f"/{Dirs.COMPONENTS_PATH}"
1121
1122 # The function that creates the component.
1123 component_fn: Callable[..., Component] = Component.create
1124
1125 # The props of the component.
1126 props: Dict[str, Any] = {}
1127
1128 def __init__(self, *args, **kwargs):
1129 """Initialize the custom component.
1130
1131 Args:
1132 *args: The args to pass to the component.
1133 **kwargs: The kwargs to pass to the component.
1134 """
1135 super().__init__(*args, **kwargs)
1136
1137 # Unset the style.
1138 self.style = Style()
1139
1140 # Set the tag to the name of the function.
1141 self.tag = format.to_title_case(self.component_fn.__name__)
1142
1143 # Set the props.
1144 props = typing.get_type_hints(self.component_fn)
1145 for key, value in kwargs.items():
1146 # Skip kwargs that are not props.
1147 if key not in props:
1148 continue
1149
1150 # Get the type based on the annotation.
1151 type_ = props[key]
1152
1153 # Handle event chains.
1154 if types._issubclass(type_, EventChain):
1155 value = self._create_event_chain(key, value)
1156 self.props[format.to_camel_case(key)] = value
1157 continue
1158
1159 # Convert the type to a Var, then get the type of the var.
1160 if not types._issubclass(type_, Var):
1161 type_ = Var[type_]
1162 type_ = types.get_args(type_)[0]
1163
1164 # Handle subclasses of Base.
1165 if types._issubclass(type_, Base):
1166 try:
1167 value = BaseVar(
1168 _var_name=value.json(), _var_type=type_, _var_is_local=True
1169 )
1170 except Exception:
1171 value = Var.create(value)
1172 else:
1173 value = Var.create(value, _var_is_string=type(value) is str)

Callers 3

wrapperFunction · 0.85

Calls

no outgoing calls

Tested by 2