textual
textual copied to clipboard
Interacting with STDIN before creating an app prevents the app from running properly
Windows Python 3.9 rich==12.5.1 textual==0.1.18
Behavior when calling below script in cmd.exe is unexpected.
Command: echo "tmp" | .\script.py
expected behavior: prints "tmp". Then runs the App (accepting inputs).
seen behaviour: App takes over terminal, but does not accept / launch anything.
script.py:
from rich.console import RenderableType
from rich.text import Text
from textual import events
from textual.app import App
from textual.reactive import Reactive
from textual.widget import Widget
class InputText(Widget):
content: Reactive[RenderableType] = Reactive("")
def __init__(self, title: str):
super().__init__(title)
self.title = title
def on_key(self, event: events.Key) -> None:
self.content = event.key
def render(self) -> RenderableType:
renderable = Align.left(Text(self.content, style="bold"))
return renderable
class MainApp(App):
username: Reactive[RenderableType] = Reactive("")
async def on_mount(self) -> None:
grid = await self.view.dock_grid(edge="left", name="left")
grid.add_column(fraction=1, name="center", min_size=20)
grid.add_row(name="top", min_size=2, max_size=2)
grid.add_areas(
area1="center,top",
)
self.input = InputText("input")
grid.place(
area1=self.input,
)
await self.view.dock(self.input, edge="bottom")
await self.input.focus()
if __name__ == "__main__":
import sys
for i, line in enumerate(sys.stdin):
print(line)
if i == 0:
break
MainApp.run(log="textual.log")
I would also like to use textual in a context where stdin is a pipe. You can easily see a problem--I assume it's the same problem, more or less--with the calculator example.
I made an asciicast to demonstrate the issue: https://asciinema.org/a/mOC6vR7aApEUSBXYO6Ro49R2C
The firs time, when I run python calculator.py
, it works as expected. But if I run echo foo | python calculator.py
I get all of this weird output, and the calculator doesn't accept my clicks.
The weird output correlates with mouse movement and clicks, it's just that the calculator never receives them.
Exactly the same issue!
@ohle has suggested a viable (for my use case) workaround in https://github.com/Textualize/textual/issues/153#issuecomment-1256933121. Does that work for you?
Yes that works, thanks for pointing it out.
https://github.com/Textualize/textual/wiki/Sorry-we-closed-your-issue
Did we solve your problem?
Glad we could help!
Have this similar issue oleksis/jtree/issues/1 . Think sys.stdin.read
block/freeze the app from render on Windows.
In GNU/Linux we need change sys.stdin = open("/dev/tty", "r")
for fix the renderables widgets
Any advice or workaround on Windows ?