click on blue
Follow us

picture

Pay attention to the official account, reply " python data " in the background, and get the python data package.

Hello everyone, welcome to the Python introductory tutorial produced by Idea Lab. I am the head of the room.

On our way to learn programming, the one that accompanies us the longest is the interpreter. And we often use the print function to let the interpreter print out some information to see how the program is running, and the information about the abnormal operation of the program will also be printed out. If the program is written without a graphical interface, the information printed by the interpreter may be the key prompt to guide people to operate the program. So an expressive print result can not only make us pleasing to the eye, but also bring a better experience to the user.

But what we print out right now is so ordinary and unremarkable. How can we make what the interpreter prints out in front of our eyes? We can use the rich module.

The rich module is so lavish when you hear the name. However, the rich here does not mean rich, it represents "rich text". Some students may ask, are there rich and poor texts? Yes! What we write in the notepad and what we print out using the print method can be regarded as "poor text". There is nothing but text, no font, no font size, no color, no size. But because the word "poor" doesn't sound good, we can change it and call it "plain text".

picture

But "rich text" has a wide and small aura as soon as it comes out, it can set various elements such as font, font size, color and so on. This increases the expressiveness of the text by several notches. So how to edit rich text? Students who have read the room director's HTML tutorial can quickly think of it: use tags. The director will talk about how to use the markers in the rich module later.

Of course, the rich module is more "rich" than rich text. It can also automatically highlight syntax, print tables, print markdown text and implement some other functions, which is very practical. Without further ado, let's install the rich module and try it out for ourselves. The installation name of the rich module is the same as the import name. Before importing, we can use python to run the rich module in the terminal (modules can also be run as code):

picture

picture

picture

Does it look dazzling?

We need to import the print method in the rich module. This method has the same name as the system print method. If these two methods are used in the code, the name needs to be changed when importing. Let's print a list to see how the two methods differ:

picture

picture

It's no different! If you are using PyCharm, you will indeed get this result, but this is because the command line interface that PyCharm uses to run the code to run the code comes out by default is "no color mode", we need to set it. Click this little wrench:

picture

Then check Emulate terminal in output console under Execution.

picture

Now let's do it again:

picture

Is it different?

Although we didn't specify the font color to print out, the rich module did so automatically based on the data type. Let's take a look at what other data types will look like:

picture

picture

picture

picture

Even bool values ​​and data types have their own exclusive colors, and now I finally don't have to look at a bunch of white text and have a headache.

Next is the markup method of rich text that we talked about earlier. Unlike HTML, the rich module is marked with square brackets:

picture

picture

But the rich module is marked in a more arbitrary way, and can accommodate multiple styles in one markup:

picture

picture

In addition, the rich module can also easily print emoji expressions, but this requires the support of PyCharm or command prompt. The head of the room has not tested successfully on win10. Students who know it can leave a message to share. The method of printing is very simple, just add two colons on both sides of the emoji expression name:

picture

More advanced, if our string to be printed is a piece of code, the rich module can also implement syntax highlighting:

picture

picture

But it doesn't look very good, that's because we didn't tell the rich module what language the code is. In the rich submodule syntax, there is a Syntax class. We pass the code into the Syntax class and set the code language and display style to make code highlighting that conforms to the language:

picture

picture

这里室长设置了两个格式,一个是显示代码行数,一个是背景颜色为黑色。如果想改文字的配色规则的话需要设置theme参数,内容是各种配色的名称,比如我们现在PyCharm使用的darcula:

picture

picture

如果你觉得Python的异常提示信息总让你摸不到头脑,还要按照行数一行一行地去比对出问题的地方,甚至不知道出问题时变量的值是多少,那你不妨尝试一下rich的traceback功能。最简单的方法就是直接把traceback安装到代码中,这样代码出现的异常都会出现rich的traceback信息:

picture

这里我们设置了show_locals参数为True,这样就可以显示异常发生时各个变量的值了:

picture

可以看到rich的traceback功能展示出了出现异常所在代码行的前几行,方便我们进行查找定位,同时展现出了异常发生时各个变量的值,方便我们排查问题。

虽然我们介绍了这么多令人眼花缭乱的功能,但这仅仅是rich模块诸多功能中的一小部分。不过如果想要实现其他更多的功能,我们需要进行稍微复杂一些的操作,当然只是稍微复杂。由于篇幅所限,我们下期再聊。如果这篇文章对你有所帮助,希望能帮室长点个赞和在看,你的鼓励是室长进步的动力!

【室长原理课】系列在不正经地科普一些互联网小知识,没有太多高深的内容,把这个系列分享给你的朋友吧!

【室长原理课】网上那么火的爬虫,到底是个什么东西?
【室长原理课】大几百买的路由器,究竟是用来干嘛的?
【室长原理课】为什么只换了个DNS,上网就一下子快了?
【室长原理课】都说P2P暴雷了,但你知道P2P到底是什么吗?

【室长原理课】为了让你看上网页,你知道浏览器有多努力吗?

【室长原理课】那么时髦的NAS,你要不要也组一个?


喜欢此内容的人还喜欢

主线教程系列:

【Python教程】铺天盖地的Python广告,点进去却发现门都入不了?


【Python教程】引言——程序是什么,又为什么是程序?


【Python教程】一、懒是人类进步的阶梯,编程则是懒上加懒|Python解释器


【Python教程】二、写一个代码有二百多个秘书伺候着,还愁写不出来好东西?Python的IDE


【Python教程】三、我把骆驼起名叫大象,也能把它装冰箱吗?程序的变量 


【Python教程】四、函数真的是含着树吗?程序的函数(一)


【Python教程】五、如果函数是一个工厂|程序的函数(二)


【Python教程】六、如果你给不了我想要的,我就报错给你看|数据类型


【Python教程】七、那么多如果,只剩下结果|if语句和bool值


【Python教程】八、码农一张嘴,程序跑断腿|for循环


【Python教程】九、只要学不死,就往死里学|while 循环和逻辑运算


【Python教程】十、站在巨人的光头上|初识Python模块


【Python教程】十一、泪流满面,下载再也不用度娘了|模块的下载与安装 


【Python教程】十二、小试牛刀,写一个给文件批量重命名的小程序名


【Python教程】十三、我连对象都找不到还让我用对象?类与对象(一)


【Python教程】十四、找不到对象?用Python自己写一个|类与对象(二)


【Python教程】十五、动物园举办联欢大会哪个动物没有来?类变量


【Python教程】十六、变量真的是变量名+变量值吗?变量的本质 


【Python教程】十七、在Python里搞克隆|浅拷贝与深拷贝 


【Python教程】十八、这根本就不是我的错嘛!Python的异常处理


【Python教程】十九、用Python写bug的你,遇到过哪些坑?Python的异常类型


【Python教程】二十、我竟然从全员大筛中领悟到了生成器的用法!迭代器和生成器


【Python教程】二十一、懒出天际!Python中的简洁语法


【Python教程】二十二——奇怪的符号增加了!Python的装饰器


【Python教程】二十三、原来是你!Python的魔法方法(一)


【Python教程】二十四、原来是你!Python的魔法方法(二)


【Python教程】二十五、Python中的魔法属性


番外系列

【Python番外】玩大了,一个解释器已经不够用了|Anaconda介绍


【Python番外】写代码宜工工整整,忌放飞自我|代码规范


【Python番外】坑太多了都不知道蹲哪个了|关键字参数和默认值 


【Python教程】列表原来是贪吃蛇?列表小技能


【Python教程】列表不是贪吃蛇,是贪吃蚯蚓?列表切片


【Python教程】羊肉串香还是字符串香?都香!字符串小技巧


【Python教程】小练习,写一个“谁在哪儿干什么”的小游戏


【Python教程】加强版列表?字典小技巧


【Python教程】Python中的时间处理——time模块


【Python教程】Python中的时间处理——datetime模块


【Python教程】把文字里的数据“抠”出来|正则表达式和re模块(一)


【Python教程】把文字里的数据“抠”出来|正则表达式和re模块(二)


【Python教程】把文字里的数据“抠”出来|正则表达式和re模块(三)


【Python教程】小练习,用Python解数独(一)


【Python教程】小练习,用Python解数独(二)


【Python教程】小练习,用Python解数独(三)


【Python教程】小练习,用Python解数独(完,内附代码)


【Python教程】随机一下!Python中的random模块


不会爬虫,没有数据?这个模块可以帮你获取!


【Python教程】使用pillow模块做个照片墙


【Python教程】造“假”有理!用faker模块批量生成假数据

Pandas、Numpy系列:

【Python教程】有了它,excel彻底沦为工具人!初识pandas模块(一)


【Python教程】有了它,excel彻底沦为工具人!初识pandas模块(二)


【Python教程】正儿八经地认识一下pandas(一)


【Python教程】正儿八经地认识一下pandas(二)


【Python教程】正儿八经地认识一下pandas(三)


【Python教程】关于pandas,你还应该知道这些(一)


【Python教程】关于pandas,你还应该知道这些(二)


【Python教程】来认识认识Pandas的爹!初识NumPy模块(一)


【Python教程】来认识认识Pandas的爹!初识NumPy模块(二)


【Python教程】来认识认识Pandas的爹!初识NumPy模块(三)


【Python教程】小练习,使用Pandas记记账


【Python教程】关于pandas和numpy,你还应该知道这些(三)


【Python教程】关于pandas和numpy,你还应该知道这些(四)

数据可视化系列有更新

【Python教程】pandas+matplotlib,数据作图从未如此简单! 


【Python教程】matplotlib中的绘图逻辑 


【Python教程】matplotlib中的画图区域


【Python教程】matplotlib的基础图表类型


【Python教程】matplotlib的图表元素


【Python教程】matplotlib中的通用参数:线型、标记类型和颜色


【Python教程】matplotlib的柱状图


【Python教程】matplotlib的饼图 


【Python教程】matplotlib的直方图


【Python教程】可以将句子拆成字词的jieba模块


【Python教程】番外篇——用Python做一张好看的词云图


【Python教程】matplotlib的箱线图


【Python教程】matplotlib的小提琴图


【Python教程】matplotlib中的误差棒


【Python教程】设置matplotlib的单个元素


【Python教程】让图表的色彩更丰富!matplotlib中的colormap


办公自动化系列

90%的人不曾用过的Word技巧——邮件合并


[Python Tutorial] Automatically generate Word documents with Python


[Python Tutorial] Automatically generate ppt with Python (1)


[Python Tutorial] Automatically generate ppt with Python (2)


[Python Tutorial] Automatically generate ppt with Python (3)


【Python Tutorial】Email with Python


Batch process photos and stop using PS! Use it ten times faster!


[Python Tutorial] Sending Emails with Python (2)


[Python Tutorial] Say goodbye to the dots! Making a Mouse Connector with Python


Swipe left and right to view the next article

picture