The Textual package deal is an effective way to create GUI-like functions with Python in your terminal. These are often called text-based person interfaces or TUIs. Textual has many alternative widgets built-in to the framework.
A kind of widgets is the ProgressBar
. If you’ll want to present the progress of a obtain or long-running course of, then you’ll in all probability wish to use a progress bar or some form of spinner widget to indicate the person that your utility is working.
This tutorial will present you learn how to create a easy progress bar with Textual!
Set up
In the event you shouldn’t have Textual put in but, you will get it by utilizing Python’s helpful pip software. Open up your terminal and run the next command there:
python -m pip set up textual
Textual will get put in, together with all its dependencies. It’s possible you’ll wish to create a digital surroundings first and set up Textual there.
Making a ProgressBar in Textual
Making a progress bar with Textual is fairly simple. The next code is predicated on an instance from the Textual documentation. The principle distinction is that this model makes use of a button to begin the timer, simulating a obtain or another long-running course of relatively than catching a key occasion.
You may copy this code into your favourite Python editor and provides it a fast research:
# progressbar_demo.py from textual.app import App, ComposeResult from textual.containers import Heart, Center from textual.timer import Timer from textual.widgets import Button, ProgressBar class Progress(App[None]): timer = Timer def compose(self) -> ComposeResult: with Heart(): with Center(): yield ProgressBar() yield Button("Begin") def on_mount(self) -> None: """ Arrange the timer to simulate progress """ self.timer = self.set_interval(1 / 10, self.advance_progressbar, pause=True) def advance_progressbar(self) -> None: """ Known as to advance the progress bar """ self.query_one(ProgressBar).advance(1) def on_button_pressed(self) -> None: """ Occasion handler that is known as when button is pressed """ self.query_one(ProgressBar).replace(whole=100) self.timer.resume() if __name__ == "__main__": app = Progress() app.run()
The compose()
technique is form of enjoyable because it makes use of each the Heart()
and the Center()
containers to place the widgets in the midst of the terminal. You then arrange the timer object in on_mount()
and also you begin the timer in on_button_pressed()
which is the Button
widget’s occasion handler.
Once you run this code, you’ll initially see what’s often called an indeterminate progress bar. What which means is that Textual doesn’t have any details about how lengthy the progress is, so the progress bar simply exhibits a form of “bouncing” or “biking” progress:
Once you press the “Begin” button, you will note the progress bar go from 0-100%, and the textual content fields to the correct will replace as effectively:
If you wish to make your progress bar stand out, add a gradient. A gradient will make the colours of the progress bar change over time and make the widget look neat!
Wrapping Up
Including a progress bar or just displaying some form of informational widget that lets the person know your utility is working is all the time a good suggestion. You don’t need the person to assume the applying has crashed or frozen. The person is likely to be compelled to shut the applying and lose their data in any case!
Thankfully, Textual makes including a progress bar simple. Not solely is it simple, however the progress bar is simple to replace so you’ll be able to precisely give the person a way of when the work will probably be executed. Give it a attempt to see what you assume!
Associated Studying
Need to study extra about Textual? Try the next articles: