-
Hi, I've been using this library recently, and it seems fantastic. I would like to gradually manage the transition from a command-line app to a textual app. Therefore, I would like to convert all the initial print() statements in the old app into prints within a textual window with a header and footer. Is it possible? from textual.app import App, ComposeResult
from textual.widgets import RichLog
from textual.widgets import Header, Footer
from textual.containers import VerticalScroll
class RichLogApp(App):
def compose(self) -> ComposeResult:
yield Header(self.title)
yield Footer()
yield VerticalScroll(
RichLog(highlight=True, markup=True)
)
def print(self, message):
text_log = self.query_one(RichLog)
text_log.write(message)
if __name__ == "__main__":
app = RichLogApp()
app.run()
app.print('your message here')
app.print('your message here 2') Of course, the code I've written doesn't work because the Is there a way to achieve the result I'm looking for? Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
You can use from textual.app import App, ComposeResult
from textual.events import Print
from textual.widgets import Log
class PrintingApp(App[None]):
def compose(self) -> ComposeResult:
yield Log()
def on_print(self, event: Print) -> None:
self.query_one(Log).write(event.text)
def on_mount(self) -> None:
self.begin_capture_print(self)
print("Your message here")
print("Your message here 2")
if __name__ == "__main__":
PrintingApp().run() |
Beta Was this translation helpful? Give feedback.
-
Hello @davep , I have an issue with the sample code. The display of prints happens at the end of the code, not while the code is being executed. Is there a way to overcome this issue? Example: from textual.app import App, ComposeResult
from textual.events import Print
from textual.widgets import Log
class PrintingApp(App[None]):
def compose(self) -> ComposeResult:
yield Log()
def on_print(self, event: Print) -> None:
self.query_one(Log).write(event.text)
def on_mount(self) -> None:
self.begin_capture_print(self)
print("Your message here 1")
time.sleep(10)
print("Your message here 2")
time.sleep(10)
print("Your message here 3")
if __name__ == "__main__":
PrintingApp().run() |
Beta Was this translation helpful? Give feedback.
You can use
print
capturing and direct the output to your app, a screen or a specific widget. For example: