If you could send some kind of notification in a few hours when i will be near the PC, I will provied you with a solution I’ve managed to create using some of the code here, which works great, not exactly a console, but you are able to see the output if it’s comming from ‘logging’, and you can see it on the screen
Is there a way to limit the number of lines that are printed?
I have an app that has a long running process with many steps. Each step prints to the console as part of its progress. I only want to display the last 10 lines of stderr. Is this possible?
If you’re using the (excellent) loguru as logging manager, I manage to redirect console logs to streamlit apps by adding a related sink to a logger.
For example, if you want st.warning to catch logger.warning (and nothing else) and st.error to catch logger.error (and nothing else), the following snippet should work
from loguru import logger
import streamlit as st
def redirect_loguru_to_streamlit():
def _filter_warning(record):
return record["level"].no == logger.level("WARNING").no
if 'warning_logger' not in st.session_state:
st.session_state['warning_logger'] = logger.add(st.warning, filter=_filter_warning, level='INFO')
if 'error_logger' not in st.session_state:
st.session_state['error_logger'] = logger.add(st.error, level='ERROR')
redirect_loguru_to_streamlit()
def main():
logger.info('This should not be printed in app.')
logger.warning('This should be printed as `st.warning`.')
logger.error('This should be printed as `st.error`.')
if st.button('Rerun'):
st.write('You should not see duplicated logs.')
if __name__ == '__main__':
main()
EDIT: the counter example works on streamlit 1.7 if you swap out the import and context constant:
# from streamlit.report_thread import REPORT_CONTEXT_ATTR_NAME
from streamlit.script_run_context import SCRIPT_RUN_CONTEXT_ATTR_NAME
...
# if getattr(current_thread(), REPORT_CONTEXT_ATTR_NAME, None):
if getattr(current_thread(), SCRIPT_RUN_CONTEXT_ATTR_NAME, None):
Got something working I think similar using contextlib. I’ll check later if there was extra logs gotten from REPORT_CONTEXT_ATTR_NAME it misses.
from streamlit.runtime.scriptrunner.script_run_context import SCRIPT_RUN_CONTEXT_ATTR_NAME
...
if getattr(current_thread(), SCRIPT_RUN_CONTEXT_ATTR_NAME, None):
Hello, can you help, I am a begginer with streamlit, how to use this pass in my code. I am running a job, and have an output to terminal and to info.log. How can I print it to my streamlit page?
I had to say: disabled the “print to stdout” is really stupid default setting. As a glue tools , the streamlit had to corporate with other lib to complete real jobs. no stdout, we lost all info from these core lib.