Plugins
WebComPyPlugin lets you package cross-cutting capabilities — debug toolbars, authentication guards, analytics trackers, HTTP/RPC middleware, and conditional external <script> tags — into reusable classes registered via WebComPyAppConfig.plugins. PluginManager discovers each plugin class by module path, runs hooks in deterministic declaration order across app startup and per-request render contexts, and collects script descriptors for HTML output.
When to use#
- Packaging reusable telemetry, auth guards, middleware bundles, or browser scripts across apps — implement a
WebComPyPlugin. - Wiring a single service or override inside one application or test — call
app.provide(...)orDIScope(...)directly (see Dependency Injection Deep Dive). - Registering ad-hoc request interceptors inside a component — call
add_fetch_middleware/add_rpc_middlewaredirectly (see Middleware).
Setup#
Create a WebComPyPlugin subclass (from webcompy.plugin import WebComPyPlugin or from webcompy.app import WebComPyPlugin) and list its "module.path:ClassName" string in WebComPyAppConfig(plugins=[...]):
# my_app/plugins.py
from webcompy.app import WebComPyApp, WebComPyPlugin, RenderContext
from webcompy.di import InjectKey
AnalyticsKey = InjectKey[str]("analytics-id")
class AnalyticsPlugin(WebComPyPlugin):
name = "analytics"
version = "0.1.0"
@staticmethod
def get_providers() -> dict[object, object]:
return {AnalyticsKey: "ua-demo-001"}
def on_app_init(self, app: WebComPyApp) -> None:
if app.router is not None:
app.router.after_route_change.append(lambda path: print("Track view:", path))
# my_app/bootstrap.py
from webcompy.app import WebComPyApp, WebComPyAppConfig
app = WebComPyApp(
root_component=AppShell,
config=WebComPyAppConfig(
plugins=["my_app.plugins:AnalyticsPlugin"],
),
)
Recipes#
1. Bundle middlewares, providers, and router hooks in one plugin#
Override get_fetch_middlewares(), get_rpc_middlewares(), get_providers(), on_app_init(), on_render_context_init(), and on_app_ready() on the same plugin class:
from webcompy.app import WebComPyApp, WebComPyPlugin, RenderContext
from webcompy.ports import FetchNext, FetchRequest
from webcompy.rpc import RpcContext
class AuthIntegrationPlugin(WebComPyPlugin):
name = "auth-integration"
version = "1.0.0"
@staticmethod
def get_fetch_middlewares():
async def add_session_header(request: FetchRequest, next: FetchNext):
request.headers = {**(request.headers or {}), "X-App-Client": "webcompy"}
return await next(request)
return [add_session_header]
@staticmethod
def get_rpc_middlewares():
async def add_rpc_header(ctx: RpcContext, next):
ctx.headers["X-App-Client"] = "webcompy"
return await next(ctx)
return [add_rpc_header]
def on_app_init(self, app: WebComPyApp) -> None:
if app.router is not None:
app.router.before_route_change.append(lambda _from, to: to != "/forbidden")
def on_app_ready(self, ctx: RenderContext) -> None:
# Runs only in the browser inside app.run(), right before first render/hydration
print("Browser app mounted with root scope:", ctx.di_scope)
2. Inject static or runtime-conditional <script> tags with PluginScript#
Return PluginScript descriptors (from webcompy.app import PluginScript) from get_scripts() (or attach them via WebComPyAppConfig.scripts). When condition=None, WebComPy emits an ordinary <script> tag; when condition is a JavaScript expression string, WebComPy emits a small self-contained wrapper script that dynamically loads <script> or executes inline script= only when the expression evaluates truthy at runtime:
from webcompy.app import PluginScript, WebComPyPlugin
class DebugToolbarPlugin(WebComPyPlugin):
name = "debug-toolbar"
version = "0.1.0"
@staticmethod
def get_scripts() -> list[PluginScript]:
return [
PluginScript(
attrs={"type": "text/javascript", "src": "https://cdn.example.com/eruda.min.js"},
condition="new URLSearchParams(location.search).get('debug') === 'True'",
script="eruda.init()",
in_head=False,
)
]
If condition is given without "src" in attrs, the inline script executes directly inside the condition branch without creating an extra <script> element (in_head=True places the tag/wrapper in <head>, in_head=False at the end of <body>).
For work that MUST execute synchronously in <head> before any <link> stylesheets paint (such as custom imperative DOM initialization prior to first paint), set pre_paint=True on PluginScript(...) or app.append_script(..., in_head=True, pre_paint=True):
PluginScript(
attrs={},
script="document.documentElement.dataset.bootMode = 'compact';",
pre_paint=True,
)
Setting pre_paint=True schedules the script after dynamic <style> elements and ahead of <link> elements in <head> without waiting for body scripts or PyScript initialization. For persisted CSS tokens, custom themes, or variant switching without authoring JavaScript, prefer declarative pre_paint_vars() / PrePaintStyle in Styling Deep Dive.
3. Provide fresh per-request values in on_render_context_init()#
Use on_render_context_init(self, ctx) whenever each SSR request (or browser boot) needs an isolated instance or port override — it runs after _register_ports() so plugins can also replace default browser/server ports safely:
import uuid
from webcompy.app import WebComPyPlugin, RenderContext
from webcompy.di import InjectKey
RequestIdKey = InjectKey[str]("request-id")
class RequestTracingPlugin(WebComPyPlugin):
name = "request-tracing"
version = "0.1.0"
def on_render_context_init(self, ctx: RenderContext) -> None:
ctx.di_scope.provide(RequestIdKey, str(uuid.uuid4()))
Behavior notes#
- Startup & per-context execution order:
WebComPyApp.__init__()resolvesconfig.pluginsviaPluginManager.discover(), validates the"module:Class"format andWebComPyPluginsubclass check, and createsapp.router.PluginManager.init_all()instantiates each plugin and invokeson_app_init(app)in declaration order (before the root component mounts).- On every
app.create_render_context()(each SSR request, SSG page, or browserapp.run()),RenderContext.__init__first calls_register_ports(), thenPluginManager.init_render_context(ctx):- Registers
get_providers()intoctx.di_scope. - Calls
on_render_context_init(ctx)on each plugin instance. - Aggregates
get_fetch_middlewares()ontoFETCH_MIDDLEWARE_KEYandget_rpc_middlewares()ontoRPC_MIDDLEWARE_KEYinWebComPyAppConfig.pluginsdeclaration order.
- Registers
- In the browser,
app.run()invokeson_app_ready(ctx)once the browserRenderContextis ready.
- Script emission:
PluginManager.scriptsflattensget_scripts()from all discovered plugins and integrates alongsideWebComPyAppConfig.scriptsintoscripts_headandscripts_bodyduring HTML generation.
Pitfalls#
- Invalid plugin path format: Omitting
:or referencing a symbol that is not a subclass ofWebComPyPluginraisesWebComPyPluginExceptionimmediately at app creation time. - Sharing mutable state on the plugin instance across requests: One plugin instance lives for the lifetime of
WebComPyApp. Storing mutable request state onselfleaks across concurrent SSR requests — put request-scoped values inctx.di_scopeinsideon_render_context_init(ctx)instead. - Assuming
on_app_ready(ctx)runs during SSR/SSG:on_app_readyruns only insideapp.run()in the browser; code needed on both server and browser belongs inget_providers()oron_render_context_init(ctx). - Factory providers re-running per component: Non-class callables inside
get_providers()followDIScopecaching rules — invoked lazily once perRenderContextscope, not perinject()call.
Next steps#
- Understand how
get_providers()andctx.di_scoperesolve and isolate dependencies in Dependency Injection Deep Dive. - Learn middleware ordering rules (
middlewares[0]outermost) in Middleware. - Register global route guards cleanly from
on_app_init(app)in Routing Deep Dive. - Inspect how generated HTML places
scripts_head/scripts_bodyin SSR & SSG. - Full API: App & Components API.