prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.37k stars 716 forks source link

Stream output from a subprocess to a window? #1076

Open keyz182 opened 4 years ago

keyz182 commented 4 years ago

I've been poking around at this for a bit now, and I'm drawing a blank as to how I'd do this.

The basic use case is - I'd like to run the command docker-compose up, and stream it's output. If I run that command alone, it just prints it's output to stdout, I'd like it to do that, but within a window/component. Later, I'd like to add a menu, and a second window to show some other bits, but right now, I'd be happy if I could just get the basic use case up and running.

From subprocess.Popen I can either obtain a BufferedReader, or I believe the FD for it's stdout to treat it like a pipe/file.

Any pointers?

jonathanslenders commented 4 years ago

Hi @keyz182,

You have to look into how to do subprocesses with asyncio: https://docs.python.org/3/library/asyncio-subprocess.html#examples Then start a coroutine that consumes the output from the streamreader, and send it to a window.

It could also be worth looking at ptterm: https://github.com/prompt-toolkit/ptterm That's a user control that implements a virtual vt terminal and can run any subprocess in there. It's not well maintained unfortunately. Check out the prompt-toolkit-3.0 branch.