How to en”Rich” your Python console scripts

Sourav De
5 min readJan 14, 2023

Create awesome command line interfaces in Python

pynetmon in action

Recently I wrote a post on Python GUI using ttkbootstrap, a GUI definitely provides better UX(User Experience) and makes our application/script easy to use. However, a lot of times we don’t need a GUI in the first place for example, in Dev and Production environments most Linux servers we use don’t have a DE (Desktop Environment) installed so we just ssh to the server and start running our scripts.

In such scenarios, we actually don’t need a GUI however we do need to monitor the output of our scripts so some level of visual feedback is still required. Now looking at a long-running stream of log messages or errors can be a daunting and boring task

So how about we make our gloomy wall of text a fresh look 😎. Textualize released a new Python module Rich which helps us create some great-looking Command Line Interfaces which previously would require a lot of effort and thus en”rich”ing our console experience 😀. Here is the demo image showing all the features supported by rich:

You can test the same on your system as well, to do that you’ll need to install the module first which you can do by the following command on the terminal/console:

python -m pip install rich

If you have both Python2 and 3 installed maybe you’ll need to change to python3 or better use venv before installing. To view the demo interface on your terminal:

python -m rich

That’ll give you a nice-looking interface check for color compatibility some terminal emulators might not support it out of the box and you might need to change the color mode to Truecolor or 16bit mode. I use Windows Terminal which supports it perfectly without any issues.

Well, the rich module definitely looks pretty but what should we do with it. So let’s create something useful utilizing this library.

I recently had issues with my ISP and wasn’t getting the bandwidth I was promised. As the issue was very sporadic it was very difficult to monitor so I did what every Network engineer does in this situation and ran the ping command:

Well, it’ll tell you if there is a drop or no connectivity etc. However I wanted to see some graphs and real-time up/downstream speeds so how about creating a Python script to just monitor those.

To show our system’s upload/download speed we need to monitor our ethernet or Wifi adapter. In Python, we have a very good module to monitor system parameters which is psutil. So I will import that module first and then write a function to monitor the active network interface.

The net_usage function calls the net_io_counters() function to get the active network interface details. To measure the speed we will need to compare the within a timeframe which is 1 second here so we measure the initial sent and received bytes then wait for a second and then measure again and finally compare the both to get the actual speed. When executed it'll give the following results

Next, we will use this function and create our interface rich for graphs I found an excellent module asciichartpy it's an npm module ported to python. it'll help us generate graphs like the following:

For the layout I will use the following template:

I think it’s a pretty basic and neat way to show all the relevant information we need to show. This is achieved by using the layout in rich and for the graphs, I will use the Live Display, with layout we can split our CLI into segments and control or update each segment on what information we want to present on the screen. Live display helps auto-refresh the screen at certain intervals and is very useful here for the graphs to update.

So here the layout is split into “header”, “main” and “footer”. The “header” just contains the name of the script “Python Network Monitoring” the footer contains control options right now only one button to exit the script.

The “main” is split into “info” and “graphs” vertically and “graphs” includes two separate sections for up and downstream graphs each. The info shows a real-time up and down speed and all the open ports in use at the moment.

So we finally get a UI that looks like the following:

The code is available on my GitHub to feel free to contribute or modify it any way to would like to and make it more useful to your exact requirements.

If you find this article useful feel free to subscribe or follow me. Also if you want to support my blogging endeavor you can buy me a coffee!

--

--

Sourav De

Python Developer & DevOps Engineer. Expertise in Python, cloud platforms, and CI/CD pipelines. Innovating at the intersection of development and operations