程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Recently found 4 Python command line visualization libraries, cool!

編輯:Python

Usually people run programs on their own computers , Directly, the corresponding results can be visualized .

If it's on the server , Use terminal , It's not convenient to view the results .

today , Small F I'll introduce it to you 4 One that can be used on the command line Python library .

Namely Bashplotlib、tqdm、PrettyTable、Colorama.

① Use Bashplotlib Drawing on the command line

If you want to draw data in a command line window , that Bashplotlib It's very suitable . 

First installation Bashplotlib This library .

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

Now we can use Bashplotlib To plot a set of normal distribution data graphs .

Use NumPy To generate a list of normally distributed numbers .

If not NumPy, It can also be used pip install .

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

Use Bashplotlib Histogram drawing function of 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)

give the result as follows .

picture

It's really possible to visualize data on the terminal .

By looking at the source code , Know the relevant parameter settings .

picture

②  Use TQDM Add progress bar

Sometimes it takes a long time to run a program , We can't see the progress of the program , The experience is not very good .

Here you can use TQDM, Visualizing the progress of a program directly on the command line .

Use pip Command to install TQDM.

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

Let's take an example ~

Let's go through the numbers 0 to 1000, And add a little delay , To see TQDM How the progress bar works .

from tqdm import trange
from time import sleep
for i in trange(1000):
    sleep(0.01)

give the result as follows .

picture

Provides an expectation , It won't be so far away .

③  Use PrettyTable Print beautiful forms

When we output table data at the terminal , Typesetting is always messy .

Use PrettyTable, It can output readable 、 Data presentation similar to tabular format .

install .

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

Let's create a population table of national 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)

give the result as follows , It really becomes clear ~

picture

It is a pity , It doesn't support Chinese very well .

meanwhile , You can also sort the contents of the table .

table.sortby = 'Capital'
print(table)

Take the capital data sorting as an example .

picture

You can see , Berlin is at the top of the list .

It can also generate HTML Code , Insert the table content into the website .

print(table.get_html_string())

give the result as follows .

picture

Create a new one HTML file , Put the form in body Under the label .

picture

Then open the file in the browser , give the result as follows .

picture

④  Use Colorama Coloring your command line

Use Colorama Output... For your program , There are different colors on the command line , Faster understanding of the operation of the program .

Use pip install .

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

Supports three different color types .

foreground, It's text color

background, It's the background color

style, It's some extra color styles

picture

By properly configuring , I can give you Python Command line applications bring convenience .

Now let's look at some examples . 

First change the text to green , To display in green “ The task has been completed ”.

This can be done by Fore Change the foreground color to green in render mode :

from colorama import Fore
print(Fore.GREEN)
print("Task completed")

give the result as follows

picture

then , Let the highlight of the red background color indicate an error , By setting the background rendering mode Back To RED:

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

result

picture

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

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

give the result as follows

picture

Last , If you want to restore the previous settings , The reset operation is as follows .

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

Okay , This is the end of this issue , If you are interested, you can learn by yourself .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved