Skip to content

Commit

Permalink
Update stlite-server catching up with the upstream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
whitphx committed May 25, 2024
1 parent 429ebcc commit 8806ea6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 10 additions & 9 deletions packages/kernel/py/stlite-server/stlite_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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,
),
)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/kernel/py/stlite-server/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8806ea6

Please sign in to comment.