Is is possible to continuously update TextLog from a running process? #2689
-
I'm trying to continuously write to a TextLog widget from a subprocess call, but it seems output is blocked until the call finishes. I have a minimal (non-)working example below (without Textual it works and updates line by line, but not with Textual TextLog). Any ideas would be appreciated? from subprocess import PIPE, STDOUT, Popen
from textual.app import App
from textual.widgets import Footer, TextLog
class UI(App):
BINDINGS = [("1", "run", "run"),]
def compose(self):
yield TextLog()
yield Footer()
def action_run(self):
log = self.query_one(TextLog)
log.write("start")
args = ["bash", "-c", "echo foo;sleep 3;echo bar"]
proc = Popen(args, stdout=PIPE, stderr=STDOUT, text=True)
while proc.poll() is None:
line = proc.stdout.readline()
if line:
log.write(line)
if __name__ == "__main__":
UI().run() |
Beta Was this translation helpful? Give feedback.
Answered by
willmcgugan
May 30, 2023
Replies: 1 comment 1 reply
-
You will need to make your action concurrent. The Textual way to do this is with the worker API. Note the addition of the decorator, and from subprocess import PIPE, STDOUT, Popen
from textual.app import App
from textual.widgets import Footer, TextLog
from textual import work
class UI(App):
BINDINGS = [
("1", "run", "run"),
]
def compose(self):
yield TextLog()
yield Footer()
@work
def action_run(self):
log = self.query_one(TextLog)
log.write("start")
args = ["bash", "-c", "echo foo;sleep 3;echo bar"]
proc = Popen(args, stdout=PIPE, stderr=STDOUT, text=True)
while proc.poll() is None:
line = proc.stdout.readline()
if line:
self.call_from_thread(log.write, line)
if __name__ == "__main__":
UI().run() |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You will need to make your action concurrent. The Textual way to do this is with the worker API.
Note the addition of the decorator, and
call_from_thread
.