4 Python Command Line Visualization Libraries I recently discovered, so cool!

Usually, everyone runs the program on their own computer, and the corresponding results can be visualized directly.

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

Today, Xiao F will introduce 4 Python libraries that can be used on the command line.

They are Bashplotlib, tqdm, PrettyTable, Colorama.

 

① Use Bashplotlib to draw on the command line

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

First install the Bashplotlib library.

pip install bashplotlib -i https://mirror.baidu.com/pypi/simple/

Below we can use Bashplotlib to plot a set of normally distributed data.

Use NumPy to generate a list of normally distributed numbers.

If you don't have NumPy yet, you can also use pip to install it.

pip install numpy -i https://mirror.baidu.com/pypi/simple/

 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=100)

The results are as follows.

It is indeed possible to visualize data in the terminal.

By viewing the source code, you can learn the relevant parameter settings.

 

② Use TQDM to add progress bar

Sometimes running a program that takes a long time, we cannot see the running 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.

Install TQDM using the pip command.

pip install tqdm -i https://mirror.baidu.com/pypi/simple/

Here's an example-

Let's go through the numbers 0 to 1000 and add a small delay to see the TQDM progress bar in action.

from tqdm import trange
from time import sleep

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

The results are as follows.

It provides people with an expectation that it will not change so far in the future.

 

③ Use PrettyTable to print beautiful tables

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

Using PrettyTable, you can output an easy-to-read, table-like data presentation on the terminal.

Install.

pip install prettytable -i https://mirror.baidu.com/pypi/simple/

Let's create a table of populations of cities in a country.

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)

The result is as follows, it really becomes clear~

Unfortunately, it does not support Chinese very well .

At the same time, you can also sort the table contents.

table.sortby = 'Capital'
print(table)

Take the capital data sorting as an example.

As you can see, Berlin is at the top of the list.

HTML code can also be generated to insert table content into the website.

print(table.get_html_string())

The results are as follows.

Create a new HTML file and place the table under the body tag.

Then open the file in the browser and the result is as follows.

 

④ Color your command line with Colorama

Use Colorama to output your program, there are different colors displayed on the command line, and you can understand the operation of the program faster.

Install using pip.

pip install colorama -i https://mirror.baidu.com/pypi/simple/

Three different color types are supported.

foreground, is the text color

background, is the background color

style, is some additional color style

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

Let's look at some examples next. 

Begin by changing 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 Fore render mode:

from colorama import Fore

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

The result is as follows

Then, let the red background color highlight indicate errors, by setting the background rendering mode Back to RED:

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

result

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

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

The result is as follows

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

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

Well, the sharing of this issue is over. Interested friends can practice and learn by themselves~

Guess you like

Origin blog.csdn.net/river_star1/article/details/117915358