Skip to content

Commit cef489b

Browse files
authored
enable sitemap plugin by default (#5558)
* enable sitemap plugin by default * add prefix comment * make it a function on its own * adjust docstring for disable_plugins
1 parent e417a99 commit cef489b

File tree

4 files changed

+72
-17
lines changed

4 files changed

+72
-17
lines changed

reflex/config.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from reflex.environment import env_var as env_var
2626
from reflex.environment import environment as environment
2727
from reflex.plugins import Plugin
28+
from reflex.plugins.sitemap import SitemapPlugin
2829
from reflex.utils import console
2930
from reflex.utils.exceptions import ConfigError
3031

@@ -235,9 +236,17 @@ class BaseConfig:
235236
# List of plugins to use in the app.
236237
plugins: list[Plugin] = dataclasses.field(default_factory=list)
237238

239+
# List of fully qualified import paths of plugins to disable in the app (e.g. reflex.plugins.sitemap.SitemapPlugin).
240+
disable_plugins: list[str] = dataclasses.field(default_factory=list)
241+
238242
_prefixes: ClassVar[list[str]] = ["REFLEX_"]
239243

240244

245+
_PLUGINS_ENABLED_BY_DEFAULT = [
246+
SitemapPlugin,
247+
]
248+
249+
241250
@dataclasses.dataclass(kw_only=True, init=False)
242251
class Config(BaseConfig):
243252
"""The config defines runtime settings for the app.
@@ -254,8 +263,8 @@ class Config(BaseConfig):
254263
)
255264
```
256265
257-
Every config value can be overridden by an environment variable with the same name in uppercase.
258-
For example, `db_url` can be overridden by setting the `DB_URL` environment variable.
266+
Every config value can be overridden by an environment variable with the same name in uppercase and a REFLEX_ prefix.
267+
For example, `db_url` can be overridden by setting the `REFLEX_DB_URL` environment variable.
259268
260269
See the [configuration](https://reflex.dev/docs/getting-started/configuration/) docs for more info.
261270
"""
@@ -285,6 +294,9 @@ def _post_init(self, **kwargs):
285294
if env_loglevel or self.loglevel != LogLevel.DEFAULT:
286295
console.set_log_level(env_loglevel or self.loglevel)
287296

297+
# Add builtin plugins if not disabled.
298+
self._add_builtin_plugins()
299+
288300
# Update the config from environment variables.
289301
env_kwargs = self.update_from_env()
290302
for key, env_value in env_kwargs.items():
@@ -302,6 +314,39 @@ def _post_init(self, **kwargs):
302314
msg = f"{self._prefixes[0]}REDIS_URL is required when using the redis state manager."
303315
raise ConfigError(msg)
304316

317+
def _add_builtin_plugins(self):
318+
"""Add the builtin plugins to the config."""
319+
for plugin in _PLUGINS_ENABLED_BY_DEFAULT:
320+
plugin_name = plugin.__module__ + "." + plugin.__qualname__
321+
if plugin_name not in self.disable_plugins:
322+
if not any(isinstance(p, plugin) for p in self.plugins):
323+
console.warn(
324+
f"`{plugin_name}` plugin is enabled by default, but not explicitly added to the config. "
325+
"If you want to use it, please add it to the `plugins` list in your config inside of `rxconfig.py`. "
326+
f"To disable this plugin, set `disable_plugins` to `{[plugin_name, *self.disable_plugins]!r}`.",
327+
)
328+
self.plugins.append(plugin())
329+
else:
330+
if any(isinstance(p, plugin) for p in self.plugins):
331+
console.warn(
332+
f"`{plugin_name}` is disabled in the config, but it is still present in the `plugins` list. "
333+
"Please remove it from the `plugins` list in your config inside of `rxconfig.py`.",
334+
)
335+
336+
for disabled_plugin in self.disable_plugins:
337+
if not isinstance(disabled_plugin, str):
338+
console.warn(
339+
f"reflex.Config.disable_plugins should only contain strings, but got {disabled_plugin!r}. "
340+
)
341+
if not any(
342+
plugin.__module__ + "." + plugin.__qualname__ == disabled_plugin
343+
for plugin in _PLUGINS_ENABLED_BY_DEFAULT
344+
):
345+
console.warn(
346+
f"`{disabled_plugin}` is disabled in the config, but it is not a built-in plugin. "
347+
"Please remove it from the `disable_plugins` list in your config inside of `rxconfig.py`.",
348+
)
349+
305350
@classmethod
306351
def class_fields(cls) -> set[str]:
307352
"""Get the fields of the config class.

reflex/plugins/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
"""Reflex Plugin System."""
22

3-
from .base import CommonContext as CommonContext
4-
from .base import Plugin as Plugin
5-
from .base import PreCompileContext as PreCompileContext
6-
from .sitemap import Plugin as SitemapPlugin
7-
from .tailwind_v3 import TailwindV3Plugin as TailwindV3Plugin
8-
from .tailwind_v4 import TailwindV4Plugin as TailwindV4Plugin
3+
from .base import CommonContext, Plugin, PreCompileContext
4+
from .sitemap import SitemapPlugin
5+
from .tailwind_v3 import TailwindV3Plugin
6+
from .tailwind_v4 import TailwindV4Plugin
7+
8+
__all__ = [
9+
"CommonContext",
10+
"Plugin",
11+
"PreCompileContext",
12+
"SitemapPlugin",
13+
"TailwindV3Plugin",
14+
"TailwindV4Plugin",
15+
]

reflex/plugins/sitemap.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def sitemap_task(unevaluated_pages: Sequence["UnevaluatedPage"]) -> tuple[str, s
193193
)
194194

195195

196-
class Plugin(PluginBase):
196+
class SitemapPlugin(PluginBase):
197197
"""Sitemap plugin for Reflex."""
198198

199199
def pre_compile(self, **context):
@@ -204,3 +204,6 @@ def pre_compile(self, **context):
204204
"""
205205
unevaluated_pages = context.get("unevaluated_pages", [])
206206
context["add_save_task"](sitemap_task, unevaluated_pages)
207+
208+
209+
Plugin = SitemapPlugin

reflex/utils/console.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def is_debug() -> bool:
7979
return _LOG_LEVEL <= LogLevel.DEBUG
8080

8181

82-
def print(msg: str, dedupe: bool = False, **kwargs):
82+
def print(msg: str, *, dedupe: bool = False, **kwargs):
8383
"""Print a message.
8484
8585
Args:
@@ -128,7 +128,7 @@ def should_use_log_file_console() -> bool:
128128
return environment.REFLEX_ENABLE_FULL_LOGGING.get()
129129

130130

131-
def print_to_log_file(msg: str, dedupe: bool = False, **kwargs):
131+
def print_to_log_file(msg: str, *, dedupe: bool = False, **kwargs):
132132
"""Print a message to the log file.
133133
134134
Args:
@@ -139,7 +139,7 @@ def print_to_log_file(msg: str, dedupe: bool = False, **kwargs):
139139
log_file_console().print(msg, **kwargs)
140140

141141

142-
def debug(msg: str, dedupe: bool = False, **kwargs):
142+
def debug(msg: str, *, dedupe: bool = False, **kwargs):
143143
"""Print a debug message.
144144
145145
Args:
@@ -161,7 +161,7 @@ def debug(msg: str, dedupe: bool = False, **kwargs):
161161
print_to_log_file(f"[purple]Debug: {msg}[/purple]", **kwargs)
162162

163163

164-
def info(msg: str, dedupe: bool = False, **kwargs):
164+
def info(msg: str, *, dedupe: bool = False, **kwargs):
165165
"""Print an info message.
166166
167167
Args:
@@ -179,7 +179,7 @@ def info(msg: str, dedupe: bool = False, **kwargs):
179179
print_to_log_file(f"[cyan]Info: {msg}[/cyan]", **kwargs)
180180

181181

182-
def success(msg: str, dedupe: bool = False, **kwargs):
182+
def success(msg: str, *, dedupe: bool = False, **kwargs):
183183
"""Print a success message.
184184
185185
Args:
@@ -197,7 +197,7 @@ def success(msg: str, dedupe: bool = False, **kwargs):
197197
print_to_log_file(f"[green]Success: {msg}[/green]", **kwargs)
198198

199199

200-
def log(msg: str, dedupe: bool = False, **kwargs):
200+
def log(msg: str, *, dedupe: bool = False, **kwargs):
201201
"""Takes a string and logs it to the console.
202202
203203
Args:
@@ -225,7 +225,7 @@ def rule(title: str, **kwargs):
225225
_console.rule(title, **kwargs)
226226

227227

228-
def warn(msg: str, dedupe: bool = False, **kwargs):
228+
def warn(msg: str, *, dedupe: bool = False, **kwargs):
229229
"""Print a warning message.
230230
231231
Args:
@@ -312,7 +312,7 @@ def deprecate(
312312
_EMITTED_DEPRECATION_WARNINGS.add(dedupe_key)
313313

314314

315-
def error(msg: str, dedupe: bool = False, **kwargs):
315+
def error(msg: str, *, dedupe: bool = False, **kwargs):
316316
"""Print an error message.
317317
318318
Args:

0 commit comments

Comments
 (0)