I'm using the Textual framework for a simple TUI.
The task is to display the results of ChatGPT-prompts as they are streamed.
Problem is: I cannot figure out, how to update the app, so that it shows the streamed results.
Here is a minimal example. I expect the Counter
label to display the numbers from 0 to 9 very quickly. The result is, that I only get the number 9 after waiting for a second.
import time
from textual.app import App
from textual.widgets import Header, Label
from textual.reactive import reactive
def randomgen():
for i in range(10):
time.sleep(0.1)
yield str(i)
class Counter(Label):
countervalue = reactive("Press Enter to start")
def watch_countervalue(self, countervalue):
self.update(countervalue)
class Minimal(App):
def compose(self):
yield Header()
yield Counter(id="counter")
def on_key(self, event):
if event.key == "enter":
for i in randomgen():
self.query_one("#counter").countervalue = i # pyright: ignore
if __name__ == "__main__":
app = Minimal()
app.run()