Sometimes insights can be drawn from visualizations of data rather than staring at it.

TL;DR: The post is about a terminal visualization tool lehar that is open sourced at https://github.com/darxtrix/lehar

While hacking on ptop last week, I thought of adding a network sensor to it but I was stuck thinking how will I visualize it to the end user. Just a heads up, ptop is a task manager in Python that I worked upon as a side project back in 2015. It went on to hit the list of trending repositories on GitHub.

A network sensor will basically show the current incoming/outgoing data on the network to the user. For eg. the CPU sensor of ptop showing the CPU info looks like this:

CPU usage info in ptop

One distinction we can draw from here is that in case of sensors like CPU, memory we have an upper_bound but in case of a network sensor we don’t have one. So the visualization should make user understand that the network utilization was more at a point t1 as compared to a point t2 along with showing the up-speed & down-speed, so relative ordering of data is needed here.

I tried to draw an analogy with the standard sorting problem where the data can be as random as network speed at a certain point of time(position in case of sorting). So, I wanted to come up with a way to show the movement(swaps) in case of a standard sorting algorithm, i.e something like this GIF:

Spark provides a shell command for creating such visualizations out of data but I can’t find any Python library for this. So I wrote lehar. lehar can be used as a Python2 and Python3 module and at the same time it can be used as shell command. The code is open sourced at GitHub

lehar uses the relative ordering of data to represent it. For eg.

data = [1,2,3,4,5] will be represented as ▂▄▅▇█ ( increasing heights)

For drawing such visualizations, just call the dead simple lehar API in your Python code. For eg. the code I used for drawing the above visualizations is a slight modification of the standard Selection Sort program where instead of moving smaller elements in the front of the array, the larger ones’ are moved towards the end. Apart from that, we are just printing partially sorted data at line#10 and giving a time delay in line#21 with a carriage return following in next line to print things in the same line in the terminal

And, yes lehar has support for colors too !

And finally, the network sensor can be visualized like this along with the network stats:

Network visualization ptop

You can contribute to lehar and find the usage instructions at https://github.com/darxtrix/lehar/

In case you liked the story, hit claps. For any reviews/thoughts or any alternate ways to show this info in terminal do comment here.

Let’s share and connect on Twitter @darxtrix