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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.