-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
60 lines (47 loc) · 1.29 KB
/
app.py
File metadata and controls
60 lines (47 loc) · 1.29 KB
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
import streamlit as st
from streamlit.report_thread import REPORT_CONTEXT_ATTR_NAME
from threading import current_thread
from contextlib import contextmanager
from io import StringIO
import sys
import logging
import time
@contextmanager
def st_redirect(src, dst):
placeholder = st.empty()
output_func = getattr(placeholder, dst)
with StringIO() as buffer:
old_write = src.write
def new_write(b):
if getattr(current_thread(), REPORT_CONTEXT_ATTR_NAME, None):
buffer.write(b + '')
output_func(buffer.getvalue() + '')
else:
old_write(b)
try:
src.write = new_write
yield
finally:
src.write = old_write
@contextmanager
def st_stdout(dst):
"this will show the prints"
with st_redirect(sys.stdout, dst):
yield
@contextmanager
def st_stderr(dst):
"This will show the logging"
with st_redirect(sys.stderr, dst):
yield
def demo_function():
"""
Just a sample function to show how it works.
:return:
"""
for i in range(10):
logging.warning(f'Counting... {i}')
time.sleep(2)
print('Time out...')
if __name__ == '__main__':
with st_stdout("success"), st_stderr("code"):
demo_function()