streamlit-remote 0.7.1__tar.gz → 0.7.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: streamlit-remote
3
- Version: 0.7.1
3
+ Version: 0.7.2
4
4
  Summary: Run Streamlit apps with optional remote HTTPS access.
5
5
  Author: Yuichiro Tachibana (Tsuchiya)
6
6
  Author-email: Yuichiro Tachibana (Tsuchiya) <t.yic.yt@gmail.com>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "streamlit-remote"
3
- version = "0.7.1"
3
+ version = "0.7.2"
4
4
  description = "Run Streamlit apps with optional remote HTTPS access."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -31,7 +31,7 @@ from streamlit_remote.providers.ngrok import (
31
31
  planned_managed_oauth_policy,
32
32
  prepare_managed_oauth_policy,
33
33
  )
34
- from streamlit_remote.runtime_display import make_runtime_display
34
+ from streamlit_remote.runtime_display import STREAMLIT_SOURCE, make_runtime_display
35
35
  from streamlit_remote.server import LocalServerConfig, is_port_available, wait_until_listening
36
36
 
37
37
 
@@ -421,7 +421,16 @@ def run(namespace: argparse.Namespace) -> int:
421
421
 
422
422
  def start_streamlit_process() -> ManagedProcess:
423
423
  display.set_status("Streamlit", "starting")
424
- return start_logged_process(streamlit_command, "streamlit", write_log=display.log)
424
+ streamlit_env: dict[str, str] = {}
425
+ streamlit_columns = display.streamlit_subprocess_columns()
426
+ if streamlit_columns is not None:
427
+ streamlit_env["COLUMNS"] = str(streamlit_columns)
428
+ return start_logged_process(
429
+ streamlit_command,
430
+ STREAMLIT_SOURCE,
431
+ write_log=display.log,
432
+ env=streamlit_env or None,
433
+ )
425
434
 
426
435
  def restart_streamlit_process(current_handle: ManagedProcess) -> ManagedProcess:
427
436
  display.set_status("Streamlit", "restarting")
@@ -3,9 +3,10 @@ from __future__ import annotations
3
3
  import subprocess
4
4
  import threading
5
5
  import time
6
- from collections.abc import Callable, Sequence
6
+ from collections.abc import Callable, Mapping, Sequence
7
7
  from contextlib import suppress
8
8
  from dataclasses import dataclass
9
+ from os import environ
9
10
 
10
11
  LineHandler = Callable[[str], None]
11
12
  LinePredicate = Callable[[str], bool]
@@ -25,7 +26,13 @@ def start_logged_process(
25
26
  on_line: LineHandler | None = None,
26
27
  should_print_line: LinePredicate | None = None,
27
28
  write_log: LogWriter | None = None,
29
+ env: Mapping[str, str] | None = None,
28
30
  ) -> ManagedProcess:
31
+ process_env = None
32
+ if env is not None:
33
+ process_env = environ.copy()
34
+ process_env.update(env)
35
+
29
36
  process = subprocess.Popen(
30
37
  list(command),
31
38
  stdout=subprocess.PIPE,
@@ -34,6 +41,7 @@ def start_logged_process(
34
41
  encoding="utf-8",
35
42
  errors="replace",
36
43
  bufsize=1,
44
+ env=process_env,
37
45
  )
38
46
 
39
47
  def pump_output() -> None:
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import re
3
4
  import sys
4
5
  import threading
5
6
  from collections import deque
@@ -12,6 +13,9 @@ from rich.panel import Panel
12
13
  from rich.table import Table
13
14
  from rich.text import Text
14
15
 
16
+ _ANSI_ESCAPE_RE = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]")
17
+ STREAMLIT_SOURCE = "streamlit"
18
+
15
19
 
16
20
  class RuntimeDisplay(Protocol):
17
21
  def start(self) -> None: ...
@@ -34,6 +38,8 @@ class RuntimeDisplay(Protocol):
34
38
 
35
39
  def set_shortcuts_visible(self, visible: bool) -> None: ...
36
40
 
41
+ def streamlit_subprocess_columns(self) -> int | None: ...
42
+
37
43
  def info(self, message: str) -> None: ...
38
44
 
39
45
  def error(self, message: str) -> None: ...
@@ -80,6 +86,9 @@ class PlainRuntimeDisplay(RuntimeDisplay):
80
86
  self.info("Runtime shortcuts:")
81
87
  self.info(" r: restart Streamlit while keeping the tunnel running")
82
88
 
89
+ def streamlit_subprocess_columns(self) -> int | None:
90
+ return None
91
+
83
92
  def info(self, message: str) -> None:
84
93
  print(message, file=self._output, flush=True)
85
94
 
@@ -97,6 +106,7 @@ class RichRuntimeDisplay(RuntimeDisplay):
97
106
  _PANEL_HORIZONTAL_OVERHEAD = 4
98
107
  _LOG_SOURCE_WIDTH = 11
99
108
  _LOG_SOURCE_SEPARATOR_WIDTH = 1
109
+ _RICH_TRACEBACK_CHARS = frozenset("╭╮╰╯│─")
100
110
 
101
111
  def __init__(
102
112
  self,
@@ -165,6 +175,11 @@ class RichRuntimeDisplay(RuntimeDisplay):
165
175
  self._shortcuts_visible = visible
166
176
  self._refresh()
167
177
 
178
+ def streamlit_subprocess_columns(self) -> int | None:
179
+ # Streamlit formats Rich tracebacks before we can render them without a
180
+ # source prefix, so give it the prefix-less log panel width.
181
+ return self._log_panel_content_width()
182
+
168
183
  def info(self, message: str) -> None:
169
184
  self.log("st-remote", message)
170
185
 
@@ -250,9 +265,13 @@ class RichRuntimeDisplay(RuntimeDisplay):
250
265
 
251
266
  lines = Text()
252
267
  visible_logs = self._visible_logs(panel_height=panel_height)
268
+ raw_traceback_indexes = self._raw_streamlit_traceback_indexes(visible_logs)
253
269
  for index, (source, line) in enumerate(visible_logs):
254
- lines.append(f"{source:>11} ", style=self._source_style(source))
255
- lines.append(self._format_log_line(line))
270
+ if index in raw_traceback_indexes:
271
+ lines.append(self._format_raw_streamlit_traceback_line(line))
272
+ else:
273
+ lines.append(f"{source:>11} ", style=self._source_style(source))
274
+ lines.append(self._format_log_line(line, width=self._log_line_width()))
256
275
  if index < len(visible_logs) - 1:
257
276
  lines.append("\n")
258
277
  return Panel(lines, title="Logs", height=panel_height)
@@ -260,24 +279,142 @@ class RichRuntimeDisplay(RuntimeDisplay):
260
279
  def _visible_logs(self, *, panel_height: int | None = None) -> list[tuple[str, str]]:
261
280
  log_panel_height = panel_height or self._layout_heights()[1]
262
281
  available_rows = max(1, log_panel_height - self._PANEL_FRAME_ROWS)
263
- return list(self._logs)[-available_rows:]
282
+ logs = self._move_interleaved_logs_after_tracebacks(list(self._logs))
283
+ return self._select_visible_logs(logs, available_rows=available_rows)
264
284
 
265
- def _format_log_line(self, line: str) -> str:
285
+ def _select_visible_logs(
286
+ self,
287
+ logs: list[tuple[str, str]],
288
+ *,
289
+ available_rows: int,
290
+ ) -> list[tuple[str, str]]:
291
+ if len(logs) <= available_rows:
292
+ return logs
293
+
294
+ traceback_spans = self._streamlit_traceback_spans(logs)
295
+ traceback_span = traceback_spans[-1] if traceback_spans else None
296
+ if traceback_span is None:
297
+ return logs[-available_rows:]
298
+
299
+ start, end = traceback_span
300
+ if any(source == STREAMLIT_SOURCE for source, _ in logs[end:]):
301
+ return logs[-available_rows:]
302
+
303
+ traceback_logs = logs[start:end]
304
+ if len(traceback_logs) >= available_rows:
305
+ return traceback_logs[-available_rows:]
306
+
307
+ following_rows = available_rows - len(traceback_logs)
308
+ following_logs = logs[end:][-following_rows:] if following_rows else []
309
+ selected_logs = [*traceback_logs, *following_logs]
310
+
311
+ preceding_rows = available_rows - len(selected_logs)
312
+ if preceding_rows:
313
+ selected_logs = [*logs[:start][-preceding_rows:], *selected_logs]
314
+ return selected_logs
315
+
316
+ def _move_interleaved_logs_after_tracebacks(
317
+ self,
318
+ logs: list[tuple[str, str]],
319
+ ) -> list[tuple[str, str]]:
320
+ arranged_logs: list[tuple[str, str]] = []
321
+ deferred_logs: list[tuple[str, str]] = []
322
+ in_streamlit_traceback = False
323
+
324
+ for source, line in logs:
325
+ if self._is_streamlit_traceback_marker(source, line):
326
+ in_streamlit_traceback = True
327
+ arranged_logs.append((source, line))
328
+ elif in_streamlit_traceback and source != STREAMLIT_SOURCE:
329
+ deferred_logs.append((source, line))
330
+ else:
331
+ arranged_logs.append((source, line))
332
+
333
+ arranged_logs.extend(deferred_logs)
334
+ return arranged_logs
335
+
336
+ def _raw_streamlit_traceback_indexes(self, logs: list[tuple[str, str]]) -> set[int]:
337
+ raw_indexes: set[int] = set()
338
+ for start, end in self._streamlit_traceback_spans(logs):
339
+ raw_indexes.update(range(start, end))
340
+ return raw_indexes
341
+
342
+ def _streamlit_traceback_spans(self, logs: list[tuple[str, str]]) -> list[tuple[int, int]]:
343
+ marker_indexes = [
344
+ index
345
+ for index, (source, line) in enumerate(logs)
346
+ if self._is_streamlit_traceback_marker(source, line)
347
+ ]
348
+ spans: list[tuple[int, int]] = []
349
+ for marker_index in marker_indexes:
350
+ start = marker_index
351
+ while start > 0 and logs[start - 1][0] == STREAMLIT_SOURCE:
352
+ start -= 1
353
+
354
+ end = marker_index + 1
355
+ while end < len(logs) and logs[end][0] == STREAMLIT_SOURCE:
356
+ end += 1
357
+
358
+ if spans and start <= spans[-1][1]:
359
+ spans[-1] = (spans[-1][0], max(spans[-1][1], end))
360
+ else:
361
+ spans.append((start, end))
362
+ return spans
363
+
364
+ def _is_streamlit_traceback_marker(self, source: str, line: str) -> bool:
365
+ if source != STREAMLIT_SOURCE:
366
+ return False
367
+ return any(char in _ANSI_ESCAPE_RE.sub("", line) for char in self._RICH_TRACEBACK_CHARS)
368
+
369
+ def _format_raw_streamlit_traceback_line(self, line: str) -> Text:
370
+ text = Text.from_ansi(line)
371
+ plain = text.plain
372
+ start = 0
373
+ end = len(plain)
374
+
375
+ leading_border_match = re.match(r"^\s*│ ?", plain)
376
+ if leading_border_match is not None:
377
+ start = leading_border_match.end()
378
+
379
+ trailing_border_match = re.search(r"\s+│\s*$", plain)
380
+ if trailing_border_match is not None and trailing_border_match.start() >= start:
381
+ end = trailing_border_match.start()
382
+
383
+ end = min(end, len(plain[:end].rstrip()))
384
+ formatted_text = text[start:end]
385
+ return self._format_raw_streamlit_traceback_text(formatted_text)
386
+
387
+ def _format_raw_streamlit_traceback_text(self, text: Text) -> Text:
388
+ width = self._log_panel_content_width()
389
+ formatted_text = text.copy()
390
+ if len(formatted_text.plain) > width:
391
+ if width <= 3:
392
+ formatted_text = Text("." * width)
393
+ else:
394
+ formatted_text = formatted_text[: width - 3]
395
+ formatted_text.append("...")
396
+ formatted_text.pad_right(width - len(formatted_text.plain))
397
+ return formatted_text
398
+
399
+ def _format_log_line(self, line: str, *, width: int) -> str:
266
400
  line = line.replace("\n", r"\n").replace("\r", r"\r")
267
- line_width = self._log_line_width()
268
- if len(line) <= line_width:
401
+ if len(line) <= width:
269
402
  return line
270
- if line_width <= 3:
271
- return "." * line_width
272
- return f"{line[: line_width - 3]}..."
403
+ if width <= 3:
404
+ return "." * width
405
+ return f"{line[: width - 3]}..."
273
406
 
274
407
  def _log_line_width(self) -> int:
275
- content_width = self._console.size.width - self._PANEL_HORIZONTAL_OVERHEAD
276
408
  return max(
277
409
  1,
278
- content_width - self._LOG_SOURCE_WIDTH - self._LOG_SOURCE_SEPARATOR_WIDTH,
410
+ self._log_panel_content_width()
411
+ - self._LOG_SOURCE_WIDTH
412
+ - self._LOG_SOURCE_SEPARATOR_WIDTH,
279
413
  )
280
414
 
415
+ def _log_panel_content_width(self) -> int:
416
+ return max(1, self._console.size.width - self._PANEL_HORIZONTAL_OVERHEAD)
417
+
281
418
  def _render_shortcuts_panel(self, *, height: int | None = None) -> Panel:
282
419
  if not self._shortcuts_visible:
283
420
  return Panel(
@@ -296,7 +433,7 @@ class RichRuntimeDisplay(RuntimeDisplay):
296
433
  return Panel(shortcuts, title="Shortcuts", border_style="green", height=height)
297
434
 
298
435
  def _source_style(self, source: str) -> str:
299
- if source == "streamlit":
436
+ if source == STREAMLIT_SOURCE:
300
437
  return "blue"
301
438
  if source in {"cloudflared", "ngrok"}:
302
439
  return "magenta"
@@ -427,6 +564,10 @@ class SwitchableRuntimeDisplay(RuntimeDisplay):
427
564
  self._shortcuts_visible = visible
428
565
  self._active_display.set_shortcuts_visible(visible)
429
566
 
567
+ def streamlit_subprocess_columns(self) -> int | None:
568
+ with self._lock:
569
+ return self._active_display.streamlit_subprocess_columns()
570
+
430
571
  def info(self, message: str) -> None:
431
572
  with self._lock:
432
573
  self._logs.append(("st-remote", message))