Textualize / textual

The lean application framework for Python. Build sophisticated user interfaces with a simple Python API. Run your apps in the terminal and a web browser.
https://textual.textualize.io/
MIT License
25.73k stars 792 forks source link

capture_print does not capture stdout from system calls #5219

Closed pthebaul closed 2 weeks ago

pthebaul commented 2 weeks ago

Outputs from os.system or subprocess.call are not captured by textual, and appear at the top-left of the terminal instead.

My goal is to run scripts upon button presses, and to see the scripts' outputs in a Log widget in realtime.

textual diagnose didn't recognize my terminal. I am using Windows Terminal version 1.21.2911.0, but I have the same issue using the legacy terminal (by selecting "Windows Console Host" in Windows 11 settings). The issue is there using PowerShell 7.4.6 or MS-DOS for the terminal. Note that os.system runs MS-DOS.

Here is a video, and the MRE can be found below.

https://github.com/user-attachments/assets/92c3a4e4-1b40-4476-8e4f-6d591d71f5ef

from textual.app import App
from textual.widgets import Log
import os

class MyApp(App):
    def compose(self):
        yield Log(id="log")

    def on_print(self, event):
        self.query_one("#log", Log).write(event.text)

    def on_key(self, event):
        if event.key == 'q':
            print('q was pressed')
        elif event.key == 'w':
            os.system("echo w was pressed")

    def on_ready(self):
        self.begin_capture_print(self, True, True)

if __name__ == "__main__":
    app = MyApp()
    app.run()

Textual Diagnostics

Versions

Name Value
Textual 0.85.2
Rich 13.9.3

Python

Name Value
Version 3.12.5
Implementation CPython
Compiler MSC v.1940 64 bit (AMD64)
Executable C:\Program Files\Python312\python.exe

Operating System

Name Value
System Windows
Release 11
Version 10.0.22631

Terminal

Name Value
Terminal Application Unknown
TERM Not set
COLORTERM Not set
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=120, height=30
legacy_windows False
min_width 1
max_width 120
is_terminal True
encoding utf-8
max_height 30
justify None
overflow None
no_wrap False
highlight None
markup None
height None
github-actions[bot] commented 2 weeks ago

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

willmcgugan commented 2 weeks ago

This is expected. Rich can only capture output generated from its own process. To capture output from another process, you will need to use subprocesses. There is good support for subprocesses in the standard library.

github-actions[bot] commented 2 weeks ago

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

pthebaul commented 2 weeks ago

Alright, thanks for your quick reply, and thank you very much for Textual!