Get the starlette/Fastapi ASGI app for running PyWebIO applications. Use :func:`pywebio.platform.fastapi.webio_routes` if you prefer handling static files yourself. The arguments of ``asgi_app()`` have the same meaning as for :func:`pywebio.platform.fastapi.start_server` :Example:
(applications, cdn=True, reconnect_timeout=0, static_dir=None, debug=False, allowed_origins=None,
check_origin=None)
| 188 | |
| 189 | |
| 190 | def asgi_app(applications, cdn=True, reconnect_timeout=0, static_dir=None, debug=False, allowed_origins=None, |
| 191 | check_origin=None): |
| 192 | """Get the starlette/Fastapi ASGI app for running PyWebIO applications. |
| 193 | |
| 194 | Use :func:`pywebio.platform.fastapi.webio_routes` if you prefer handling static files yourself. |
| 195 | |
| 196 | The arguments of ``asgi_app()`` have the same meaning as for :func:`pywebio.platform.fastapi.start_server` |
| 197 | |
| 198 | :Example: |
| 199 | |
| 200 | To be used with ``FastAPI.mount()`` to include pywebio as a subapp into an existing Starlette/FastAPI application:: |
| 201 | |
| 202 | from fastapi import FastAPI |
| 203 | from pywebio.platform.fastapi import asgi_app |
| 204 | from pywebio.output import put_text |
| 205 | app = FastAPI() |
| 206 | subapp = asgi_app(lambda: put_text("hello from pywebio")) |
| 207 | app.mount("/pywebio", subapp) |
| 208 | |
| 209 | :Returns: Starlette/Fastapi ASGI app |
| 210 | |
| 211 | .. versionadded:: 1.3 |
| 212 | """ |
| 213 | try: |
| 214 | from starlette.staticfiles import StaticFiles |
| 215 | except Exception: |
| 216 | raise RuntimeError(strip_space(""" |
| 217 | Missing dependency package `aiofiles` for static file serving. |
| 218 | You can install it with the following command: |
| 219 | pip install aiofiles |
| 220 | """.strip(), n=8)) from None |
| 221 | debug = Session.debug = os.environ.get('PYWEBIO_DEBUG', debug) |
| 222 | cdn = cdn_validation(cdn, 'warn') |
| 223 | if cdn is False: |
| 224 | cdn = 'pywebio_static' |
| 225 | routes = webio_routes(applications, cdn=cdn, reconnect_timeout=reconnect_timeout, |
| 226 | allowed_origins=allowed_origins, check_origin=check_origin) |
| 227 | if static_dir: |
| 228 | routes.append(Mount('/static', app=StaticFiles(directory=static_dir), name="static")) |
| 229 | routes.append(Mount('/pywebio_static', app=StaticFiles(directory=STATIC_PATH), name="pywebio_static")) |
| 230 | return Starlette(routes=routes, debug=debug) |
no test coverage detected
searching dependent graphs…