Axa Assurance Maroc - Insurer Innovation Award 2024
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
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