foreach.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """Create a list of components from an iterable."""
  2. from __future__ import annotations
  3. from typing import Any, Callable, Iterable
  4. from pynecone.components.component import Component
  5. from pynecone.components.layout.fragment import Fragment
  6. from pynecone.components.tags import IterTag
  7. from pynecone.vars import BaseVar, Var, get_unique_variable_name
  8. class Foreach(Component):
  9. """A component that takes in an iterable and a render function and renders a list of components."""
  10. # The iterable to create components from.
  11. iterable: Var[Iterable]
  12. # A function from the render args to the component.
  13. render_fn: Callable = Fragment.create
  14. @classmethod
  15. def create(cls, iterable: Var[Iterable], render_fn: Callable, **props) -> Foreach:
  16. """Create a foreach component.
  17. Args:
  18. iterable: The iterable to create components from.
  19. render_fn: A function from the render args to the component.
  20. **props: The attributes to pass to each child component.
  21. Returns:
  22. The foreach component.
  23. Raises:
  24. TypeError: If the iterable is of type Any.
  25. """
  26. try:
  27. type_ = (
  28. iterable.type_
  29. if iterable.type_.mro()[0] == dict
  30. else iterable.type_.__args__[0]
  31. )
  32. except Exception:
  33. type_ = Any
  34. iterable = Var.create(iterable) # type: ignore
  35. if iterable.type_ == Any:
  36. raise TypeError(
  37. f"Could not foreach over var of type Any. (If you are trying to foreach over a state var, add a type annotation to the var.)"
  38. )
  39. arg = BaseVar(name="_", type_=type_, is_local=True)
  40. return cls(
  41. iterable=iterable,
  42. render_fn=render_fn,
  43. children=[IterTag.render_component(render_fn, arg=arg)],
  44. **props,
  45. )
  46. def _render(self) -> IterTag:
  47. return IterTag(iterable=self.iterable, render_fn=self.render_fn)
  48. def render(self):
  49. """Render the component.
  50. Returns:
  51. The dictionary for template of component.
  52. """
  53. tag = self._render()
  54. try:
  55. type_ = (
  56. self.iterable.type_
  57. if self.iterable.type_.mro()[0] == dict
  58. else self.iterable.type_.__args__[0]
  59. )
  60. except Exception:
  61. type_ = Any
  62. arg = BaseVar(
  63. name=get_unique_variable_name(),
  64. type_=type_,
  65. )
  66. index_arg = tag.get_index_var_arg()
  67. component = tag.render_component(self.render_fn, arg)
  68. return dict(
  69. tag.add_props(
  70. **self.event_triggers,
  71. key=self.key,
  72. sx=self.style,
  73. id=self.id,
  74. class_name=self.class_name,
  75. ).set(
  76. children=[component.render()],
  77. props=tag.format_props(),
  78. ),
  79. iterable_state=tag.iterable.full_name,
  80. arg_name=arg.name,
  81. arg_index=index_arg,
  82. iterable_type=tag.iterable.type_.mro()[0].__name__,
  83. )