SlideShare a Scribd company logo
Hello, I’m MASASHI.
Live Broadcasting Engineer in Japan.
c-bata c_bata_! "
I love Django, Pandas and prompt-toolkit ❤
How to develop
a rich terminal UI application.
#pyconmy #pyconapac2017
What is python-prompt-toolkit.
go-prompt
golang port is published at:
https://github.com/c-bata/go-prompt
1. How to receive a keyboard input
2. How to control the terminal output
Two things you need to know
when you develop a rich terminal UI app.
How to receive a keyboard input
$ cat hello.py
x = input()
print(x)
$ python3 hello.py
Hello World
Hello World
input() function
Most basic way but some special characters are interpreted

ex) Ctrl+A, Ctrl+E, F1, F2, etc…
How to receive a keyboard input
$ cat hello.py
x = input()
print(x)
$ python3 hello.py
Hello World
Hello World
input() function
Most basic way but some special characters are interpreted

ex) Ctrl+A, Ctrl+E, F1, F2, etc…
Your input interpreted some
special characters.
e.g. Ctrl+A, Ctrl+E, F1, F2, …
How to receive a keyboard input
$ cat hello.py
x = input()
print(x)
$ python3 hello.py
Hello World
Hello World
input() function
Most basic way but some special characters are interpreted

ex) Ctrl+A, Ctrl+E, F1, F2, etc…
And this way cannot receive immediately
when user press a something key.
How to receive a keyboard input
passes the data as-is to the program without interpreting

any of the special characters.
Raw mode input
See https://github.com/jonathanslenders/python-prompt-
toolkit/blob/master/prompt_toolkit/terminal/vt100_input.py
How to change the Raw Mode.
UNIX system calls tcgetattr and tcsetattr is available

for manipulating termios structure provided by termios
package in standard library.
import termios
def tty_raw(fd): # put terminal into a raw mode
buf = termios.tcgetattr(fd)
buf[3] &= ~(termios.ECHO|termios.ICANON|termios.IEXTEN|
termios.ISIG)
buf[0] &= ~(termios.BRKINT|termios.ICRNL|termios.INPCK|
termios.ISTRIP|termios.IXON)
buf[3] &= ~(termios.CSIZE|termios.PARENB)
buf[3] |= termios.CS8
buf[1] &= ~(termios.OPOST)
buf[6][termios.VMIN] = 1
buf[6][termios.VTIME] = 0
termios.tcsetattr(fd, termios.TCSAFLUSH, buf)
1. How to receive a keyboard input
2. How to control the terminal output
Two things you need to know
when you develop a rich terminal UI app.
VT100 was the first of digital terminal.

It can export bold text, italic text,

blinking text and underline in specific words.
What is “VT100 Escape Sequences”
https://en.wikipedia.org/wiki/VT100
What is “Escape Sequences”
What is “Escape Sequences”
Show underline
Change background color to Cyan
Change text color to Black
http://www.termsys.demon.co.uk/vtansi.htm
What is “Escape Sequences”
print “Hello”
What is “Escape Sequences”
Reset all attributes.
Summary
Raw mode
VT100 escape sequences
Happy terminal hacking ;)
TERIMA KASIH
Thank you for listening :)

More Related Content

PDF
Golangにおける端末制御 リッチなターミナルUIの実現方法
PDF
Ctf hello,world!
PDF
Learning go for perl programmers
PDF
PDF
Introduction to Go programming language
PDF
Go Lang Tutorial
PDF
LTO plugin
PPT
Dynamic data encryption using the concepts of ANN
Golangにおける端末制御 リッチなターミナルUIの実現方法
Ctf hello,world!
Learning go for perl programmers
Introduction to Go programming language
Go Lang Tutorial
LTO plugin
Dynamic data encryption using the concepts of ANN

What's hot (20)

PDF
Instruction Combine in LLVM
PDF
D vs OWKN Language at LLnagoya
PPTX
Go Language Hands-on Workshop Material
PDF
Donnez des couleurs a votre terminal
PDF
Coding in GO - GDG SL - NSBM
PDF
Phttp://www.cw360ms.com/pmsurveyresults/index.aspda
PDF
Introduction to go language programming
PDF
Defer, Panic, Recover
PDF
Taming Rich GML with Stetl - FOSS4G 2013 Nottingham
PDF
Stetl for INSPIRE Data Transformation
ODP
ocelot
PDF
Geospatial ETL with Stetl - GeoPython 2016
PDF
JavaForum Nord 2021: Java to Go - Google Go für Java-Entwickler
PDF
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
PDF
Why my Go program is slow?
DOC
Restore
TXT
Script
PDF
5 Minute Intro to Stetl
PDF
Porting Gentoo to DragonFly
PDF
Introduction to Go language
Instruction Combine in LLVM
D vs OWKN Language at LLnagoya
Go Language Hands-on Workshop Material
Donnez des couleurs a votre terminal
Coding in GO - GDG SL - NSBM
Phttp://www.cw360ms.com/pmsurveyresults/index.aspda
Introduction to go language programming
Defer, Panic, Recover
Taming Rich GML with Stetl - FOSS4G 2013 Nottingham
Stetl for INSPIRE Data Transformation
ocelot
Geospatial ETL with Stetl - GeoPython 2016
JavaForum Nord 2021: Java to Go - Google Go für Java-Entwickler
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
Why my Go program is slow?
Restore
Script
5 Minute Intro to Stetl
Porting Gentoo to DragonFly
Introduction to Go language
Ad

Similar to How to develop a rich terminal UI application (20)

PDF
Introduction to Python Programming | InsideAIML
PPTX
PPTX
Python Course Basic
PPTX
slides1-introduction to python-programming.pptx
ODP
Python 3000
PDF
Python PPT1.pdf
PDF
Python-GTK
PPT
Python Programming Introduction demo.ppt
PDF
Introduction to Python
PPT
Lec 47.48 - stream-files
PPTX
Python_Haegl.powerpoint presentation. tx
PPTX
Introduction to Python Programming
PPTX
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
PDF
Python build your security tools.pdf
PPTX
Introduction to Python.Net
DOCX
System programmin practical file
PDF
Class 1: Welcome to programming
PPT
Python ppt
PPTX
Python knowledge ,......................
PDF
Python GTK (Hacking Camp)
Introduction to Python Programming | InsideAIML
Python Course Basic
slides1-introduction to python-programming.pptx
Python 3000
Python PPT1.pdf
Python-GTK
Python Programming Introduction demo.ppt
Introduction to Python
Lec 47.48 - stream-files
Python_Haegl.powerpoint presentation. tx
Introduction to Python Programming
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Python build your security tools.pdf
Introduction to Python.Net
System programmin practical file
Class 1: Welcome to programming
Python ppt
Python knowledge ,......................
Python GTK (Hacking Camp)
Ad

More from Masashi Shibata (20)

PDF
MLOps Case Studies: Building fast, scalable, and high-accuracy ML systems at ...
PDF
実践Djangoの読み方 - みんなのPython勉強会 #72
PDF
CMA-ESサンプラーによるハイパーパラメータ最適化 at Optuna Meetup #1
PDF
サイバーエージェントにおけるMLOpsに関する取り組み at PyDataTokyo 23
PDF
Implementing sobol's quasirandom sequence generator
PDF
DARTS: Differentiable Architecture Search at 社内論文読み会
PDF
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
PDF
PythonとAutoML at PyConJP 2019
PDF
Djangoアプリのデプロイに関するプラクティス / Deploy django application
PDF
Django REST Framework における API 実装プラクティス | PyCon JP 2018
PDF
Django の認証処理実装パターン / Django Authentication Patterns
PDF
RTMPのはなし - RTMP1.0の仕様とコンセプト / Concepts and Specification of RTMP
PDF
システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)
PDF
Introduction of Feedy
PDF
Webフレームワークを作ってる話 #osakapy
PDF
Pythonのすすめ
PDF
pandasによるデータ加工時の注意点やライブラリの話
PDF
Pythonistaのためのデータ分析入門 - C4K Meetup #3
PDF
テスト駆動開発入門 - C4K Meetup#2
PDF
Introduction of PyCon JP 2015 at PyCon APAC/Taiwan 2015
MLOps Case Studies: Building fast, scalable, and high-accuracy ML systems at ...
実践Djangoの読み方 - みんなのPython勉強会 #72
CMA-ESサンプラーによるハイパーパラメータ最適化 at Optuna Meetup #1
サイバーエージェントにおけるMLOpsに関する取り組み at PyDataTokyo 23
Implementing sobol's quasirandom sequence generator
DARTS: Differentiable Architecture Search at 社内論文読み会
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
PythonとAutoML at PyConJP 2019
Djangoアプリのデプロイに関するプラクティス / Deploy django application
Django REST Framework における API 実装プラクティス | PyCon JP 2018
Django の認証処理実装パターン / Django Authentication Patterns
RTMPのはなし - RTMP1.0の仕様とコンセプト / Concepts and Specification of RTMP
システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)
Introduction of Feedy
Webフレームワークを作ってる話 #osakapy
Pythonのすすめ
pandasによるデータ加工時の注意点やライブラリの話
Pythonistaのためのデータ分析入門 - C4K Meetup #3
テスト駆動開発入門 - C4K Meetup#2
Introduction of PyCon JP 2015 at PyCon APAC/Taiwan 2015

Recently uploaded (20)

PPTX
Spectroscopy.pptx food analysis technology
PPTX
Big Data Technologies - Introduction.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
KodekX | Application Modernization Development
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation theory and applications.pdf
Spectroscopy.pptx food analysis technology
Big Data Technologies - Introduction.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
Understanding_Digital_Forensics_Presentation.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Spectral efficient network and resource selection model in 5G networks
Per capita expenditure prediction using model stacking based on satellite ima...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Encapsulation_ Review paper, used for researhc scholars
Review of recent advances in non-invasive hemoglobin estimation
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Network Security Unit 5.pdf for BCA BBA.
KodekX | Application Modernization Development
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation theory and applications.pdf

How to develop a rich terminal UI application

  • 1. Hello, I’m MASASHI. Live Broadcasting Engineer in Japan. c-bata c_bata_! " I love Django, Pandas and prompt-toolkit ❤ How to develop a rich terminal UI application. #pyconmy #pyconapac2017
  • 3. go-prompt golang port is published at: https://github.com/c-bata/go-prompt
  • 4. 1. How to receive a keyboard input 2. How to control the terminal output Two things you need to know when you develop a rich terminal UI app.
  • 5. How to receive a keyboard input $ cat hello.py x = input() print(x) $ python3 hello.py Hello World Hello World input() function Most basic way but some special characters are interpreted ex) Ctrl+A, Ctrl+E, F1, F2, etc…
  • 6. How to receive a keyboard input $ cat hello.py x = input() print(x) $ python3 hello.py Hello World Hello World input() function Most basic way but some special characters are interpreted ex) Ctrl+A, Ctrl+E, F1, F2, etc… Your input interpreted some special characters. e.g. Ctrl+A, Ctrl+E, F1, F2, …
  • 7. How to receive a keyboard input $ cat hello.py x = input() print(x) $ python3 hello.py Hello World Hello World input() function Most basic way but some special characters are interpreted ex) Ctrl+A, Ctrl+E, F1, F2, etc… And this way cannot receive immediately when user press a something key.
  • 8. How to receive a keyboard input passes the data as-is to the program without interpreting any of the special characters. Raw mode input See https://github.com/jonathanslenders/python-prompt- toolkit/blob/master/prompt_toolkit/terminal/vt100_input.py
  • 9. How to change the Raw Mode. UNIX system calls tcgetattr and tcsetattr is available for manipulating termios structure provided by termios package in standard library. import termios def tty_raw(fd): # put terminal into a raw mode buf = termios.tcgetattr(fd) buf[3] &= ~(termios.ECHO|termios.ICANON|termios.IEXTEN| termios.ISIG) buf[0] &= ~(termios.BRKINT|termios.ICRNL|termios.INPCK| termios.ISTRIP|termios.IXON) buf[3] &= ~(termios.CSIZE|termios.PARENB) buf[3] |= termios.CS8 buf[1] &= ~(termios.OPOST) buf[6][termios.VMIN] = 1 buf[6][termios.VTIME] = 0 termios.tcsetattr(fd, termios.TCSAFLUSH, buf)
  • 10. 1. How to receive a keyboard input 2. How to control the terminal output Two things you need to know when you develop a rich terminal UI app.
  • 11. VT100 was the first of digital terminal. It can export bold text, italic text, blinking text and underline in specific words. What is “VT100 Escape Sequences” https://en.wikipedia.org/wiki/VT100
  • 12. What is “Escape Sequences”
  • 13. What is “Escape Sequences” Show underline Change background color to Cyan Change text color to Black http://www.termsys.demon.co.uk/vtansi.htm
  • 14. What is “Escape Sequences” print “Hello”
  • 15. What is “Escape Sequences” Reset all attributes.
  • 16. Summary Raw mode VT100 escape sequences Happy terminal hacking ;)
  • 17. TERIMA KASIH Thank you for listening :)