code.py 1.7 KB

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