Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

capture_print does not capture stdout from system calls #5219

Closed
pthebaul opened this issue Nov 8, 2024 · 4 comments
Closed

capture_print does not capture stdout from system calls #5219

pthebaul opened this issue Nov 8, 2024 · 4 comments

Comments

@pthebaul
Copy link

pthebaul commented Nov 8, 2024

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.

textual.mp4
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
Copy link

github-actions bot commented Nov 8, 2024

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
Copy link
Collaborator

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.

@pthebaul pthebaul closed this as completed Nov 8, 2024
Copy link

github-actions bot commented Nov 8, 2024

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@pthebaul
Copy link
Author

pthebaul commented Nov 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants