
* add module prefix to state names * fix state names in test_app * update state names in test_state * fix state names in test_var * fix state name in test_component * fix state names in test_format * fix state names in test_foreach * fix state names in test_cond * fix state names in test_datatable * fix state names in test_colors * fix state names in test_script * fix state names in test_match * fix state name in event1 fixture * fix pyright and darglint * fix state names in state_tree * fix state names in redis only test * fix state names in test_client_storage * fix state name in js template * add `get_state_name` and `get_full_state_name` helpers for `AppHarness` * fix state names in test_dynamic_routes * use new state name helpers in test_client_storage * fix state names in test_event_actions * fix state names in test_event_chain * fix state names in test_upload * fix state name in test_login_flow * fix state names in test_input * fix state names in test_form_submit * ruff * validate state module names * wtf is going on here? * remove comments leftover from refactoring * adjust new test_add_style_embedded_vars * fix state name in state.js * fix integration/test_client_state.py new SessionStorage feature was added with more full state names that need to be formatted in * fix pre-commit issues in test_client_storage.py * adjust test_computed_vars * adjust safe-guards * fix redis tests with new exception state --------- Co-authored-by: Masen Furer <m_github@0x26.net>
121 lines
4.5 KiB
Python
121 lines
4.5 KiB
Python
"""Templates to use in the reflex compiler."""
|
|
|
|
from jinja2 import Environment, FileSystemLoader, Template
|
|
|
|
from reflex import constants
|
|
from reflex.utils.format import format_state_name, json_dumps
|
|
|
|
|
|
class ReflexJinjaEnvironment(Environment):
|
|
"""The template class for jinja environment."""
|
|
|
|
def __init__(self) -> None:
|
|
"""Set default environment."""
|
|
extensions = ["jinja2.ext.debug"]
|
|
super().__init__(
|
|
extensions=extensions,
|
|
trim_blocks=True,
|
|
lstrip_blocks=True,
|
|
)
|
|
self.filters["json_dumps"] = json_dumps
|
|
self.filters["react_setter"] = lambda state: f"set{state.capitalize()}"
|
|
self.filters["var_name"] = format_state_name
|
|
self.loader = FileSystemLoader(constants.Templates.Dirs.JINJA_TEMPLATE)
|
|
self.globals["const"] = {
|
|
"socket": constants.CompileVars.SOCKET,
|
|
"result": constants.CompileVars.RESULT,
|
|
"router": constants.CompileVars.ROUTER,
|
|
"event_endpoint": constants.Endpoint.EVENT.name,
|
|
"events": constants.CompileVars.EVENTS,
|
|
"state": constants.CompileVars.STATE,
|
|
"final": constants.CompileVars.FINAL,
|
|
"processing": constants.CompileVars.PROCESSING,
|
|
"initial_result": {
|
|
constants.CompileVars.STATE: None,
|
|
constants.CompileVars.EVENTS: [],
|
|
constants.CompileVars.FINAL: True,
|
|
constants.CompileVars.PROCESSING: False,
|
|
},
|
|
"color_mode": constants.ColorMode.NAME,
|
|
"resolved_color_mode": constants.ColorMode.RESOLVED_NAME,
|
|
"toggle_color_mode": constants.ColorMode.TOGGLE,
|
|
"set_color_mode": constants.ColorMode.SET,
|
|
"use_color_mode": constants.ColorMode.USE,
|
|
"hydrate": constants.CompileVars.HYDRATE,
|
|
"on_load_internal": constants.CompileVars.ON_LOAD_INTERNAL,
|
|
"update_vars_internal": constants.CompileVars.UPDATE_VARS_INTERNAL,
|
|
"frontend_exception_state": constants.CompileVars.FRONTEND_EXCEPTION_STATE_FULL,
|
|
}
|
|
|
|
|
|
def get_template(name: str) -> Template:
|
|
"""Get render function that work with a template.
|
|
|
|
Args:
|
|
name: The template name. "/" is used as the path separator.
|
|
|
|
Returns:
|
|
A render function.
|
|
"""
|
|
return ReflexJinjaEnvironment().get_template(name=name)
|
|
|
|
|
|
# Template for the Reflex config file.
|
|
RXCONFIG = get_template("app/rxconfig.py.jinja2")
|
|
|
|
# Code to render a NextJS Document root.
|
|
DOCUMENT_ROOT = get_template("web/pages/_document.js.jinja2")
|
|
|
|
# Code to render NextJS App root.
|
|
APP_ROOT = get_template("web/pages/_app.js.jinja2")
|
|
|
|
# Template for the theme file.
|
|
THEME = get_template("web/utils/theme.js.jinja2")
|
|
|
|
# Template for the context file.
|
|
CONTEXT = get_template("web/utils/context.js.jinja2")
|
|
|
|
# Template for Tailwind config.
|
|
TAILWIND_CONFIG = get_template("web/tailwind.config.js.jinja2")
|
|
|
|
# Template to render a component tag.
|
|
COMPONENT = get_template("web/pages/component.js.jinja2")
|
|
|
|
# Code to render a single NextJS page.
|
|
PAGE = get_template("web/pages/index.js.jinja2")
|
|
|
|
# Code to render the custom components page.
|
|
COMPONENTS = get_template("web/pages/custom_component.js.jinja2")
|
|
|
|
# Code to render Component instances as part of StatefulComponent
|
|
STATEFUL_COMPONENT = get_template("web/pages/stateful_component.js.jinja2")
|
|
|
|
# Code to render StatefulComponent to an external file to be shared
|
|
STATEFUL_COMPONENTS = get_template("web/pages/stateful_components.js.jinja2")
|
|
|
|
# Sitemap config file.
|
|
SITEMAP_CONFIG = "module.exports = {config}".format
|
|
|
|
# Code to render the root stylesheet.
|
|
STYLE = get_template("web/styles/styles.css.jinja2")
|
|
|
|
# Code that generate the package json file
|
|
PACKAGE_JSON = get_template("web/package.json.jinja2")
|
|
|
|
# Code that generate the pyproject.toml file for custom components.
|
|
CUSTOM_COMPONENTS_PYPROJECT_TOML = get_template(
|
|
"custom_components/pyproject.toml.jinja2"
|
|
)
|
|
|
|
# Code that generates the README file for custom components.
|
|
CUSTOM_COMPONENTS_README = get_template("custom_components/README.md.jinja2")
|
|
|
|
# Code that generates the source file for custom components.
|
|
CUSTOM_COMPONENTS_SOURCE = get_template("custom_components/src.py.jinja2")
|
|
|
|
# Code that generates the init file for custom components.
|
|
CUSTOM_COMPONENTS_INIT_FILE = get_template("custom_components/__init__.py.jinja2")
|
|
|
|
# Code that generates the demo app main py file for testing custom components.
|
|
CUSTOM_COMPONENTS_DEMO_APP = get_template("custom_components/demo_app.py.jinja2")
|