DataTable dynamic update (several times in one function) #3776
-
Tell me a way to update fields in the DataTable within one function (this works great if you hang it, for example, on a Button.Pressed handler), but in one function sequentially. from time import sleep
from textual import on
from textual.app import App, ComposeResult
from textual.coordinate import Coordinate
from textual.widgets import Button, DataTable
class TableApp(App):
cnt = 0
def compose(self) -> ComposeResult:
yield Button()
yield DataTable(cursor_type='row')
def on_mount(self) -> None:
self.table = self.query_one(DataTable)
self.table.add_column('A')
self.table.add_row('0')
def change_value(self):
self.table.update_cell_at(
coordinate=Coordinate(row=0, column=0),
value=self.cnt,
update_width=True,
)
self.cnt += 1
@on(message_type=Button.Pressed)
def input_typing_handler(self, event: Button.Pressed) -> None:
self.change_value()
sleep(3) # some hard work
self.change_value()
app = TableApp()
if __name__ == '__main__':
app.run() I don't see two consecutive changes using the change_value function with a break for some external function, I immediately get the last result after the work is done (speed(3) in the example. self.post_message(MyCustomMessage())
sleep(3) # some hard work
self.post_message(MyCustomMessage()) But the result was the same. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
If you're going to be doing non-async-friendly busy work, and you want to update the UI as the work takes place, you should have a read about workers and, in your case, especially thread workers. |
Beta Was this translation helpful? Give feedback.
If you're going to be doing non-async-friendly busy work, and you want to update the UI as the work takes place, you should have a read about workers and, in your case, especially thread workers.