Skip to content

Commit

Permalink
Install pyodide-py for type checking and improve type hints in stlite…
Browse files Browse the repository at this point in the history
…_server/server.py (#1027)

* Install pyodide-py for type checking and improve type hints in stlite_server/server.py

* Fix

* Fix

* Fix
  • Loading branch information
whitphx authored Jul 26, 2024
1 parent 616ced8 commit 093e8cd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
13 changes: 12 additions & 1 deletion packages/kernel/py/stlite-server/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/kernel/py/stlite-server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ streamlit-aggrid = "^1.0.5"
pandas-stubs = "^2.2.2.240603"
matplotlib = "^3.9.0"
ruff = "^0.5.2"
pyodide-py = "^0.26.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand All @@ -33,7 +34,7 @@ module = ["streamlit", "streamlit.*", "tests.*"]
ignore_errors = true

[[tool.mypy.overrides]]
module = ["pyodide", "pyodide.*", "matplotlib", "st_aggrid"]
module = ["st_aggrid"]
ignore_missing_imports = true

[tool.pytest.ini_options]
Expand Down
24 changes: 14 additions & 10 deletions packages/kernel/py/stlite-server/stlite_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import urllib.parse
from typing import Callable, Final, cast

import pyodide
import pyodide.ffi
from streamlit.proto.BackMsg_pb2 import BackMsg
from streamlit.proto.ForwardMsg_pb2 import ForwardMsg
from streamlit.runtime import Runtime, RuntimeConfig, SessionClient
Expand Down Expand Up @@ -102,7 +102,10 @@ def start_websocket(self, path: str, on_message):
raise RuntimeError("Invalid WebSocket endpoint")
self._websocket_handler.open(on_message)

def receive_websocket_from_js(self, payload_from_js: pyodide.ffi.JsProxy):
def receive_websocket_from_js(
self,
payload_from_js: pyodide.ffi.JsBuffer, # Unit8Array value is passed from JS
):
payload = payload_from_js.to_bytes()

if not isinstance(payload, bytes):
Expand All @@ -120,14 +123,16 @@ def receive_http_from_js(
self,
method: str,
path: str,
headers: pyodide.ffi.JsProxy,
body: str | pyodide.ffi.JsProxy,
headers_proxy: pyodide.ffi.JsProxy, # object is passed from JS
body_proxy: str
| pyodide.ffi.JsBuffer, # string or ArrayBuffer value is passed from JS
on_response: Callable[[int, dict, bytes], None],
):
headers = headers.to_py()
headers: dict = headers_proxy.to_py()

if isinstance(body, pyodide.ffi.JsProxy):
body = body.to_bytes()
body: str | bytes = (
body_proxy if isinstance(body_proxy, str) else body_proxy.to_bytes()
)

return self.receive_http(
method=method,
Expand All @@ -153,11 +158,10 @@ def receive_http(
# Find the handler for the path and method.
handler = None
for path_regex, handler_candidate in self._routes:
match = path_regex.match(path)
if match:
if match := path_regex.match(path):
handler = handler_candidate
break
if handler is None:
else:
on_response(404, {}, b"No handler found")
return
method_name = method.lower()
Expand Down

0 comments on commit 093e8cd

Please sign in to comment.