4 cool Python libraries for command line visualization

created at 07-14-2021 views: 5

Introduction

Usually everyone runs the program on their own computer and can directly visualize the corresponding results.

If it is on the server, it is not convenient to use the terminal to view the results.

Today I will introduce you to 4 Python libraries that can be used on the command line.

They are 

  1. Bashplotlib
  2. tqdm 
  3. PrettyTable
  4. Colorama

plot on command line Bashplotlib

If you want to plot data in the command line window, then Bashplotlib is very suitable.

First install the Bashplotlib library.

pip install bashplotlib

Use Bashplotlib's histogram plotting function plot_hist.

import numpy as np
from bashplotlib.histogram import plot_hist

rand_nums = np.random.normal(size=1000, loc=0, scale=1)
plot_hist(rand_nums, bincount=50)

output:

 57|                     o o o o o                     
 54|                     o o o ooo                     
 51|                     o o o ooo                     
 48|                     o ooo oooo                    
 45|                     o oooooooo                    
 42|                    oo oooooooo                    
 39|                  o ooooooooooo o                  
 36|                  ooooooooooooooo                  
 33|                  oooooooooooooooo                 
 30|                 ooooooooooooooooo o               
 27|                 ooooooooooooooooooo               
 24|                 ooooooooooooooooooo               
 21|               o ooooooooooooooooooo               
 18|               ooooooooooooooooooooo               
 15|               oooooooooooooooooooooo              
 12|               oooooooooooooooooooooo o            
  9|             oooooooooooooooooooooooooo            
  6|           ooooooooooooooooooooooooooooooo         
  3|           ooooooooooooooooooooooooooooooo         
  1| ooo oo oooooooooooooooooooooooooooooooooooooo    o
    --------------------------------------------------

It is indeed possible to visualize data on the terminal.

By viewing the source code, know the relevant parameter settings:

def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="default", title="", xlab=None, showSummary=False, regular=False):
    """
    Make a histogram

    Arguments:
        height -- the height of the histogram in # of lines
        bincount -- number of bins in the histogram
        binwidth -- width of bins in the histogram
        pch -- shape of the bars in the plot
        colour -- colour of the bars in the terminal
        title -- title at the top of the plot
        xlab -- boolen value for whether or not to display x-axis labels
        showSummary -- boolean value for whether or not to display a summary
        regular -- boolean value for whether or not to start y-labels at 0
    """
    if pch is None:
        pch = "o"

    if isinstance(f, str):
        f = open(f).readlines()

    min_val, max_val = None, None
    n, mean = 0.0, 0.0

add progress bar with TQDM

Sometimes when running a program that takes a long time, we can't see the progress of the program, and the experience is not very good.

Here you can use TQDM to visualize the running progress of the program directly on the command line.

Use the pip command to install TQDM.

pip install tqdm 

Let’s take an example~

Let's traverse the numbers 0 to 1000 and add a small delay to see how the TQDM progress bar is working.

from tqdm import trange
from time import sleep

for i in trange(1000):
    sleep(0.01)

progress bar

It provides users with an expectation that will not change so far indefinitely.

When we output table data in the terminal, the layout is always messy.

Using PrettyTable, you can display data in a readable, table-like format on the terminal.

installation.

pip install prettytable

Let's create a population table of a country's cities.

from prettytable import PrettyTable

table = PrettyTable()

table.field_names = ['Country', 'Capital', 'Population']
table.add_row(["China", "Beijing", 21893095])
table.add_row(["Russia", "Moscow", 12195221])
table.add_row(["Germany", "Berlin", 3748148])
table.add_row(["Spain", "Madrid", 3223334])
table.add_row(["Finland", "Helsinki", 631695])

print(table)

result:

+---------+----------+------------+
| Country | Capital  | Population |
+---------+----------+------------+
|  China  | Beijing  |  21893095  |
|  Russia |  Moscow  |  12195221  |
| Germany |  Berlin  |  3748148   |
|  Spain  |  Madrid  |  3223334   |
| Finland | Helsinki |   631695   |
+---------+----------+------------+

At the same time, operations such as sorting the contents of the table can also be performed.

table.sortby = 'Population'
print(table)
+---------+----------+------------+
| Country | Capital  | Population |
+---------+----------+------------+
| Finland | Helsinki |   631695   |
|  Spain  |  Madrid  |  3223334   |
| Germany |  Berlin  |  3748148   |
|  Russia |  Moscow  |  12195221  |
|  China  | Beijing  |  21893095  |
+---------+----------+------------+

As you can see, Finland is at the top

You can also generate HTML code to insert the content of the table into the website.

<table>
    <thead>
        <tr>
            <th>Country</th>
            <th>Capital</th>
            <th>Population</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>China</td>
            <td>Beijing</td>
            <td>21893095</td>
        </tr>
        <tr>
            <td>Russia</td>
            <td>Moscow</td>
            <td>12195221</td>
        </tr>
        <tr>
            <td>Germany</td>
            <td>Berlin</td>
            <td>3748148</td>
        </tr>
        <tr>
            <td>Spain</td>
            <td>Madrid</td>
            <td>3223334</td>
        </tr>
        <tr>
            <td>Finland</td>
            <td>Helsinki</td>
            <td>631695</td>
        </tr>
    </tbody>
</table>

highlight the command line with Colorama

Use Colorama to output your program, and there will be different colors displayed on the command line, so you can understand the program's operation more quickly.

Use pip to install.

pip install colorama

Supports three different color types.

  • foreground: is the text color
  • background: is the background color
  • style: is some additional color styles

Through proper configuration, you can bring convenience to your Python command line applications.

Let us look at some examples next.

First change the text to green so that "task completed" is displayed in green font.

This can be done by changing the foreground color to green in the Fore rendering mode:

from colorama import Fore

print(Fore.GREEN)
print("Task completed")

command line highlight

from colorama import Back
print(Back.RED)
print("Error occurred!")

background color

You can also darken the text by changing the rendering style:

from colorama import Style
print(Style.DIM)
print("Not that important")

style

Finally, if you want to restore the previous settings, the reset operation is as follows.

print(Style.RESET_ALL)
print('hello')

Well, this sharing is over. Interested friends can practice and learn on their own.

created at:07-14-2021
edited at: 07-14-2021: