Redirect stdout to TextLog? #2810
Unanswered
victor-shepardson
asked this question in
Q&A
Replies: 1 comment
-
I found that I can monkey-patch print in some cases: import some_module
from textual.app import App
MyTUI(App):
def __init__(self):
super().__init__()
self.std_log = TextLog(id='std_log')
def compose(self):
yield Header()
yield self.std_log
yield Footer()
def print(self, *a, **kw):
"""redirects to the UI's default std output log"""
kw['file'] = self
print(*a, **kw)
def flush(self): pass
def write(self, s):
"""for redirecting stdout to a file-like"""
if self.is_running:
return self.call_from_anywhere(self.std_log.write, s) # std_log is a TextLog
else:
return sys.__stdout__.write(s)
def call_from_anywhere(self, f, *a, **kw):
try:
return self.call_from_thread(f, *a, **kw)
except RuntimeError as e:
return f(*a, **kw)
my_tui_instance = MyTUI()
some_module.print = my_tui_instance.print
my_tui_instance.run() but it would be great to capture output from C extensions, logging frameworks, or any case where |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Conceptually I want to redirect all stdout/stderr to a
TextLog
, so I can incrementally move an app to Textual (and have error / debug statements show up within my TUI rather than being hidden until the app exits). But I don't literally want that since Textual itself needs to render the app to stdout (?) and seems to foil a global redirection of stdout here. Is there anything I can do if I want e.g.print
calls in library code which I don't control to wind up in aTextLog
?Related to #190
my current approach is to add this to my TUI class:
and set
sys.stdout = my_tui_instance
. This works if I useprint(..., file=my_tui_instance)
but fails to redirect unmodified print statements.Beta Was this translation helpful? Give feedback.
All reactions