calendar from python to html

Discussion in 'Python' started by Luca Sanna, Oct 5, 2012.

  1. Luca Sanna

    Luca Sanna Guest

    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
    Luca Sanna, Oct 5, 2012
    #1
    1. Advertising

  2. 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
    >
    Jason Benjamin, Oct 6, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shevek
    Replies:
    3
    Views:
    6,008
    Shevek
    Jun 23, 2004
  2. =?Utf-8?B?Q2hyaXM=?=
    Replies:
    3
    Views:
    13,730
    Peter Blum
    Mar 30, 2005
  3. Kajsa Anka

    HTML Calendar code

    Kajsa Anka, Jan 13, 2007, in forum: Python
    Replies:
    2
    Views:
    2,229
    Kajsa Anka
    Jan 13, 2007
  4. Shevek
    Replies:
    0
    Views:
    373
    Shevek
    Jun 23, 2004
  5. Parthiv Joshi
    Replies:
    1
    Views:
    641
    Samuel L Matzen
    Jul 6, 2004
Loading...

Share This Page