1
0

templates.py 4.5 KB

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