MCPcopy Index your code
hub / github.com/pywebio/PyWebIO / asgi_app

Function asgi_app

pywebio/platform/fastapi.py:190–230  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

188
189
190def 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)

Callers 1

start_serverFunction · 0.85

Calls 5

strip_spaceFunction · 0.85
cdn_validationFunction · 0.85
webio_routesFunction · 0.85
appendMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…