Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading