Textual is a good Python bundle for creating a light-weight, highly effective, text-based person interface. Meaning you’ll be able to create a GUI in your terminal with Python with out studying curses! However what occurs whenever you encounter some issues that require debugging your utility? A TUI takes over your terminal, which implies you can not see something from Python’s print()
assertion.
Wait? What about your IDE? Can that assist? Really no. Whenever you run a TUI, you want a totally practical terminal to work together with it. PyCharm doesn’t work nicely with Textual. WingIDE doesn’t also have a terminal emulator. Visible Studio Code additionally doesn’t work out of the field, though you could possibly make it work with a customized json or yaml file. However what do you do if you happen to can’t determine that out?
That’s the crux of the issue and what you’ll find out about on this tutorial: Learn how to debug Textual purposes!
Getting Began
To get probably the most out of this tutorial, ensure you have put in Textual’s improvement instruments through the use of the next command:
python -m pip set up textual-dev --upgrade
After getting the most recent model of textual-dev
put in, you might proceed!
Debugging with Developer Mode
Whenever you wish to debug a Textual utility, it’s good to open two terminal home windows. On Microsoft Home windows, you’ll be able to open two Powershell or two Command Prompts. Within the first terminal, run this command:
textual console
The Textual console will hear for any Textual utility working in developer mode. However first, you want some form of utility to check with. Open up your favourite Python IDE and create a brand new file known as hello_textual.py
. Then enter the next code into it:
from textual.app import App, ComposeResult from textual.widgets import Button class WelcomeButton(App): def compose(self) -> ComposeResult: yield Button("Exit") def on_button_pressed(self) -> None: self.mount(Button("Different")) if __name__ == "__main__": app = WelcomeButton() app.run()
To run a Textual utility, use the opposite terminal you opened earlier. The one which isn’t working Textual Console in it. Then run this command:
textual run --dev hello_textual.py
You will notice the next in your terminal:
In the event you swap over to the opposite terminal, you will notice a whole lot of output that appears one thing like this:
Now, if you wish to check that you’re reaching part of your code in Textual, you’ll be able to add a print()
perform now to your on_button_pressed()
technique. You can too use self.log.data()
which you’ll be able to examine within the Textual documentation.
Let’s replace your code to incorporate some logging:
from textual.app import App, ComposeResult from textual.widgets import Button class WelcomeButton(App): def compose(self) -> ComposeResult: yield Button("Exit") print("The compose() technique was known as!") def on_button_pressed(self) -> None: self.log.data("You pressed a button") self.mount(Button("Different")) if __name__ == "__main__": app = WelcomeButton() app.run()
Now, whenever you run this code, you’ll be able to test your Textual Console for output. The print()
assertion needs to be within the Console with out you doing something aside from working the code. It’s essential to click on the button to get the log assertion within the Console.
Here’s what the log output will appear to be within the Console:
And right here is an instance of what you get whenever you print()
to the Console:
There’s not a lot distinction right here, eh? Both manner, you get the data you want and if it’s good to print out Python objects, this is usually a useful debugging software.
In the event you discover the output within the Console to be too verbose, you should utilize -x
or --exclude
to exclude log teams. Right here’s an instance:
textual console -x SYSTEM -x EVENT -x DEBUG -x INFO
On this model of the Textual Console, you might be suppressing SYSTEM, EVENT, DEBUG, and INFO messages.
Launch your code from earlier and you will notice that the output in your Console is drastically lowered:
Now, let’s learn to use notification as a debugging software.
Debugging with Notification
In the event you like utilizing print()
statements then you’ll love that Textual’s App()
class offers a notify()
technique. You’ll be able to name it anyplace in your utility utilizing self.app.notify()
, together with a message. If you’re in your App
class, you’ll be able to cut back the decision to easily self.notify().
Let’s take the instance from earlier and replace it to make use of the notify technique as an alternative:
from textual.app import App, ComposeResult from textual.widgets import Button class WelcomeButton(App): def compose(self) -> ComposeResult: yield Button("Exit") def on_button_pressed(self) -> None: self.mount(Button("Different")) self.notify("You pressed the button!") if __name__ == "__main__": app = WelcomeButton() app.run()
The notify()
technique takes the next parameters:
message
– The message you wish to show within the notificationtitle
– An optionally available title so as to add to the messageseverity
– The message’s severity, which interprets to a special colour for the notification. You could use “info”, “error” or “warning”timeout
– The timeout in seconds for the way lengthy to indicate the message
Attempt modifying the notification to make use of extra of those options. For instance, you possibly can replace the code above to make use of this as an alternative:
self.notify("You pressed the button!", title="Information Message", severity="error")
Textual’s App
class additionally offers a bell()
technique you’ll be able to name to play the system bell. You would add this to actually get the person’s consideration, assuming they’ve the system bell enabled on their pc.
Wrapping Up
Debugging your TUI utility efficiently is a ability. You could know the best way to discover errors, and Textual’s dev mode makes this simpler. Whereas it will be nice if a Python IDE had a totally practical terminal constructed into it, that could be a very area of interest want. So it’s nice that Textual included the tooling it’s good to determine your code.
Give the following tips a strive, and also you’ll quickly be capable of debug your Textual purposes simply!