Switching screen when a worker thread finishes it's work? #6227
-
|
Hi all. I am very new to Textual and learning the ins and outs. I am a bit stuck here, and I'm wondering if I'm misunderstanding the patterns of workers and screens. Here's my code so far: from textual.app import App, ComposeResult
from textual.widgets import Footer, Header, Input, LoadingIndicator, Label
from textual.screen import Screen
from textual.message import Message
import roster
from pathlib import Path
TITLE = "rosterget"
VERSION = "v0.1"
class RosterApp(App):
CSS_PATH = "roster.tcss"
def compose(self) -> ComposeResult:
yield Header()
yield Input(placeholder="Enter path to .xlsx file...")
def on_mount(self) -> None:
self.title = TITLE
self.sub_title = VERSION
async def on_input_submitted(self, message:Input.Submitted) -> None:
input_box = self.query_one(Input)
self.run_worker(self.load_excel(input_box.value),exclusive=True,thread=True)
# Spawned our worker thread. Switch the UI over to our loading screen to show the user we're working.
self.push_screen(LoadingScreen())
# This loading screen works perfectly while the background work is going!
async def load_excel(self,path:str):
self.loaded_table = roster.RosterTable(Path(path))
self.title = "Loaded " + str(self.loaded_table.get_row_count()) + " Rows"
self.sub_title = TITLE + " " + VERSION
# Despite the loading screen looking great, we're stuck on that screen.
# I've tried screen_push(), which causes the app to close.
# I've tried screen_switch(), which causes the app to freeze.
# Would I have to do this in the above async function?
# Or, would I have to create something within the LoadingScreen class to handle the worker thread completing it's work?
class LoadingScreen(Screen):
def compose(self) -> ComposeResult:
yield Header()
yield LoadingIndicator()
class QueryScreen(Screen):
def compose(self) -> ComposeResult:
yield Header()
yield Label("Finished!")
if __name__ == "__main__":
app = RosterApp()
app.run()I added some comments where I'm tripped up. I am assuming I'm using the concept of screens incorrectly. I'd like to be able to switch to a new UI when the spreadsheet is loaded so that the user can complete queries through widgets within the loaded workbook. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
You should avoid calling methods on your UI directly from a threaded worker - use |
Beta Was this translation helpful? Give feedback.
You should avoid calling methods on your UI directly from a threaded worker - use
call_from_threador custom messages for multiple updates.https://textual.textualize.io/guide/workers/#thread-workers