-
Hi, I was wondering how I would be able to use the I thought about this concept:
here is the example code. gui.py #
# imports
#
# non-standard
import textual
from textual import on
from textual.app import App, ComposeResult
from textual.reactive import reactive
from textual.widgets import Header, Footer, Label, Markdown, TabbedContent, TabPane, Static, Button, DataTable, ProgressBar
from rich.text import Text
# application
import test__mllicmgt
#
# application
#
class TestApp(App):
#
# construct the object
#
def __init__(self, config=None, *args, **kwargs):
super().__init__(*args, **kwargs)
# link application
self.testclass = test__mllicmgt.testclass()
self.data = None
#
# set application hotkeys
#
BINDINGS = [
("q", "exit", "Quit"),
]
#
# reactives
#
# reactive status variable to trigger ProgressBar re-render
progress_status = reactive(0.0)
#
# compose the app GUI
#
def compose(self) -> ComposeResult:
# show footer row (and hotkeys)
yield Footer()
# show progress bar
yield ProgressBar()
yield Button("execute library function", id="btn-exe-lib-fct")
#
# watchers for reactives
#
def watch_progress_status(self) -> None:
self.query_one(ProgressBar).update(
total=1,
progress=self.progress_status,
)
#
# event handlers and action implementations
#
@on(Button.Pressed, "#btn-exe-lib-fct")
def read_data(self):
# execute a library function while connecting it with the progress bar
# widget
self.data = self.testclass.test(
_progress_state=self.progress_status,
)
# now, when the execute was done, change the button text
self.query_one(Button).text = "finished"
def action_exit(self) -> None:
"""exit application"""
self.exit()
#
# when started from CLI
#
if __name__ == "__main__":
# start gui
app = TestApp()
app.run() test__mllicmgt.py import time
#
# test class definition
#
class testclass(object):
def test(
self,
_progress_state=None, # internal variable to connect to progressbar
):
# iterate over 90 items in order to see floating point increments in
# the gui's ProgressBar element
number_of_iterations = 9
for i in range(number_of_iterations):
# update progress. this should trigger the re-render in the gui as
# the _progress_state variable had been defined as "reactive" and
# is written here.
if _progress_state is not None:
_progress_state = i / number_of_iterations
# wait a second within each iteration in order to imitate a real
# calculation process
time.sleep(1)
# return data
return [ "some", "data" ] When Clicking on the Button, the library method gets called, and the I am sure, I disregarded the whole Are there any tips to get the Thanks for any help. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
in effect, I don't want to create an application from scratch but use hope, this would be possible via the concept explained above. |
Beta Was this translation helpful? Give feedback.
-
answered in #5098 |
Beta Was this translation helpful? Give feedback.
answered in #5098