Spaces:
Sleeping
Sleeping
rmm
commited on
Commit
·
380d10c
1
Parent(s):
b43dfdf
docs: docstrings, typehints for logger
Browse files- call_models/st_logs.py +15 -5
- docs/st_logs.md +1 -1
call_models/st_logs.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import logging
|
2 |
from datetime import datetime
|
3 |
import re
|
@@ -35,6 +36,7 @@ class StreamlitLogHandler(logging.Handler):
|
|
35 |
log_area (streamlit.DeltaGenerator): An empty Streamlit container for log output.
|
36 |
buffer (collections.deque): A deque buffer to store log messages with a maximum length.
|
37 |
_n (int): A counter to keep track of the number of log messages seen.
|
|
|
38 |
Methods:
|
39 |
__init__(container, maxlen=15, debug=False):
|
40 |
Initializes the StreamlitLogHandler with a Streamlit container, buffer length, and debug flag.
|
@@ -70,13 +72,18 @@ class StreamlitLogHandler(logging.Handler):
|
|
70 |
Returns:
|
71 |
str: A string representing the total number of elements seen and the number of elements in the buffer.
|
72 |
"""
|
73 |
-
''' return a string with num elements seen and num elements in buffer '''
|
74 |
if verb:
|
75 |
return f"total: {self._n}|| in buffer:{len(self.buffer)}"
|
76 |
|
77 |
return f"{self._n}||{len(self.buffer)}"
|
78 |
|
79 |
-
def emit(self, record):
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
self._n += 1
|
81 |
msg = f"[{self._n}]" + self.format(record)
|
82 |
self.buffer.append(msg)
|
@@ -119,11 +126,13 @@ def setup_logging(level:int=logging.INFO, buffer_len:int=15) -> StreamlitLogHand
|
|
119 |
# st.session_state['handler'] = handler
|
120 |
return handler
|
121 |
|
122 |
-
def parse_log_buffer(log_contents: deque) ->
|
123 |
"""
|
124 |
-
Convert log buffer to a list of dictionaries.
|
|
|
125 |
Args:
|
126 |
log_contents (deque): A deque containing log lines as strings.
|
|
|
127 |
Returns:
|
128 |
list: A list of dictionaries, each representing a parsed log entry with the following keys:
|
129 |
- 'timestamp' (datetime): The timestamp of the log entry.
|
@@ -164,7 +173,8 @@ def parse_log_buffer(log_contents: deque) -> list:
|
|
164 |
return records
|
165 |
|
166 |
def demo_log_callback() -> None:
|
167 |
-
'''
|
|
|
168 |
logger = logging.getLogger(__name__)
|
169 |
logger.setLevel(logging.DEBUG)
|
170 |
logger.debug("debug message")
|
|
|
1 |
+
from typing import List
|
2 |
import logging
|
3 |
from datetime import datetime
|
4 |
import re
|
|
|
36 |
log_area (streamlit.DeltaGenerator): An empty Streamlit container for log output.
|
37 |
buffer (collections.deque): A deque buffer to store log messages with a maximum length.
|
38 |
_n (int): A counter to keep track of the number of log messages seen.
|
39 |
+
|
40 |
Methods:
|
41 |
__init__(container, maxlen=15, debug=False):
|
42 |
Initializes the StreamlitLogHandler with a Streamlit container, buffer length, and debug flag.
|
|
|
72 |
Returns:
|
73 |
str: A string representing the total number of elements seen and the number of elements in the buffer.
|
74 |
"""
|
|
|
75 |
if verb:
|
76 |
return f"total: {self._n}|| in buffer:{len(self.buffer)}"
|
77 |
|
78 |
return f"{self._n}||{len(self.buffer)}"
|
79 |
|
80 |
+
def emit(self, record) -> None:
|
81 |
+
'''put the record into buffer so it gets displayed
|
82 |
+
|
83 |
+
Args:
|
84 |
+
record (logging.LogRecord): The log record to process and display.
|
85 |
+
|
86 |
+
'''
|
87 |
self._n += 1
|
88 |
msg = f"[{self._n}]" + self.format(record)
|
89 |
self.buffer.append(msg)
|
|
|
126 |
# st.session_state['handler'] = handler
|
127 |
return handler
|
128 |
|
129 |
+
def parse_log_buffer(log_contents: deque) -> List[dict]:
|
130 |
"""
|
131 |
+
Convert log buffer to a list of dictionaries for use with a streamlit datatable.
|
132 |
+
|
133 |
Args:
|
134 |
log_contents (deque): A deque containing log lines as strings.
|
135 |
+
|
136 |
Returns:
|
137 |
list: A list of dictionaries, each representing a parsed log entry with the following keys:
|
138 |
- 'timestamp' (datetime): The timestamp of the log entry.
|
|
|
173 |
return records
|
174 |
|
175 |
def demo_log_callback() -> None:
|
176 |
+
'''basic demo of adding log entries as a callback function'''
|
177 |
+
|
178 |
logger = logging.getLogger(__name__)
|
179 |
logger.setLevel(logging.DEBUG)
|
180 |
logger.debug("debug message")
|
docs/st_logs.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
This module
|
2 |
|
3 |
|
4 |
# Streamlit log handler
|
|
|
1 |
+
This module provides utilities to incorporate a standard python logger within streamlit.
|
2 |
|
3 |
|
4 |
# Streamlit log handler
|