Log output from subprocess to defined area of console? #1912
Answered
by
zachwhaley
zachwhaley
asked this question in
Q&A
-
Can Rich log or "stream" output from a subprocess to some defined area of the console? The effect I'm looking for is basically exactly like what the docker cli does below. |
Beta Was this translation helpful? Give feedback.
Answered by
zachwhaley
Feb 4, 2022
Replies: 1 comment
-
I managed to figure something out that is good enough for me. It's not multiple lines of output, just a single line. Code: # progress bar for terminal output (will be hidden when step is done)
# disabling markup because it causes live to hang
prog = Progress(TextColumn("{task.description}", markup=False))
task_id = prog.add_task(" ".join(cmd))
# Use Live to update progress with output
with Live(prog, console=console, transient=True) as live:
with open(file, 'wb') as o: # the file here is to log the full output
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc:
for line in proc.stdout:
o.write(line)
prog.update(task_id, description=line.decode())
live.refresh() |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
zachwhaley
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I managed to figure something out that is good enough for me. It's not multiple lines of output, just a single line.
Code: