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

Method __init__

nextpy/frontend/components/component.py:191–309  ·  view source on GitHub ↗

Initialize the component. Args: *args: Args to initialize the component. **kwargs: Kwargs to initialize the component. Raises: TypeError: If an invalid prop is passed. ValueError: If a prop value is invalid.

(self, *args, **kwargs)

Source from the content-addressed store, hash-verified

189 field.default = Var.create(field.default)
190
191 def __init__(self, *args, **kwargs):
192 """Initialize the component.
193
194 Args:
195 *args: Args to initialize the component.
196 **kwargs: Kwargs to initialize the component.
197
198 Raises:
199 TypeError: If an invalid prop is passed.
200 ValueError: If a prop value is invalid.
201 """
202 # Set the id and children initially.
203 children = kwargs.get("children", [])
204 initial_kwargs = {
205 "id": kwargs.get("id"),
206 "children": children,
207 **{
208 prop: Var.create(kwargs[prop])
209 for prop in self.get_initial_props()
210 if prop in kwargs
211 },
212 }
213 super().__init__(**initial_kwargs)
214
215 self._validate_component_children(children)
216
217 # Get the component fields, triggers, and props.
218 fields = self.get_fields()
219 triggers = self.get_event_triggers().keys()
220 props = self.get_props()
221
222 # Add any events triggers.
223 if "event_triggers" not in kwargs:
224 kwargs["event_triggers"] = {}
225 kwargs["event_triggers"] = kwargs["event_triggers"].copy()
226
227 # Iterate through the kwargs and set the props.
228 for key, value in kwargs.items():
229 if key in triggers:
230 # Event triggers are bound to event chains.
231 field_type = EventChain
232 elif key in props:
233 # Set the field type.
234 field_type = fields[key].type_
235
236 else:
237 continue
238
239 # Check whether the key is a component prop.
240 if types._issubclass(field_type, Var):
241 try:
242 # Try to create a var from the value.
243 kwargs[key] = Var.create(value)
244
245 # Check that the var type is not None.
246 if kwargs[key] is None:
247 raise TypeError
248

Callers 1

__init__Method · 0.45

Calls 13

get_initial_propsMethod · 0.95
get_event_triggersMethod · 0.95
get_propsMethod · 0.95
_create_event_chainMethod · 0.95
StyleClass · 0.90
get_fieldsMethod · 0.80
itemsMethod · 0.80
joinMethod · 0.80
getMethod · 0.45
createMethod · 0.45
keysMethod · 0.45

Tested by

no test coverage detected