One solution would be to redirect stdout/stderr to st.write (or anything you want).
Here’s a quick example for stdout:
from contextlib import contextmanager, redirect_stdout
from io import StringIO
from time import sleep
import streamlit as st
@contextmanager
def st_capture(output_func):
with StringIO() as stdout, redirect_stdout(stdout):
old_write = stdout.write
def new_write(string):
ret = old_write(string)
output_func(stdout.getvalue())
return ret
stdout.write = new_write
yield
output = st.empty()
with st_capture(output.code):
print("Hello")
sleep(1)
print("World")
output = st.empty()
with st_capture(output.info):
print("Goodbye")
sleep(1)
print("World")
It looks like I may have spoken too fast! The workaround I found earlier doesn’t quite work, issue logged here:
I believe that being able to print the console’s output to Streamlit would likely fix the issue. As you can see in the screenshot below, users would need to click on the URL in the console for the app to work:
Alright, new version. Tell me if it works in your case
EDIT: removed output = st.empty(). Now you just have to put streamlit’s function name as parameter.
from contextlib import contextmanager
from io import StringIO
from streamlit.report_thread import REPORT_CONTEXT_ATTR_NAME
from threading import current_thread
import streamlit as st
import sys
@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):
with st_redirect(sys.stdout, dst):
yield
@contextmanager
def st_stderr(dst):
with st_redirect(sys.stderr, dst):
yield
with st_stdout("code"):
print("Prints as st.code()")
with st_stdout("info"):
print("Prints as st.info()")
with st_stdout("markdown"):
print("Prints as st.markdown()")
with st_stdout("success"), st_stderr("error"):
print("You can print regular success messages")
print("And you can redirect errors as well at the same time", file=sys.stderr)
#1 Your code, which I pasted at the end of my script.
#2 the terminal text I want printed
#3 the terminal text doesn’t print in Streamlit
#4 FYI the Google Consent Screen which appears locally gets blocked when deployed in Heroku or Streamlit Sharing - This is the main reason why I want the terminal message to be printed
Note that I’ve pasted your code in my script without any modifications - Maybe I should have added something to it?
My guess is that your google API does some sort of print (which uses sys.stdout internally).
What my proof-of-concept does is redirect sys.stdout to a streamlit function of your choice.
To make it work in your case, you have to put your functions that print messages in your terminal inside that kind of code block:
with st_stdout("info"):
# Inside this block, every function that prints to your terminal
# will have their output redirected to output.info(), a streamlit function.
I guess that the function which prints to your terminal is searchconsole.authenticate(), right? You’d do something like this:
with st_stdout("info"):
searchconsole.authenticate(client_config="GSCTatieLouCredentials.json", serialize='credentials.json', flow="console")
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.