__init__.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """Import all the components."""
  2. from __future__ import annotations
  3. from typing import TYPE_CHECKING
  4. from pynecone import utils
  5. from .component import Component
  6. from .datadisplay import *
  7. from .disclosure import *
  8. from .feedback import *
  9. from .forms import *
  10. from .graphing import *
  11. from .layout import *
  12. from .media import *
  13. from .navigation import *
  14. from .overlay import *
  15. from .typography import *
  16. if TYPE_CHECKING:
  17. from typing import Any
  18. # Add the convenience methods for all the components.
  19. locals().update(
  20. {
  21. utils.to_snake_case(name): value.create
  22. for name, value in locals().items()
  23. if isinstance(value, type) and issubclass(value, Component)
  24. }
  25. )
  26. # Add responsive styles shortcuts.
  27. def mobile_only(*children, **props):
  28. """Create a component that is only visible on mobile.
  29. Args:
  30. *children: The children to pass to the component.
  31. **props: The props to pass to the component.
  32. Returns:
  33. The component.
  34. """
  35. return Box.create(*children, **props, display=["block", "none", "none", "none"])
  36. def tablet_only(*children, **props):
  37. """Create a component that is only visible on tablet.
  38. Args:
  39. *children: The children to pass to the component.
  40. **props: The props to pass to the component.
  41. Returns:
  42. The component.
  43. """
  44. return Box.create(*children, **props, display=["none", "block", "block", "none"])
  45. def desktop_only(*children, **props):
  46. """Create a component that is only visible on desktop.
  47. Args:
  48. *children: The children to pass to the component.
  49. **props: The props to pass to the component.
  50. Returns:
  51. The component.
  52. """
  53. return Box.create(*children, **props, display=["none", "none", "none", "block"])
  54. def tablet_and_desktop(*children, **props):
  55. """Create a component that is only visible on tablet and desktop.
  56. Args:
  57. *children: The children to pass to the component.
  58. **props: The props to pass to the component.
  59. Returns:
  60. The component.
  61. """
  62. return Box.create(*children, **props, display=["none", "block", "block", "block"])
  63. def mobile_and_tablet(*children, **props):
  64. """Create a component that is only visible on mobile and tablet.
  65. Args:
  66. *children: The children to pass to the component.
  67. **props: The props to pass to the component.
  68. Returns:
  69. The component.
  70. """
  71. return Box.create(*children, **props, display=["block", "block", "block", "none"])
  72. def cond(condition: Any, c1: Any, c2: Any = None):
  73. """Create a conditional component or Prop.
  74. Args:
  75. condition: The cond to determine which component to render.
  76. c1: The component or prop to render if the cond_var is true.
  77. c2: The component or prop to render if the cond_var is false.
  78. Returns:
  79. The conditional component.
  80. """
  81. # Import here to avoid circular imports.
  82. from pynecone.var import Var
  83. from .tags.tag import PropCond
  84. # Convert the condition to a Var.
  85. cond_var = Var.create(condition)
  86. assert cond_var is not None, "The condition must be set."
  87. # If the first component is a component, create a Cond component.
  88. if isinstance(c1, Component):
  89. assert c2 is None or isinstance(
  90. c2, Component
  91. ), "Both arguments must be components."
  92. return Cond.create(cond_var, c1, c2)
  93. # Otherwise, create a PropCond.
  94. assert not isinstance(c2, Component), "Both arguments must be props."
  95. return PropCond.create(cond_var, c1, c2)