templates.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "resolved_color_mode": constants.ColorMode.RESOLVED_NAME,
  36. "toggle_color_mode": constants.ColorMode.TOGGLE,
  37. "set_color_mode": constants.ColorMode.SET,
  38. "use_color_mode": constants.ColorMode.USE,
  39. "hydrate": constants.CompileVars.HYDRATE,
  40. "on_load_internal": constants.CompileVars.ON_LOAD_INTERNAL,
  41. "update_vars_internal": constants.CompileVars.UPDATE_VARS_INTERNAL,
  42. }
  43. def get_template(name: str) -> Template:
  44. """Get render function that work with a template.
  45. Args:
  46. name: The template name. "/" is used as the path separator.
  47. Returns:
  48. A render function.
  49. """
  50. return ReflexJinjaEnvironment().get_template(name=name)
  51. # Template for the Reflex config file.
  52. RXCONFIG = get_template("app/rxconfig.py.jinja2")
  53. # Code to render a NextJS Document root.
  54. DOCUMENT_ROOT = get_template("web/pages/_document.js.jinja2")
  55. # Code to render NextJS App root.
  56. APP_ROOT = get_template("web/pages/_app.js.jinja2")
  57. # Template for the theme file.
  58. THEME = get_template("web/utils/theme.js.jinja2")
  59. # Template for the context file.
  60. CONTEXT = get_template("web/utils/context.js.jinja2")
  61. # Template for Tailwind config.
  62. TAILWIND_CONFIG = get_template("web/tailwind.config.js.jinja2")
  63. # Template to render a component tag.
  64. COMPONENT = get_template("web/pages/component.js.jinja2")
  65. # Code to render a single NextJS page.
  66. PAGE = get_template("web/pages/index.js.jinja2")
  67. # Code to render the custom components page.
  68. COMPONENTS = get_template("web/pages/custom_component.js.jinja2")
  69. # Code to render Component instances as part of StatefulComponent
  70. STATEFUL_COMPONENT = get_template("web/pages/stateful_component.js.jinja2")
  71. # Code to render StatefulComponent to an external file to be shared
  72. STATEFUL_COMPONENTS = get_template("web/pages/stateful_components.js.jinja2")
  73. # Sitemap config file.
  74. SITEMAP_CONFIG = "module.exports = {config}".format
  75. # Code to render the root stylesheet.
  76. STYLE = get_template("web/styles/styles.css.jinja2")
  77. # Code that generate the package json file
  78. PACKAGE_JSON = get_template("web/package.json.jinja2")
  79. # Code that generate the pyproject.toml file for custom components.
  80. CUSTOM_COMPONENTS_PYPROJECT_TOML = get_template(
  81. "custom_components/pyproject.toml.jinja2"
  82. )
  83. # Code that generates the README file for custom components.
  84. CUSTOM_COMPONENTS_README = get_template("custom_components/README.md.jinja2")
  85. # Code that generates the source file for custom components.
  86. CUSTOM_COMPONENTS_SOURCE = get_template("custom_components/src.py.jinja2")
  87. # Code that generates the init file for custom components.
  88. CUSTOM_COMPONENTS_INIT_FILE = get_template("custom_components/__init__.py.jinja2")
  89. # Code that generates the demo app main py file for testing custom components.
  90. CUSTOM_COMPONENTS_DEMO_APP = get_template("custom_components/demo_app.py.jinja2")