Using the curses library to view IoT data
Curses Windows
The examples so far have been based on a main screen object, but the curses library also supports windows. Windows are useful because they support border outlines, and text can be cleared from and written to windows without affecting the main screen object. The syntax to create a curses window object is:
mynewwindow = curses.newwin( height, width, begin_y, begin_x)
The code for Figure 3,
# define a win1 window object win1 = curses.newwin(9, 44, 6, 4) # write text inside the window object win1.addstr(8,0, "Sensor 1: Temperature Reading", curses.A_BOLD) value1 = pyfiglet.figlet_format( "23 C", font = "doom") win1.addstr(0,0,value1,curses.color_pair(2) )
produces two curses windows. The major difference in the code is that information within a window is addressed by the window object instead of the background screen object.
Dynamic Bars
Simple progress or indicator bars can be created by a curses window with a border (Listing 4). The bar itself is generated by writing a space character with inverse video (Figure 4).
Listing 4
bar.py
01 import curses 02 03 bar = ' ' # with reverse video a space will show up 04 value1 = 10 # in real life this needs to be scaled 05 06 stdscr = curses.initscr() 07 curses.curs_set(0) # don't show the cursor 08 09 stdscr.addstr(1,3, "Python Curses Bar") 10 stdscr.refresh() 11 # Define windows 12 win1 = curses.newwin(3, 32, 3, 2) 13 win1.border(0) # add a border 14 # a horizontal bar 10 characters wide 15 win1.addstr(1, 1, bar * value1,curses.A_REVERSE ) 16 win1.refresh() 17 18 # Wait for a key press then exit 19 stdscr.getch() 20 curses.endwin() # restore the terminal to original settings
After I had the basic curses bar working, I was able to use what I learned in the earlier examples to create Raspberry Pi interfaces that included color and large text (Figure 5).
Summary
If you are looking for a quick and easy way to present data, without the bother of a GUI, curses is a great solution. Curses is supported in a variety of programming languages. I focused my curses work here on C and Python, but I've also had good success in Lua.
I only had one presentation issue and that was with line drawing characters like borders when I used Putty (a Windows-based SSH program). To fix this issue, I changed the Putty Window | Translation setting to use the VSCII character set; then, everything looked good.
Infos
- Python curses: https://docs.python.org/3/library/curses.html
- Code for this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/232/
- pyfiglet: https://github.com/pwaller/pyfiglet
- FIGlet fonts: http://www.figlet.org/examples.html
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
PipeWire 1.0 Officially Released
PipeWire was created to take the place of the oft-troubled PulseAudio and has finally reached the 1.0 status as a major update with plenty of improvements and the usual bug fixes.
-
Rocky Linux 9.3 is Available for Download
The latest version of the RHEL alternative is now available and brings back cloud and container images for ppc64le along with plenty of new features and fixes.
-
Ubuntu Budgie Shifts How to Tackle Wayland
Ubuntu Budgie has yet to make the switch to Wayland but with a change in approaches, they're finally on track to making it happen.
-
TUXEDO's New Ultraportable Linux Workstation Released
The TUXEDO Pulse 14 blends portability with power, thanks to the AMD Ryzen 7 7840HS CPU.
-
AlmaLinux Will No Longer Be "Just Another RHEL Clone"
With the release of AlmaLinux 9.3, the distribution will be built entirely from upstream sources.
-
elementary OS 8 Has a Big Surprise in Store
When elementary OS 8 finally arrives, it will not only be based on Ubuntu 24.04 but it will also default to Wayland for better performance and security.
-
OpenELA Releases Enterprise Linux Source Code
With Red Hat restricting the source for RHEL, it was only a matter of time before those who depended on that source struck out on their own.
-
StripedFly Malware Hiding in Plain Sight as a Cryptocurrency Miner
A rather deceptive piece of malware has infected 1 million Windows and Linux hosts since 2017.
-
Experimental Wayland Support Planned for Linux Mint 21.3
As with most Linux distributions, the migration to Wayland is in full force. While some distributions have already made the move, Linux Mint has been a bit slower to do so.
-
Window Maker Live 0.96.0-0 Released
If you're a fan of the Window Maker window manager, there's a new official release of the Linux distribution that champions the old-school user interface.