-
Notifications
You must be signed in to change notification settings - Fork 60
/
shinyapp.py
200 lines (162 loc) · 7.69 KB
/
shinyapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
__all__ = ("ShinyApp",)
from typing import Any, List, Optional, Union, Dict, Callable, cast
from htmltools import Tag, TagList, HTMLDocument, HTMLDependency, RenderedHTML
import starlette.routing
import starlette.websockets
from starlette.types import ASGIApp, Message, Receive, Scope, Send
from starlette.requests import Request
from starlette.responses import Response, HTMLResponse, JSONResponse
from .http_staticfiles import StaticFiles
from .shinysession import ShinySession, session_context
from . import reactcore
from .connmanager import (
Connection,
StarletteConnection,
)
from .html_dependencies import jquery_deps, shiny_deps
class ShinyApp:
LIB_PREFIX = "lib/"
def __init__(
self,
ui: Union[Tag, TagList],
server: Callable[[ShinySession], None],
*,
debug: bool = False,
) -> None:
self.ui: RenderedHTML = _render_page(ui, lib_prefix=self.LIB_PREFIX)
self.server: Callable[[ShinySession], None] = server
self._debug: bool = debug
self._sessions: Dict[str, ShinySession] = {}
self._last_session_id: int = 0 # Counter for generating session IDs
self._sessions_needing_flush: Dict[int, ShinySession] = {}
self._registered_dependencies: Dict[str, HTMLDependency] = {}
self._dependency_handler: Any = starlette.routing.Router()
self.starlette_app = starlette.routing.Router(
routes=[
starlette.routing.WebSocketRoute("/websocket/", self._on_connect_cb),
starlette.routing.Route("/", self._on_root_request_cb, methods=["GET"]),
starlette.routing.Route(
"/session/{session_id}/{action}/{subpath:path}",
self._on_session_request_cb,
methods=["GET", "POST"],
),
starlette.routing.Mount("/", app=self._dependency_handler),
]
)
def create_session(self, conn: Connection) -> ShinySession:
self._last_session_id += 1
id = str(self._last_session_id)
session = ShinySession(self, id, conn, debug=self._debug)
self._sessions[id] = session
return session
def remove_session(self, session: Union[ShinySession, str]) -> None:
if isinstance(session, ShinySession):
session = session.id
if self._debug:
print(f"remove_session: {session}")
del self._sessions[session]
def run(self, debug: Optional[bool] = None) -> None:
import uvicorn # type: ignore
if debug is not None:
self._debug = debug
uvicorn.run(cast(Any, self), host="0.0.0.0", port=8000)
# ASGI entrypoint. Handles HTTP, WebSocket, and lifespan.
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
await self.starlette_app(scope, receive, send)
async def call_pyodide(self, scope: Scope, receive: Receive, send: Send) -> None:
# TODO: Pretty sure there are objects that need to be destroy()'d here?
scope = cast(Any, scope).to_py()
# ASGI requires some values to be byte strings, not character strings. Those are
# not that easy to create in JavaScript, so we let the JS side pass us strings
# and we convert them to bytes here.
if "headers" in scope:
# JS doesn't have `bytes` so we pass as strings and convert here
scope["headers"] = [
[value.encode("latin-1") for value in header]
for header in scope["headers"]
]
if "query_string" in scope and scope["query_string"]:
scope["query_string"] = scope["query_string"].encode("latin-1")
if "raw_path" in scope and scope["raw_path"]:
scope["raw_path"] = scope["raw_path"].encode("latin-1")
async def rcv() -> Message:
event = await receive()
return cast(Message, cast(Any, event).to_py())
async def snd(event: Message):
await send(event)
await self(scope, rcv, snd)
async def stop(self) -> None:
# Close all sessions (convert to list to avoid modifying the dict while
# iterating over it, which throws an error).
for session in list(self._sessions.values()):
await session.close()
# ==========================================================================
# Connection callbacks
# ==========================================================================
async def _on_root_request_cb(self, request: Request) -> Response:
"""
Callback passed to the ConnectionManager which is invoked when a HTTP
request for / occurs.
"""
self._ensure_web_dependencies(self.ui["dependencies"])
return HTMLResponse(content=self.ui["html"])
async def _on_connect_cb(self, ws: starlette.websockets.WebSocket) -> None:
"""
Callback which is invoked when a new WebSocket connection is established.
"""
await ws.accept()
conn = StarletteConnection(ws)
session = self.create_session(conn)
await session.run()
async def _on_session_request_cb(self, request: Request) -> ASGIApp:
"""
Callback passed to the ConnectionManager which is invoked when a HTTP
request for /session/* occurs.
"""
session_id: str = request.path_params["session_id"] # type: ignore
action: str = request.path_params["action"] # type: ignore
subpath: str = request.path_params["subpath"] # type: ignore
if session_id in self._sessions:
session: ShinySession = self._sessions[session_id]
with session_context(session):
return await session.handle_request(request, action, subpath)
return JSONResponse({"detail": "Not Found"}, status_code=404)
# ==========================================================================
# Flush
# ==========================================================================
def request_flush(self, session: ShinySession) -> None:
# TODO: Until we have reactive domains, because we can't yet keep track
# of which sessions need a flush.
pass
# self._sessions_needing_flush[session.id] = session
async def flush_pending_sessions(self) -> None:
await reactcore.flush()
# TODO: Until we have reactive domains, flush all sessions (because we
# can't yet keep track of which ones need a flush)
for _, session in self._sessions.items():
await session.flush()
# for id, session in self._sessions_needing_flush.items():
# await session.flush()
# del self._sessions_needing_flush[id]
# ==========================================================================
# HTML Dependency stuff
# ==========================================================================
def _ensure_web_dependencies(self, deps: List[HTMLDependency]) -> None:
for dep in deps:
self.register_web_dependency(dep)
def register_web_dependency(self, dep: HTMLDependency) -> None:
if (
dep.name in self._registered_dependencies
and dep.version >= self._registered_dependencies[dep.name].version
):
return
paths = dep.source_path_map(lib_prefix=self.LIB_PREFIX)
self._dependency_handler.mount(
"/" + paths["href"],
StaticFiles(directory=paths["source"]),
name=dep.name + "-" + str(dep.version),
)
self._registered_dependencies[dep.name] = dep
def _render_page(ui: Union[Tag, TagList], lib_prefix: str) -> RenderedHTML:
doc = HTMLDocument(TagList(jquery_deps(), shiny_deps(), ui))
return doc.render(lib_prefix=lib_prefix)