Rick_S
Posts: 10
Joined: Thu Jan 26, 2012 12:36 am

Python newbie help with curses

Sat Apr 12, 2014 1:20 pm

Hello, I'm a 49 yr old who is just now starting to dabble in python. I've programmed in GW-Basic and Visual Basic for DOS decades ago, and lightly learned C on to program AVR microcontrollers.

My goals with this program are these:

1) Load multiple comma delimited (Excel saved .csv files)
2) Format them on a text based screen
3) Display each file a page at a time in rotation.
4) Set this program to start automatically upon power up of the Rasberry Pi.

The csv files would be stored in a shared folder on the PI and could be edited to update their contents at any time from another computer (Windows based) on the network using a samba share.

I have created a program that will read the csv from 1 file and display a single page using curses. Here is the program I am currently working on.

Code: Select all

import curses
import sys
import csv
import time

loopholder = 1

def mycsv(myscreen):
	myscreen = curses.initscr()
	myscreen.nodelay(1)
	curses.noecho()
	curses.curs_set(0)

	curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
	curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK)
	curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
	
	while(loopholder == 1):
		c = myscreen.getch()
		if c == ord('q'):
			break
		else:
			myscreen.clear()
			myscreen.border(0)
			myscreen.addstr(2, 2, '----------------------------------------------------------------------------')
			i = 1
			cl = 1
			with open("PI_SHARE/test.csv","rb") as f:
				theline = csv.reader(f)
				for row in theline:
					myscreen.addstr(i, 2, row[0][:10].ljust(6,' ').title(), curses.color_pair(cl))
					myscreen.addstr(i, 9, row[1].ljust(6,' ').title(), curses.color_pair(cl))
					myscreen.addstr(i, 16, row[2].ljust(10,' ').title(), curses.color_pair(cl))
					myscreen.addstr(i, 27, row[3].ljust(25,' ').title(), curses.color_pair(cl))
					myscreen.addstr(i, 53, row[4].ljust(8,' ').title(), curses.color_pair(cl))
					myscreen.addstr(i, 62, row[5].ljust(10,' ').title(), curses.color_pair(cl))
					myscreen.addstr(i, 73, row[6].ljust(5,' ').title(), curses.color_pair(cl))
					i = i + 1
					if i == 2:
						i = i + 1
					cl = cl + 1
					if cl == 4:
					  cl = 2
			myscreen.refresh()
			time.sleep(5)
		
curses.wrapper(mycsv)
This is the csv file it reads.

Code: Select all

Job#,Quote#,Company,Description,Due Date,Status,Who
131095,17654,Mittal,Downender Cradle,2/25,Prod,Rick
140012,17856,Nucor,TENSIONER Reel,3/3,Prod,Tony
140013,17999,Mittal,Turret Screw Jack,2/15,In Process,Rick
140026,17685,Stl Whs,Arbor shaft,12/12/14,Prod,Rick
The output looks something like this.

Image

I don't know how in python to store the contents of what I've read from the files into arrays for each page, then 'create' individual screens with curses to rotate.

I'm also not sure what the best way to run this at power up. I've read several different methods, some of which I've tried don't work.

Any suggestions would be greatly appreciated.

Thanks,

Rick

User avatar
paddyg
Posts: 2501
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Python newbie help with curses

Sat Apr 12, 2014 4:57 pm

Rick, If the files are small then you could read and store the parsed files in a list. (If they are big or might change then it would make sense to read them from file) i.e. before your while loop do something like

Code: Select all

myfiles = ["PI_SHARE/test1.csv", "PI_SHARE/test2.csv", "PI_SHARE/test3.csv"]
readfiles = []
for i, fname in enumerate(myfiles):
  with open(fname, "rb") as f:
    theline = csv.reader(f)
    readfiles.append([row for row in theline])
then in you while loop

Code: Select all

     while(loopholder == 1):
      for readfile in readfiles:
..
         i = 1
         cl = 1
         for row in readfile:
            myscreen.addstr(i, 2, row[0][:10].ljust(6,' ').title(), curses.color_pair(cl))

NB I haven't tried this and it might be full of syntax errors but it indicates the kind of thing I would look at doing!
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Rick_S
Posts: 10
Joined: Thu Jan 26, 2012 12:36 am

Re: Python newbie help with curses

Sat Apr 12, 2014 5:12 pm

That gives me some ideas, thanks. This is meant to be a display board in a machine shop I work at. I want to read the work schedule for seven different machine centers and display each machines info on a page in rotation. Each machine center will have it's data in a csv file that I will read at most 1 page worth of text from. I would like to re-read the files probably once a minute to refresh the data. The csv files will be updated by the shop schedulers using a VB for applications program I will write in their Excel spreadsheet they currently use. That way the updating for the shop display will be transparent to the supervisors as they will just be doing what they have been doing when making their schedules.

Thanks again for the input, I now have some more food for thought.

BillBodkin
Posts: 2
Joined: Sat May 24, 2014 4:10 pm

Re: Python newbie help with curses

Sat May 24, 2014 4:13 pm

color=#0000BF]How do I get this module from the command line?[[/color]

User avatar
DougieLawson
Posts: 37732
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Python newbie help with curses

Sun May 25, 2014 8:07 am

I think you should use an SQL database that could be SQLite3 or MySQL.

One task for each machine would periodically write records to the database. Your display task would read the last record for each machine and display it.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Python newbie help with curses

Sun May 25, 2014 9:14 am

BillBodkin wrote:How do I get this module from the command line?
This seems to be unrelated to any previous discussion in this thread. Which module? Since 'modules' are usually chunks of python code, does it make sense to be using the command line?
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

Rick_S
Posts: 10
Joined: Thu Jan 26, 2012 12:36 am

Re: Python newbie help with curses

Sun May 25, 2014 10:59 am

Thank you everyone for your input. I have everything working to the satisfaction of the plant owner. Essentially what it boils down to is this:

The shift supervisors were already using an Excel spreadsheet to create the work list for the shop. I wrote a Visual Basic for Applications program that would take each work centers work list and output it in CSV format. These files were saved through the network on a share on the Raspberry Pi.

On the Pi, I read each file individually and display the work list. If there is no work for a given machine, I just skip ahead to the next so as not to pause on a screen without information. Once all machines have been gone through, I then display a page of all work centers without scheduled work. I've even thrown in a screen with local weather, and an optional screen(s) that only show if there are severe weather alerts. So far it has been running a few weeks without problems.

I setup the Pi so it automatically logs in and runs the program on startup. I also setup crontabs to run the background program that polls for the weather and writes it to a file.

I'm sure this could all be polished up and be much tighter cleaner code, but I'm pretty pleased considering a couple months ago, I was a complete newbie to Linux and Python. Just goes to show at 49yrs old, I still have a lot of learning to do.

Thanks again everyone, and thanks foundation for making this great little VERY AFFORDABLE learning tool.

Rick

BillBodkin
Posts: 2
Joined: Sat May 24, 2014 4:10 pm

Thu Jun 19, 2014 5:46 pm

This seems to be unrelated to any previous discussion in this thread. Which module? Since 'modules' are usually chunks of python code, does it make sense to be using the command line?
soz, i cant rember :?: what i ment, that was a while back.
Ill sleep on it.

Return to “Python”