I'm writing a python script to pull out hit data for items in a MySQL db and then write that data to a csv file. I'd like the program to prompt the user to save this csv file on their computer (I'm using Tkinter for this). The script all works fine on my local machine but when I try to implement it using CGI or even just placing it on a remote server with all of the packages installed (non-CGI), I get the following error:
Traceback (most recent call last):
File "ct_hits.py", line 29, in <module>
root = Tkinter.Tk()
File "/usr/lib64/python2.6/lib-tk/Tkinter.py", line 1643, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
The essence of the script is as follows:
#!/usr/bin/python
import MySQLdb
import csv
import getpass
import Tkinter, tkFileDialog, Tkconstants
uname = raw_input("Database user id: ")
pword = getpass.getpass("Database password: ")
try:
db = MySQLdb.connect("mysqldbservername",uname,pword,"databasename")
except:
print "Invalid username and/or password!"
quit()
formats = [('Comma Separated values', '*.csv'), ]
root = Tkinter.Tk()
file_name = tkFileDialog.asksaveasfilename(parent=root, filetypes=formats, title="Save as...",defaultextension='.csv')
writer = csv.writer(open(file_name, 'w'))
writer.writerow(["Hits for date range:", str(date_start) + " to " + str(date_end)])
writer.writerow(["ID:", "Title:", "Hits:"])
I've searched and seen a few other posts that mention similar errors but these involve matplotlib (which I'm not using): Generating a PNG with matplotlib when DISPLAY is undefined
Or possibly making adjustments to the $DISPLAY/ENV variables or variations in X (all of which I'm pretty unfamiliar with).
Eventually I'd like the program to be accessible to other users so I'm wondering what adjustments I need to make to the script (are there other options besides Tkinter?). Or do I need to make adjustments to environment or path variables on the server? Thanks.