1
0

templates.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Templates to use in the reflex compiler."""
  2. from jinja2 import Environment, FileSystemLoader, Template
  3. from reflex import constants
  4. from reflex.utils.format import format_state_name, json_dumps
  5. class ReflexJinjaEnvironment(Environment):
  6. """The template class for jinja environment."""
  7. def __init__(self) -> None:
  8. """Set default environment."""
  9. extensions = ["jinja2.ext.debug"]
  10. super().__init__(
  11. extensions=extensions,
  12. trim_blocks=True,
  13. lstrip_blocks=True,
  14. )
  15. self.filters["json_dumps"] = json_dumps
  16. self.filters["react_setter"] = lambda state: f"set{state.capitalize()}"
  17. self.filters["var_name"] = format_state_name
  18. self.loader = FileSystemLoader(constants.Templates.Dirs.JINJA_TEMPLATE)
  19. self.globals["const"] = {
  20. "socket": constants.CompileVars.SOCKET,
  21. "result": constants.CompileVars.RESULT,
  22. "router": constants.CompileVars.ROUTER,
  23. "event_endpoint": constants.Endpoint.EVENT.name,
  24. "events": constants.CompileVars.EVENTS,
  25. "state": constants.CompileVars.STATE,
  26. "final": constants.CompileVars.FINAL,
  27. "processing": constants.CompileVars.PROCESSING,
  28. "initial_result": {
  29. constants.CompileVars.STATE: None,
  30. constants.CompileVars.EVENTS: [],
  31. constants.CompileVars.FINAL: True,
  32. constants.CompileVars.PROCESSING: False,
  33. },
  34. "color_mode": constants.ColorMode.NAME,
  35. "toggle_color_mode": constants.ColorMode.TOGGLE,
  36. "use_color_mode": constants.ColorMode.USE,
  37. "hydrate": constants.CompileVars.HYDRATE,
  38. "on_load_internal": constants.CompileVars.ON_LOAD_INTERNAL,
  39. "update_vars_internal": constants.CompileVars.UPDATE_VARS_INTERNAL,
  40. }
  41. def get_template(name: str) -> Template:
  42. """Get render function that work with a template.
  43. Args:
  44. name: The template name. "/" is used as the path separator.
  45. Returns:
  46. A render function.
  47. """
  48. return ReflexJinjaEnvironment().get_template(name=name)
  49. # Template for the Reflex config file.
  50. RXCONFIG = get_template("app/rxconfig.py.jinja2")
  51. # Code to render a NextJS Document root.
  52. DOCUMENT_ROOT = get_template("web/pages/_document.js.jinja2")
  53. # Code to render NextJS App root.
  54. APP_ROOT = get_template("web/pages/_app.js.jinja2")
  55. # Template for the theme file.
  56. THEME = get_template("web/utils/theme.js.jinja2")
  57. # Template for the context file.
  58. CONTEXT = get_template("web/utils/context.js.jinja2")
  59. # Template for Tailwind config.
  60. TAILWIND_CONFIG = get_template("web/tailwind.config.js.jinja2")
  61. # Template to render a component tag.
  62. COMPONENT = get_template("web/pages/component.js.jinja2")
  63. # Code to render a single NextJS page.
  64. PAGE = get_template("web/pages/index.js.jinja2")
  65. # Code to render the custom components page.
  66. COMPONENTS = get_template("web/pages/custom_component.js.jinja2")
  67. # Code to render Component instances as part of StatefulComponent
  68. STATEFUL_COMPONENT = get_template("web/pages/stateful_component.js.jinja2")
  69. # Code to render StatefulComponent to an external file to be shared
  70. STATEFUL_COMPONENTS = get_template("web/pages/stateful_components.js.jinja2")
  71. # Sitemap config file.
  72. SITEMAP_CONFIG = "module.exports = {config}".format
  73. # Code to render the root stylesheet.
  74. STYLE = get_template("web/styles/styles.css.jinja2")
  75. # Code that generate the package json file
  76. PACKAGE_JSON = get_template("web/package.json.jinja2")
  77. # Code that generate the pyproject.toml file for custom components.
  78. CUSTOM_COMPONENTS_PYPROJECT_TOML = get_template(
  79. "custom_components/pyproject.toml.jinja2"
  80. )
  81. # Code that generates the README file for custom components.
  82. CUSTOM_COMPONENTS_README = get_template("custom_components/README.md.jinja2")
  83. # Code that generates the source file for custom components.
  84. CUSTOM_COMPONENTS_SOURCE = get_template("custom_components/src.py.jinja2")
  85. # Code that generates the init file for custom components.
  86. CUSTOM_COMPONENTS_INIT_FILE = get_template("custom_components/__init__.py.jinja2")
  87. # Code that generates the demo app main py file for testing custom components.
  88. CUSTOM_COMPONENTS_DEMO_APP = get_template("custom_components/demo_app.py.jinja2")