Create TUI on python

Hello, Habr! In this article I'll talk about
npyscreen
- a library for creating text interfaces for terminal and console applications.
 
Create TUI on python
 
site with the documentation .
 
Let's write Hello World
 
It is convenient to create forms by inheriting them from the built-in classes. Thus, you can override the built-in methods to extend the functionality of the application.
 
Here is a simple Hello World [/b]
#! /usr /bin /env python3
import npyscreen
class App (npyscreen.StandardApp):
def onStart (self):
self.addForm ("MAIN", MainForm, name = "Hello Habr!")
class MainForm (npyscreen.ActionForm):
# Constructor
def create (self):
# Add the TitleText widget to the form
self.title = self.add (npyscreen.TitleText, name = "TitleText", value = "Hello World!")
# an override method that triggers when the "ok" button
is clicked. def on_ok (self):
self.parentApp.setNextForm (None)
# an overridden method that triggers when the cancel button is pressed
def on_cancel (self):
self.title.value = "Hello World!"
MyApp = App ()
MyApp.run ()

 

 
 
The arrangement of the elements is
 
By default, widgets occupy the maximum possible space.
 
To specify the exact coordinates, you need to set the parameters:
 
 
relx
,
rely
- the position of the widget relative to the origin of the form.
 
width
,
height
,
max_width
,
max_height
- Limit the size of the widget.
 
 
Example [/b]
#! /usr /bin /env python3
import npyscreen
class App (npyscreen.StandardApp):
def onStart (self):
self.addForm ("MAIN", MainForm, name = "Hello Habr!")
class MainForm (npyscreen.FormBaseNew):
def create (self):
# Recognize the space used by the form
y, x = self.useable_space ()
self.add (npyscreen.TitleDateCombo, name = "Date:", max_width = x //2)
self.add (npyscreen.TitleMultiSelect, relx = x //2 + ? rely = ? value =[1, 2], name = "Pick Several", values ​​=["Option1", "Option2", "Option3"], scroll_exit = True)
# You can use the negative coordinates
self.add (npyscreen.TitleFilename, name = "Filename:", rely = -5)
MyApp = App ()
MyApp.run ()

 

 
 
Boxes and custom colors
 
To make a wrapper in the form of a box is simple - you need to create a class inherited from
BoxTitle
and override the attribute
_contained_widget
, putting there the widget that will be inside.
 
In
npyscreen
Several built-in color themes are available. If you want, you can add your own. You can install them using the
method. setTheme
.
 
With the adjustment of the color of the text, everything is slightly more complicated. I had expand the functional library to make it work.
 
Example [/b]
#! /usr /bin /env python3
from src import npyscreen
import random
class App (npyscreen.StandardApp):
def onStart (self):
# Set the theme. DefaultTheme
is used by default. npyscreen.setTheme (npyscreen.Themes.ColorfulTheme)
self.addForm ("MAIN", MainForm, name = "Hello Habr!")
class InputBox (npyscreen.BoxTitle):
# MultiLineEdit will now be surrounded by box
_contained_widget = npyscreen.MultiLineEdit
class MainForm (npyscreen.FormBaseNew):
def create (self):
y, x = self.useable_space ()
obj = self.add (npyscreen.BoxTitle, name = "BoxTitle",
custom_highlighting = True, values ​​=["first line", "second line"],
.us = y //? max_width = x //2 - ? max_height = y //2)
self.add (InputBox, name = "Boxed MultiLineEdit", footer = "footer",
relx = x //? rely = 2)
color1 = self.theme_manager.findPair (self, 'GOOD')
color2 = self.theme_manager.findPair (self, 'WARNING')
color3 = self.theme_manager.findPair (self, 'NO_EDIT')
color_list =[color1, color2, color3]
first_line_colors =[random.choice(color_list) for i in range(len("first line"))]
second_line_colors =[random.choice(color_list) for i in range(len("second"))]
# Fill the lines with custom colors
obj.entry_widget.highlighting_arr_color_data =[first_line_colors, second_line_colors]
MyApp = App ()
MyApp.run ()

 

 
 
Events and handlers
 
Class
StandardApp
npyscreen supports the event queue.
 
As a treatment for pressing, the method
is used. add_handlers
.
 
Example [/b]
#! /usr /bin /env python3
import npyscreen
import curses
class App (npyscreen.StandardApp):
def onStart (self):
self.addForm ("MAIN", MainForm, name = "Hello Habr!")
class InputBox1 (npyscreen.BoxTitle):
_contained_widget = npyscreen.MultiLineEdit
def when_value_edited (self):
self.parent.parentApp.queue_event (npyscreen.Event ("event_value_edited"))
class InputBox2 (npyscreen.BoxTitle):
_contained_widget = npyscreen.MultiLineEdit
class MainForm (npyscreen.FormBaseNew):
def create (self):
self.add_event_hander ("event_value_edited", self.event_value_edited)
new_handlers = {
# Set ctrl + Q for the output of
"^ Q": self.exit_func,
# Set alt + enter to clear inputbox
curses.ascii.alt (curses.ascii.NL): self.inputbox_clear
}
self.add_handlers (new_handlers)
y, x = self.useable_space ()
self.InputBox1 = self.add (InputBox? name = "Editable", max_height = y //2)
self.InputBox2 = self.add (InputBox? footer = "No editable", editable = False)
def event_value_edited (self, event):
self.InputBox2.value = self.InputBox1.value
self.InputBox2.display ()
def inputbox_clear (self, _input):
self.InputBox1.value = self.InputBox2.value = ""
self.InputBox1.display ()
self.InputBox2.display ()
def exit_func (self, _input):
exit (0)
MyApp = App ()
MyApp.run ()

 

 
 
References:
 
Official documentation
 
The original source code is
 
The repository I updated (the main githab seems to have died)
 
The telegram client is on npyscreen (which is on the first screenshot)
+ +1 -

Comments 11

Offline
Dave
Dave 16 May 2018 15:56
Thanks for your article, there's some information here that I have not seen elsewhere. I have been trying to run you examples on my PC to help me to learn more about npyscreen but am having trouble with syntax errors - I think because the format of the code on the page is not good. Please send me the raw code or a link to the code so I can try you examples.
Offline
Kevin
Kevin 23 July 2018 13:20
Hi there, 

good article - really got me convinced to use this lib. 
I've tried it and it works really nicely. However, I wanted to build a personal website, with different tools/personlized apps. Since I am pretty new to python, I wanted to ask if I use this as the UI for my web-application in Django?

Best regards, 
Kevin
Offline
Rey Sterling
Rey Sterling 9 December 2018 18:37
I guess that development on programming or python starts with best term paper writing service. If people can understand what they need from here, they'll be happy to understand this thing also.
Offline
HARRISON BOOTH
HARRISON BOOTH 27 December 2018 18:19
Fantastic submit,  Many thanks regarding revealing This kind of information. Fantasticly  composed write-up, only if almost all people supplied the identical amount of  articles when you, the net would have been a greater spot. You should keep  writing! Accountants Crawley
Offline
Sunny Deol
Sunny Deol 7 January 2019 11:44
Whilst we obtained in your internet record nevertheless including attention just a little contact submits. Good way of possible, We're book-marking throughout a time period discover variations determine spgs method upward. payday loan consolidation company

Offline
sad
sad 8 January 2019 10:30
I’m the sucker with regard to the majority of the content pieces, We completely savored, I'd truly choose much more information concerning this particular, considering the fact that it is good., Thanks intended for publishing. ทางเข้าufabet 

Offline
Anna Sally
Anna Sally 11 January 2019 19:28
You know your  projects stand out of the herd. There is something special about them. It  seems to me all of them are really brilliant!valentines  day wishes
Offline
Anna Sally
Anna Sally 12 January 2019 09:59
It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing. 토토

Offline
Anna Sally
Anna Sally 14 January 2019 15:46
Excellent post. I was reviewing this blog continuously, and I am impressed! Extremely helpful information especially this page. Thank you and good luck. Hair  growth products

Offline
Anna Sally
Anna Sally 14 January 2019 20:01
I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. etisalat  speed test
Offline
charlos john
charlos john Yesterday, 09:57
Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.www.centrales-vapeur.fr

Add comment