Python Prompt Toolkit:構建強大交互式命令行的 Python 工具庫
prompt_toolkit
是一個用於構建強大交互式命令行的 Python 工具庫。
你是不是在找交互式的 Python shell 工具 ptpython
呢?我們把 ptpython
的源碼轉移到了一個獨立的倉庫。如此一來,我們確信 prompt_toolkit
庫不會被其他 ptpython
東西“污染”,並且 ptpython
也可以獨立開發。現在必須用下面這個命令安裝 ptpython
:
pip install ptpython
prompt-toolkit 的特性features
prompt_toolkit
可用作 GNU readline 的替代品,但其實比后者還好得多。
部分特性:
-
純 Python;
-
輸入時支持語法高亮;(有 Pygments 詞法分析器)
-
支持多行輸入編輯;
-
高級代碼補全;
-
支持 Emacs 和 Vi 熱鍵綁定;(和 readline 類似)
-
支持反向和正向的增量搜索;
-
支持 Python 2.6 到 3.4 之間的版本;
-
支持 Unicode 雙寬字符;(比如:中文輸入)
-
選擇文本復制/粘貼 (Both Emacs and Vi style.)
-
多輸入緩存
-
輕量級,只依賴於 Pygments、six 和 wcwidth;
-
Code written with love.
-
支持 Linux、OS X、OpenBSD 和 Windows 系統;
安裝
pip install prompt-toolkit
getting started
先來個簡單的示例:
from prompt_toolkit.shortcuts import get_input
if __name__ == '__main__':
answer = get_input('Give me some input: ')
print('You said: %s' % answer)
復雜的示例,查看 examples
目錄吧。所選示例都是直桿一件事的。不要怕看源碼哦。 get_input
函數的實現,是個不錯的開始。
注意:在 Python 2 下,你需要往上述例子添加 from __future__ import unicode_literals
。所有字符會看作是 unicode 字符。
使用 prompt-toolkit 的開源項目
-
ptpython: Python REPL
-
ptpdb: Python 調試器 (pdb 替代品)
-
pgcli: Postgres 客戶端.
-
mycli: MySql 客戶端.
-
pyvim: 純 Python 實現的 Vim 克隆版
-
wharfee: Docker 命令行工具
- 新智雲官網 www.enncloud.cn