How does integrating a backend in python textual work?

29 Views Asked by At

I want to automate a testing process by reading data from an Excel file, performing the tests and then output the results in the Python file.

So far I managed to write several classes which each perform at task and save information. In my main() function I create instances and then call functions in these instances to perform the tests.

Now I want to add a GUI using the python framework "textual". I can't wrap my head around on how I would use the class instances with textual. Basically I want to call functions when buttons are presses and later output the results.

This is my backend code.

excelClass = excel_handling()
excelClass.copyExcelFile("Test input data.xlsx")
excelClass.readDataFromExcel()
testDeviceClass = DeviceDataReader(excelClass.ipOfDevice)
dataReaderClass = SerialReader(str(excelClass.comPort))
dataReaderClass.initialize()
#Should be called by a button
runTests(excelClass, testDeviceClass, data)

My app looks like this so far. What's the best way to connect this to my backend?

I had no look with adding an init() function and creating the backend class instances in the GUI app.

class ButtonArea(Static):       
    def on_button_pressed(self, event: Button.Pressed) -> None:
        """Event handler called when a button is pressed."""
        button_id = event.button.id
        if button_id == "start1":
            pass
            #run backend functions
        elif button_id == "start2":
            pass
        elif button_id == "rereadExcel":
            pass
    def compose(self)-> ComposeResult:
        yield Button("Start measurement 1", id="start1", variant = "success")
        yield Button("Start speed measurement", id="start2", variant = "success")
        yield Button("Reread Excel input file", id="rereadExcel", variant = "success")
class GUI(App):
    CSS_PATH = "gui.tcss"
    def compose(self) -> ComposeResult:
        """Create child widgets for the app."""
        yield Header()
        yield Footer()
        with Horizontal(id= "ButtonArea"):
            yield ButtonArea(guiInstance=self)

def main():
    app = GUI()
    app.run()
 
if __name__ == '__main__':
    main()

Thank you for your help.

Please see code above.

0

There are 0 best solutions below