Skip to content

morph

WSGIPrefixRestorer

Python
WSGIPrefixRestorer(app, prefix)

Tiny ASGI middleware that restores the prefix stripped by Starlette's mount. This ensures the Optuna WSGI app sees the full path it expects.

Source code in src/mujoco_mojo/utils/layers/dojo/routers/morph.py
Python
def __init__(self, app, prefix):
    self.app = app
    self.prefix = prefix

get_optimizer async

Python
get_optimizer(request: Request) -> HTMLResponse

Serves the wrapper frame for the Optuna Dashboard.

Source code in src/mujoco_mojo/utils/layers/dojo/routers/morph.py
Python
@router.get("/", response_class=HTMLResponse)
async def get_optimizer(request: Request) -> HTMLResponse:
    """Serves the wrapper frame for the Optuna Dashboard."""
    return shared.templates.TemplateResponse(
        request=request, name="morph.html", context={"request": request}
    )

mount_optuna_engine

Python
mount_optuna_engine(app: FastAPI, storage_url: str) -> None

Mounts the Optuna Dashboard as a sub-app at /morph.

Source code in src/mujoco_mojo/utils/layers/dojo/routers/morph.py
Python
def mount_optuna_engine(app: FastAPI, storage_url: str) -> None:
    """Mounts the Optuna Dashboard as a sub-app at /morph."""
    import warnings

    try:
        import optuna
        import optuna_dashboard
    except ModuleNotFoundError:
        msg = "The `optuna` and `optuna-dashboard` packages are required to view optimization jobs in the Dojo. Install with `uv add mujoco-mojo[optimize]` or `pip install mujoco-mojo[optimize]`"
        logger.exception(msg)
        raise ModuleNotFoundError(msg)

    from fastapi.middleware.wsgi import WSGIMiddleware
    from fastapi.staticfiles import StaticFiles

    optuna.logging.set_verbosity(optuna.logging.WARNING)

    warnings.filterwarnings("ignore", category=optuna.exceptions.ExperimentalWarning)
    warnings.filterwarnings("ignore", category=UserWarning, module="optuna_dashboard")

    app.mount(
        "/static",
        StaticFiles(directory=Path(optuna_dashboard.__file__).parent / "public"),
        name="optuna_static",
    )

    optuna_wsgi = WSGIMiddleware(optuna_dashboard.wsgi(storage=storage_url))
    app.mount("/api", WSGIPrefixRestorer(optuna_wsgi, "/api"))
    app.mount("/dashboard", WSGIPrefixRestorer(optuna_wsgi, "/dashboard"))