diff --git a/packages/kernel/py/stlite-server/stlite_server/bootstrap.py b/packages/kernel/py/stlite-server/stlite_server/bootstrap.py index 0d68ea7b7..a80865824 100644 --- a/packages/kernel/py/stlite-server/stlite_server/bootstrap.py +++ b/packages/kernel/py/stlite-server/stlite_server/bootstrap.py @@ -38,7 +38,7 @@ def _fix_sys_path(main_script_path: str) -> None: sys.path.insert(0, os.path.dirname(main_script_path)) -def _fix_sys_argv(main_script_path: str, args: List[str]) -> None: +def _fix_sys_argv(main_script_path: str, args: list[str]) -> None: """sys.argv needs to exclude streamlit arguments and parameters and be set to what a user's script may expect. """ diff --git a/packages/kernel/py/stlite-server/stlite_server/media_file_handler.py b/packages/kernel/py/stlite-server/stlite_server/media_file_handler.py index b2a9c897e..b79e8f721 100644 --- a/packages/kernel/py/stlite-server/stlite_server/media_file_handler.py +++ b/packages/kernel/py/stlite-server/stlite_server/media_file_handler.py @@ -66,9 +66,11 @@ def get(self, request: Request, path: str) -> Response: # type: ignore[override # Check that the value can be encoded in latin1. Latin1 is # the default encoding for headers. filename.encode("latin1") - file_expr = 'filename="{}"'.format(filename) + file_expr = f'filename="{filename}"' except UnicodeEncodeError: - file_expr = "filename*=UTF-8''{}".format(quote(filename)) + # RFC5987 syntax. + # See: https://datatracker.ietf.org/doc/html/rfc5987 + file_expr = f"filename*=utf-8''{quote(filename)}" headers["Content-Disposition"] = "attachment; " + file_expr diff --git a/packages/kernel/py/stlite-server/stlite_server/server.py b/packages/kernel/py/stlite-server/stlite_server/server.py index 3d715e740..05142b7bc 100644 --- a/packages/kernel/py/stlite-server/stlite_server/server.py +++ b/packages/kernel/py/stlite-server/stlite_server/server.py @@ -22,7 +22,7 @@ from .server_util import make_url_path_regex from .upload_file_request_handler import UploadFileRequestHandler -LOGGER = logging.getLogger(__name__) +_LOGGER: Final = logging.getLogger(__name__) # These route definitions are copied from the original impl at https://github.com/streamlit/streamlit/blob/1.27.0/lib/streamlit/web/server/server.py#L83-L92 # noqa: E501 MEDIA_ENDPOINT: Final = "/media" @@ -34,7 +34,7 @@ class Server: _routes: list[tuple[re.Pattern, RequestHandler]] = [] - def __init__(self, main_script_path: str, command_line: str | None) -> None: + def __init__(self, main_script_path: str) -> None: self._main_script_path = main_script_path self._media_file_storage = MemoryMediaFileStorage(MEDIA_ENDPOINT) @@ -44,10 +44,11 @@ def __init__(self, main_script_path: str, command_line: str | None) -> None: self._runtime = Runtime( RuntimeConfig( script_path=main_script_path, - command_line=command_line, + command_line=None, media_file_storage=self._media_file_storage, uploaded_file_manager=uploaded_file_mgr, cache_storage_manager=MemoryCacheStorageManager(), + is_hello=False, ), ) @@ -59,9 +60,9 @@ async def start(self) -> None: When this returns, Streamlit is ready to accept new sessions. """ - LOGGER.debug("Starting server...") + _LOGGER.debug("Starting server...") - # In stlite, impl, we deal with WebSocket separately. + # In stlite, we deal with WebSocket separately. self._websocket_handler = WebSocketHandler(self._runtime) # Based on the original impl at https://github.com/streamlit/streamlit/blob/1.18.1/lib/streamlit/web/server/server.py#L221 # noqa: E501 @@ -105,7 +106,7 @@ def receive_websocket_from_js(self, payload_from_js: pyodide.ffi.JsProxy): payload = payload_from_js.to_bytes() if not isinstance(payload, bytes): - LOGGER.warning( + _LOGGER.warning( "The WebSocket payload is not of type bytes, but %s", type(payload) ) return @@ -144,7 +145,7 @@ def receive_http( body: str | bytes, on_response: Callable[[int, dict, bytes], None], ): - LOGGER.debug("HTTP request (%s %s %s %s)", method, path, headers, body) + _LOGGER.debug("HTTP request (%s %s %s %s)", method, path, headers, body) url_parse_result = urllib.parse.urlparse(path) path = url_parse_result.path @@ -270,10 +271,10 @@ def on_message(self, payload: str | bytes) -> None: msg = BackMsg() msg.ParseFromString(payload) - LOGGER.debug("Received the following back message:\n%s", msg) + _LOGGER.debug("Received the following back message:\n%s", msg) except Exception as ex: - LOGGER.error(ex) + _LOGGER.error(ex) self._runtime.handle_backmsg_deserialization_exception(self._session_id, ex) return diff --git a/packages/kernel/py/stlite-server/tests/test_server.py b/packages/kernel/py/stlite-server/tests/test_server.py index 2055efe6f..cff5d4973 100644 --- a/packages/kernel/py/stlite-server/tests/test_server.py +++ b/packages/kernel/py/stlite-server/tests/test_server.py @@ -38,7 +38,7 @@ def setup_server(): async def init_server(): """Mimic streamlit.web.bootstrap.run()""" - server = Server(filename, None) + server = Server(filename) await server.start() data_from_thread["server"] = server event.set()