calendar from python to html

Jason Benjamin hexusnexus at gmail.com
Fri Oct 5 22:20:03 EDT 2012


Well, you need a web server, a webpage, a database (could just be a 
file), a cgi script, and the datetime module.  Optionally, you can use a 
web framework like CherryPy or Django, which covers a lot of these by 
itself.

I only know Python 2, but here are some examples:

A basic web server:

webdir = '.'
port = 80

import os, sys
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler

if len(sys.argv) > 1: webdir = sys.argv[1]
if len(sys.argv) > 2: port = int(sys.argv[2])
print 'webdir "%s", port %s' % (webdir, port)

#Windows only hack
if sys.platform[:3] == 'win':
     CGIHTTPRequestHandler.have_popen2 = False
     CGIHTTPRequestHandler.have_popen3 = False
     sys.path.append('cgi-bin')

os.chdir(webdir)
srvraddr = ("", port)
srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
srvrobj.serve_forever()


Start the script in the same directory as the cgi script and HTML.
Assuming you have a file that holds '1' time per date, you could
write a program to pickle and unpickle dictionaries that
are derived from form data:

import pickle
import cgi

file = open('dates.pkl', 'rb')
mydates = pickle.load(file)
file.close()

html = """
<html>
<body>
<h1>Schedule</h1>
<form method=POST action="yourscript.py">
<table border=1>
<tr>
<td><input type=text name=april01 value=%(april01)s></td>
<td><input type=text name=april02 value=%(april02)s></td>
</tr>
<tr>
<td><input type=text name=april03 value=%(april03)s></td>
<td><input type=text name=april04 value=%(april04)s></td>
</tr>
</table>
<input type=hidden name=submitted value=done>
<p><input type=submit>
</form>
</body>
</html>
"""

dates = ['april01', 'april02', 'april03', 'april04']
if form.has_key('submitted'):
     newdates = {}
     for d in dates:
         if form.has_key(d):
             newdates[d] = form[d].value
         else:
             newdates[d] = ''
     mydates = newdates
     output = open('dates.pkl', 'wb')
     pickle.dump(mydates, output)
     output.close()
else:
     for d in dates:
         if not mydates.has_key(d):
             mydates[d] = ''

print html % mydates


Then you could write an additional program that runs
in the background or something:

import pickle
from datetime import date, datetime

mycode = 'print "Hello World!"'
file = open('dates.pkl', 'rb')
mydates = pickle.load(file)
file.close()

while True:
today = date.today()
     if today.month == 4 and today.day == 01:
         hour = datetime.time(datetime.now()).hour
         min = datetime.time(datetime.now()).minute
         if hour == int(mydates['april04'][0]) and min ==
                 int(mydates['april04'][-2:]):
             exec mycode

_exec_ executes a Python string like a program.  To execute an actual 
python script use subprocess instead:

import subprocess
subprocess.call(["python", "myscript.py"])

Hope this helps.

On 10/05/2012 11:55 AM, Luca Sanna wrote:
> hi,
>
> I enter a calendar in an html page
> in each calendar day, I enter a time that is used by the program to perform actions with python
>
> What can I use to do this?
>
> thanks
>




More information about the Python-list mailing list