View Controller
mountaineer.controller.ControllerBase
ControllerBase(
slow_ssr_threshold=0.1, hard_ssr_timeout=10.0
)
Bases: ABC
, Generic[RenderInput]
One Controller should be created for every frontend page in your webapp. Clients can override
this __init__
function so long as they call super().__init__()
at the start of their init
to setup the internal handlers.
PARAMETER | DESCRIPTION |
---|---|
slow_ssr_threshold |
Each python process has a single V8 runtime associated with it, so SSR rendering can become a bottleneck if it requires processing. We log a warning if we detect that an SSR render took longer than this threshold.
TYPE:
|
hard_ssr_timeout |
If the SSR render takes longer than this threshold, we will automatically kill the V8 runtime and return an error to the client. This is useful for avoiding blocking the reset of the server process if the React render logic hangs.
TYPE:
|
url
instance-attribute
url
view_path
instance-attribute
view_path
definition
class-attribute
instance-attribute
definition = None
bundled_scripts
instance-attribute
bundled_scripts = []
initialized
instance-attribute
initialized = True
source_map
instance-attribute
source_map = None
view_base_path
instance-attribute
view_base_path = None
ssr_path
instance-attribute
ssr_path = None
build_metadata
instance-attribute
build_metadata = None
render
abstractmethod
render(*args, **kwargs)
Render provides the raw data payload that will be sent to the frontend on initial render and during any sideeffect update. In most cases, you should return a RenderBase instance. If you have no data to display you can also return None.
This function must be explicitly typehinted with your response type, which allows the AppController to generate the correct TypeScript types for the frontend:
class MyServerData(RenderBase):
pass
class MyController:
def render(self) -> MyServerData:
pass
If you don't intend to sync any data from server->client you can typehint this function with an explicit None return annotation:
class MyController:
def render(self) -> None:
pass
Render functions accept any number of arguments and keyword arguments, following the FastAPI route parameter style. This includes query parameters, path parameters, and request bodies.
class MyController:
url = "/my-url/{path_param}"
def render(
self,
query_param: str,
path_param: int,
dependency: MyDependency = Depends(MyDependency),
) -> MyServerData:
...
RETURNS | DESCRIPTION |
---|---|
RenderBase | None | Coroutine[Any, Any, RenderBase | None]
|
A RenderBase instance or None |
resolve_paths
resolve_paths(view_base=None, force=True)
Typically used internally by the Mountaineer build pipeline. Calling this function
sets the active view_base
of the frontend project, which allows us to resolve the
built javascripts that are required for this controller.
RETURNS | DESCRIPTION |
---|---|
bool
|
Whether we have found all necessary files and fully updated the controller state. |
Layout Controller
mountaineer.controller_layout.LayoutControllerBase
LayoutControllerBase(
slow_ssr_threshold=0.1, hard_ssr_timeout=10.0
)
Bases: ControllerBase
Base class for layouts. Layout controllers are used to generate the HTML that wrap a regular view controller. They support all actions that a regular controller does (@sideeffect and @passthrough).
Their limitations: - They are run in an isolated dependency injection context, they don't share dependency injected values with the given page - The current page Request is not supported within render() - Sideeffect updates to the layout don't affect the page state, and vice-versa - Layout controllers can't be mounted as a URL route
One Controller should be created for every frontend page in your webapp. Clients can override
this __init__
function so long as they call super().__init__()
at the start of their init
to setup the internal handlers.
PARAMETER | DESCRIPTION |
---|---|
slow_ssr_threshold |
Each python process has a single V8 runtime associated with it, so SSR rendering can become a bottleneck if it requires processing. We log a warning if we detect that an SSR render took longer than this threshold.
TYPE:
|
hard_ssr_timeout |
If the SSR render takes longer than this threshold, we will automatically kill the V8 runtime and return an error to the client. This is useful for avoiding blocking the reset of the server process if the React render logic hangs.
TYPE:
|
url
instance-attribute
url
view_path
instance-attribute
view_path
bundled_scripts
instance-attribute
bundled_scripts = []
definition
class-attribute
instance-attribute
definition = None
initialized
instance-attribute
initialized = True
source_map
instance-attribute
source_map = None
view_base_path
instance-attribute
view_base_path = None
ssr_path
instance-attribute
ssr_path = None
build_metadata
instance-attribute
build_metadata = None
render
abstractmethod
render(*args, **kwargs)
Render provides the raw data payload that will be sent to the frontend on initial render and during any sideeffect update. In most cases, you should return a RenderBase instance. If you have no data to display you can also return None.
This function must be explicitly typehinted with your response type, which allows the AppController to generate the correct TypeScript types for the frontend:
class MyServerData(RenderBase):
pass
class MyController:
def render(self) -> MyServerData:
pass
If you don't intend to sync any data from server->client you can typehint this function with an explicit None return annotation:
class MyController:
def render(self) -> None:
pass
Render functions accept any number of arguments and keyword arguments, following the FastAPI route parameter style. This includes query parameters, path parameters, and request bodies.
class MyController:
url = "/my-url/{path_param}"
def render(
self,
query_param: str,
path_param: int,
dependency: MyDependency = Depends(MyDependency),
) -> MyServerData:
...
RETURNS | DESCRIPTION |
---|---|
RenderBase | None | Coroutine[Any, Any, RenderBase | None]
|
A RenderBase instance or None |
resolve_paths
resolve_paths(view_base=None, force=True)
Typically used internally by the Mountaineer build pipeline. Calling this function
sets the active view_base
of the frontend project, which allows us to resolve the
built javascripts that are required for this controller.
RETURNS | DESCRIPTION |
---|---|
bool
|
Whether we have found all necessary files and fully updated the controller state. |