Lazy loading
wybthon.lazy¶
lazy
¶
Lazy-loaded components integrated with Loading boundaries.
lazy wraps a loader callback (sync or async) that
produces a component. The component loads on first mount, backed by an
async create_memo: while the load is in
flight, the nearest Loading boundary shows its
fallback, and a load failure raises into the nearest
Errored boundary. This matches SolidJS's
lazy(() => import(...)) semantics, adapted to Python's import system.
The loader may return:
- a component callable directly,
- an imported module (an export is picked by convention:
Page, thendefault, then the first callable), or - a module path string, or a
(module_path, attr)tuple, imported viaimportlib.
Async loaders can await arbitrary work first (for example
micropip.install(...) in Pyodide) before returning any of the above.
Example
About = lazy(lambda: ("app.about.page", "Page"))
async def load_chart():
import micropip
await micropip.install("app-charts")
import app_charts
return app_charts.Chart
Chart = lazy(load_chart)
Chart.preload() # warm the cache on hover/intent
html(t"<main>{Loading(About(), fallback=html(t'<p>Loading...</p>'))}</main>")
See Also
Classes:
| Name | Description |
|---|---|
LazyModule |
A cached import that starts on demand; returned by |
Functions:
| Name | Description |
|---|---|
lazy |
Create a lazily-loaded component from a loader callback. |
lazy_module |
A cached, on-demand import of a module that isn't a component. |
LazyComponent
¶
LazyModule
¶
A cached import that starts on demand; returned by lazy_module.
Call it (or preload) to start the
import; both return the same awaitable. peek
returns the module once it has loaded, else None.
Methods:
| Name | Description |
|---|---|
preload |
Start the import now; the same awaitable as calling the loader. |
peek |
The loaded module, or |
lazy
¶
lazy(loader: Callable[[], Any], *, chunk: str | None = None) -> LazyComponent
Create a lazily-loaded component from a loader callback.
The loader runs once, on the first mount (or on .preload()); the
resolved component is cached for every later mount. While loading,
the nearest Loading boundary shows its
fallback. A loader error raises into the nearest
Errored boundary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
Callable[[], Any]
|
Zero-arg callable, sync or async, returning a component
callable, a module, a module-path string, or a
|
required |
chunk
|
str | None
|
Optional named build chunk to fetch before invoking the loader. |
None
|
Returns:
| Type | Description |
|---|---|
LazyComponent
|
A component with a |
LazyComponent
|
early. |
lazy_module
¶
lazy_module(loader: Callable[[], Any], *, chunk: str | None = None) -> LazyModule
A cached, on-demand import of a module that isn't a component.
The counterpart of Solid 2.0's lazyModule; lazy
is the component-shaped sibling. The loader returns a module, a
module path, or an awaitable of either, and runs once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
Callable[[], Any]
|
Zero-arg callable, sync or async. |
required |
chunk
|
str | None
|
Optional named build chunk to fetch before invoking the loader. |
None
|
What's in this module¶
lazy defers loading a component until it first mounts, and
lazy_module defers any other import until it's first needed.
A lazy component's load is backed by an async create_memo, so
it suspends into the nearest Loading while in
flight and raises into the nearest Errored on
failure, matching SolidJS's lazy(() => import(...)) adapted to
Python's import system.
| Name | Description |
|---|---|
lazy |
lazy(loader, *, chunk=None); the loader is a zero-arg callable, sync or async, returning a component, a module, a module-path string, or a (module_path, attr) tuple. chunk names a build chunk to fetch first. |
LazyComponent |
What lazy returns: a Component with .preload() to start the load early and .retry() to retry a failed load. |
lazy_module |
lazy_module(loader, *, chunk=None): a cached, on-demand import of a module that isn't a component, the counterpart of Solid 2.0's lazyModule. |
LazyModule |
What lazy_module returns: call it (or .preload()) for an awaitable of the module; .peek() returns the module once it has loaded, else None. |
from wybthon import Loading, component, html, lazy
from wybthon.router import Route, create_router
About = lazy(lambda: ("app.about.page", "Page")) # importlib, attribute "Page"
async def load_chart():
import micropip
await micropip.install("app-charts")
import app_charts
return app_charts.Chart
Chart = lazy(load_chart)
router = create_router([Route("/about", About), Route("/chart", Chart)])
@component
def App():
return Loading(
html(t'<nav><a href="/chart">Chart</a></nav>{router()}'),
fallback=html(t"<p>Loading...</p>"),
)
- When the loader returns a module, the export is picked by convention:
Page, thendefault, then the first callable. - The loader runs at most once; the resolved component is cached across unmounts.
- Props and children passed to the lazy component are forwarded to the
loaded component unchanged. As a route's component, it gets only the
route props (
params,search,data,children) that the loaded component declares as parameters. - A lazy route component is preloaded when the pointer rests on (or
focus reaches) a link to its route, so
<a href="/chart">above starts the import before the click. Call.preload()yourself for any other intent.
lazy_module loads code that isn't a component: a route table, a
parser, a charting library's helpers. The loader runs once, on the first
call or .preload(), and every caller awaits the same import.
from wybthon import lazy_module
admin = lazy_module(lambda: "app.admin.routes", chunk="admin")
async def open_admin():
routes = await admin() # imports on first use, then cached
routes.register()
admin.peek() # the module, or None until it has loaded
See also¶
- Loading and Error boundary
- Router: pair with
Routefor code-split pages, whichpreload_routeand link hovers load early - Concepts: Async and loading
- Guides: Performance