code.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """A code component."""
  2. from typing import Dict
  3. from pynecone.components.component import Component
  4. from pynecone.components.libs.chakra import ChakraComponent
  5. from pynecone.var import Var
  6. class CodeBlock(Component):
  7. """A code block."""
  8. library = "react-syntax-highlighter"
  9. tag = "Prism"
  10. # The language to use.
  11. language: Var[str]
  12. # If this is enabled line numbers will be shown next to the code block.
  13. show_line_numbers: Var[bool]
  14. # The starting line number to use.
  15. starting_line_number: Var[int]
  16. # Whether to wrap long lines.
  17. wrap_long_lines: Var[bool]
  18. # A custom style for the code block.
  19. custom_style: Var[Dict[str, str]]
  20. # Props passed down to the code tag.
  21. code_tag_props: Var[Dict[str, str]]
  22. @classmethod
  23. def create(cls, *children, **props):
  24. """Create a text component.
  25. Args:
  26. *children: The children of the component.
  27. **props: The props to pass to the component.
  28. Returns:
  29. The text component.
  30. """
  31. # This component handles style in a special prop.
  32. custom_style = props.get("custom_style", {})
  33. # Transfer style props to the custom style prop.
  34. for key, value in props.items():
  35. if key not in cls.get_fields():
  36. custom_style[key] = value
  37. # Create the component.
  38. return super().create(
  39. *children,
  40. **props,
  41. )
  42. def _add_style(self, style):
  43. self.custom_style = self.custom_style or {}
  44. self.custom_style.update(style) # type: ignore
  45. class Code(ChakraComponent):
  46. """Used to display inline code."""
  47. tag = "Code"