-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathstreamlit_utils.py
83 lines (64 loc) · 2.6 KB
/
streamlit_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Streamlit utils."""
import logging
import streamlit as st
class StreamlitLogger:
"""Pickup logger and write to Streamlit front end."""
def __init__(self, placeholder, logger_name=None, accumulate=True, persist=True):
"""
Pickup logger and write to Streamlit front end.
Parameters
----------
placeholder: streamlit.empty
Streamlit placeholder object on which to write logs.
logger_name: str, optional
Module name of logger to pick up. Leave to None to pick up root logger.
accumulate: boolean, optional
Whether to accumulate log messages or to overwrite with each new
message, keeping only the last line. (default: True)
persist: boolean, optional
Wheter to keep the log when finished, or empty the placeholder element.
"""
self.placeholder = placeholder
self.persist = persist
self.logging_stream = _StreamlitLoggingStream(placeholder, accumulate)
self.handler = logging.StreamHandler(self.logging_stream)
self.logger = logging.getLogger(logger_name)
def __enter__(self):
self.logger.addHandler(self.handler)
def __exit__(self, exc_type, exc_val, exc_tb):
self.logger.removeHandler(self.handler)
if not self.persist:
self.placeholder.empty()
class _StreamlitLoggingStream:
"""Logging stream that writes logs to Streamlit front end."""
def __init__(self, placeholder, accumulate=True):
"""
Logging stream that writes logs to Streamlit front end.
Parameters
----------
placeholder: streamlit.empty
Streamlit placeholder object on which to write logs
accumulate: boolean, optional
Whether to accumulate log messages or to overwrite with each new
message, keeping only the last line (default: True)
"""
self.placeholder = placeholder
self.accumulate = accumulate
self.message_list = []
def write(self, message):
if self.accumulate:
self.message_list.append(message)
else:
self.message_list = [message]
self.placeholder.markdown("```\n" + "".join(self.message_list) + "\n```")
def hide_streamlit_menu():
hide_menu_style = """
<style>
#MainMenu {visibility: hidden;}
</style>
"""
st.markdown(hide_menu_style, unsafe_allow_html=True)
@st.cache
def save_dataframe(df):
"""Save dataframe to file object, with streamlit cache."""
return df.to_csv().encode('utf-8')