Python?命令行?prompt_toolkit?庫詳解
Python 的第三方庫 prompt_toolkit 用于打造交互式命令行,在交互式場景的使用中,prompt_toolkit 具有以下特點(diǎn):
- 語法高亮
- 支持多行編輯
- 支持代碼補(bǔ)全
- 支持自動提示
- 使用鼠標(biāo)移動光標(biāo)
- 支持查詢歷史
- 對 Unicode 支持良好
- 跨平臺
- 支持 Emacs 與 Vi 風(fēng)格的快捷鍵
prompt_toolkit 在使用前需要先進(jìn)行安裝:
pip install prompt_toolkit
一. 使用 Bash 下常用快捷鍵
想必很多開發(fā)者在創(chuàng)建交互式命令行工具時(shí),使用最多的還是 input 和 raw_input 。比如下面的代碼讀取用戶輸入數(shù)據(jù),并進(jìn)行打印:
while True:
# user_input = input('>')
user_input = raw_input('>')
print(user_input)
if user_input.strip().lower() == 'exit':
break上述程序在 Linux 環(huán)境下運(yùn)行時(shí),我們將無法使用任何的 Linux 快捷鍵,甚至在輸入錯(cuò)誤時(shí),按退格刪除內(nèi)容都會出現(xiàn)問題:

下面,我們使用 prompt_toolkit 模塊中的 prompt 函數(shù)改寫上述程序:
from __future__ import print_function
from prompt_toolkit import prompt
while True:
user_input = prompt(u'>>')
print(user_input)運(yùn)行新的程序,你會發(fā)現(xiàn),不僅可以實(shí)現(xiàn)退格刪除,而且可以使用 Bash 下常用的快捷鍵:Ctrl + a 跳轉(zhuǎn)到開頭、Ctrl + e 跳轉(zhuǎn)到末尾、Ctrl + k 刪除光標(biāo)到末尾的內(nèi)容。
二. 實(shí)現(xiàn)查找歷史命令
在 Bash 下,我們可以使用方向鍵中的 ↑ 和 ↓ 查看歷史輸入,或者使用 Ctrl + r 搜索歷史命令:

在 Python 打造的交互式命令行中,使用 prompt_toolkit.history 我們可以很容易實(shí)現(xiàn)查找歷史:
from __future__ import print_function
from __future__ import unicode_literals
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
while True:
user_input = prompt('>>>', history=FileHistory('history.txt'))
print(user_input)運(yùn)行結(jié)果:

上述歷史輸入將被保存至當(dāng)前目錄下的 history.txt 文件中,后續(xù)就可以使用查看或搜索歷史命令了~

三. 根據(jù)歷史輸入自動提示
在上面是示例中我們實(shí)現(xiàn)了查看或搜索歷史輸入的功能,其實(shí)我們還可以更加充分地利用 history.txt 中記載的歷史輸入,在用戶輸入時(shí)進(jìn)行提示。實(shí)現(xiàn)此功能只需要在調(diào)用 prompt 函數(shù)時(shí)指定 auto_suggest 的參數(shù)即可:
from __future__ import print_function
from __future__ import unicode_literals
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
while True:
user_input = prompt('>>>', history=FileHistory('history.txt'),
auto_suggest=AutoSuggestFromHistory())
if user_input.strip().lower() == 'exit':
break
print(user_input)prompt_toolkit 將以暗色字體顯示匹配的歷史輸入:

四. 實(shí)現(xiàn)輸入的自動補(bǔ)全
所謂自動補(bǔ)全,即用戶輸入了關(guān)鍵字的一部分,我們的交互式程序能夠根據(jù)已有的輸入進(jìn)行提示,用戶可以使用 Tab 鍵補(bǔ)全選擇提示的內(nèi)容。以上功能,prompt_toolkit 提供了名為 WorldCompleter 的類來幫助我們實(shí)現(xiàn)。下面我們來模仿 MySQL 客戶端的提示功能:
from __future__ import print_function
from __future__ import unicode_literals
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.contrib.completers import WordCompleter
SQLCompleter = WordCompleter(['select', 'from', 'insert', 'update', 'delete'
'drop'], ignore_case=True)
while True:
user_input = prompt('SQL>', history=FileHistory('history.txt'),
auto_suggest=AutoSuggestFromHistory(),
completer=SQLCompleter)
if user_input.strip().lower() == 'exit':
break
print(user_input)

到此這篇關(guān)于Python 命令行 - prompt_toolkit 庫的文章就介紹到這了,更多相關(guān)Python prompt_toolkit 庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python去除字符串中的空格、特殊字符和指定字符的三種方法
本文主要介紹了python去除字符串中的空格、特殊字符和指定字符的三種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python實(shí)現(xiàn)LSTM學(xué)習(xí)的三維軌跡
這篇文章主要為大家詳細(xì)介紹了如何使用LSTM來學(xué)習(xí)和預(yù)測三維軌跡,并提供詳細(xì)的Python實(shí)現(xiàn)示例,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12
關(guān)于 Python json中l(wèi)oad和loads區(qū)別
這篇文章主要介紹了關(guān)于 Python json中l(wèi)oad和loads區(qū)別,文章也有簡單的說明它們之間的相同點(diǎn),然后詳細(xì)介紹不同點(diǎn),需要的朋友可以參考一下文章的具體內(nèi)容2021-11-11
python數(shù)字圖像處理之估計(jì)噪聲參數(shù)
這篇文章主要介紹了python數(shù)字圖像處理之估計(jì)噪聲參數(shù),圖像復(fù)原與重建,想了解圖像處理的同學(xué),一定要好好看看2021-04-04
Python還能這么玩之只用30行代碼從excel提取個(gè)人值班表
公司實(shí)行項(xiàng)目值班制度,拿到值班表,看到全部的值班信息,要去查找自己的值班信息,是一件頭痛的事情.作為程序員,當(dāng)然要簡化,將自己的信息提煉出來,需要的朋友可以參考下2021-06-06

