html - Post in form not passing to Python -
first time posting here excuse me not being clear or not following guidelines well. have been trying working far no luck , cant figure out why. "timeinterval" selector in form passes fine "setpointval" selector not pass when form submitted. have stripped away code bare bones , upon reloading, when call obtain sp in main(), form=cgi.fieldstorage not see "setpointval" in form. guidance appreciated.
the code below capable of creating chart data in database based on "timeinterval" selector , able have setpoint sent python script control of system. have of control setup in other script, trying form maintain inputted setpoint.
#!/usr/bin/env python import sqlite3 import sys import cgi import cgitb #--------------------------------------------------------------------------------------------------------------------------------- # global variables speriod=(15*60)-1 dbname='/var/www/temp_data.db' setp=0 #sp=get_sp() #sp = form["setpoint"].value #--------------------------------------------------------------------------------------------------------------------------------- # print http header def printhttpheader(): print "content-type: text/html\n\n" print #--------------------------------------------------------------------------------------------------------------------------------- # print html head section # arguments page title , table chart def printhtmlhead(title, table): print "<head>" print " <title>" print title print " </title>" print_graph_script(table) print "</head>" #--------------------------------------------------------------------------------------------------------------------------------- # data database # if interval passed, # return list of records database def get_data(interval): conn=sqlite3.connect(dbname) curs=conn.cursor() if interval == none: curs.execute("select * temps") else: curs.execute("select * temps timestamp>datetime('now','localtime','-%s hours')" % interval) rows=curs.fetchall() conn.close() return rows #--------------------------------------------------------------------------------------------------------------------------------- # convert rows database javascript table def create_table(rows): chart_table="" row in rows[:-1]: rowstr="['{0}', {1}],\n".format(str(row[0]),str(row[1])) chart_table+=rowstr row=rows[-1] rowstr="['{0}', {1}]\n".format(str(row[0]),str(row[1])) chart_table+=rowstr return chart_table #--------------------------------------------------------------------------------------------------------------------------------- # print javascript generate chart # pass table generated database info def print_graph_script(table): # google chart snippet chart_code=""" <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("visualization", "1", {packages:["corechart"]}); google.setonloadcallback(drawchart); function drawchart() { var data = google.visualization.arraytodatatable([ ['time', 'temperature channel 0'],%s ]); var options = { title: 'temperature' }; var chart = new google.visualization.linechart(document.getelementbyid('chart_div')); chart.draw(data, options); } </script>""" print chart_code % (table) #--------------------------------------------------------------------------------------------------------------------------------- # print div contains graph def show_graph(): print "<h2>temperature chart</h2>" print '<div id="chart_div" style="width: 900px; height: 500px;"></div>' #--------------------------------------------------------------------------------------------------------------------------------- # connect db , show stats # argument option number of hours def show_stats(option,sp): conn=sqlite3.connect(dbname) curs=conn.cursor() if option none: option = str(24) curs.execute("select timestamp,max(temp0) temps timestamp>datetime('now','localtime','-%s hour') , timestamp<=datetime('now','localtime')" % option) rowmax0=curs.fetchone() rowstrmax0="             {0}                    {1} c".format(str(rowmax0[0]),str(rowmax0[1])) curs.execute("select timestamp,min(temp0) temps timestamp>datetime('now','localtime','-%s hour') , timestamp<=datetime('now','localtime')" % option) rowmin0=curs.fetchone() rowstrmin0="             {0}                    {1} c".format(str(rowmin0[0]),str(rowmin0[1])) curs.execute("select avg(temp0) temps timestamp>datetime('now','localtime','-%s hour') , timestamp<=datetime('now','localtime')" % option) # curs.execute("select avg(temp0) temps timestamp>datetime('2014-03-18 00:00:00','-%s hour') , timestamp<=datetime('2014-03-18 20:45:00')" % option) rowavg0=curs.fetchone() curs.execute("select max(timestamp),temp0 temps") rownow=curs.fetchone() rowstrnow="             {0}                    {1} c".format(str(rownow[0]),str(rownow[1])) print "<hr>" print "<h2>current temperatures     ch0</h2>" print rowstrnow print "<h2>minimum temperature   ch0</h2>" print rowstrmin0 print "<h2>maximum temperature  ch0</h2>" print rowstrmax0 print "<h2>average temperatures    ch0</h2>" print "                                                               %.3f" % rowavg0+" c" print "<hr>" print "<h2>in last hour:</h2>" print "<table>" print "<tr><td><strong>date/time</strong></td><td><strong>ch0 temp</strong>  </td></tr>" rows=curs.execute("select * temps timestamp>datetime('now','localtime','-1 hour') , timestamp<=datetime('now','localtime')") row in rows: rowstr="<tr><td>{0}  </td><td>{1} c  </td></tr>".format(str(row[0]),str(row[1])) print rowstr print "</table>" print "<hr>" conn.close() #--------------------------------------------------------------------------------------------------------------------------------- def print_time_selector(option): print """<form action="/cgi-bin/webgui1.py" method="post"> show temperature logs <select name="timeinterval">""" if option not none: if option == "1": print "<option value=\"1\" selected=\"selected\">the last hour</option>" else: print "<option value=\"1\">the last hour</option>" if option == "6": print "<option value=\"6\" selected=\"selected\">the last 6 hours</option>" else: print "<option value=\"6\">the last 6 hours</option>" if option == "12": print "<option value=\"12\" selected=\"selected\">the last 12 hours</option>" else: print "<option value=\"12\">the last 12 hours</option>" if option == "24": print "<option value=\"24\" selected=\"selected\">the last 24 hours</option>" else: print "<option value=\"24\">the last 24 hours</option>" if option == "48": print "<option value=\"48\" selected=\"selected\">the last 2 days</option>" else: print "<option value=\"48\">the last 2 days</option>" if option == "168": print "<option value=\"168\" selected=\"selected\">the last week</option>" else: print "<option value=\"168\">the last week</option>" if option == "240": print "<option value=\"240\" selected=\"selected\">the last 10 days</option>" else: print "<option value=\"240\">the last 10 days</option>" else: print """<option value="1">the last hour</option> <option value="6">the last 6 hours</option> <option value="12">the last 12 hours</option> <option value="24" selected="selected">the last 24 hours</option> <option value="48" selected="selected">the last 2 days</option> <option value="168" selected="selected">the last week</option> <option value="240" selected="selected">the last 10 days</option>""" print """ </select>""" #--------------------------------------------------------------------------------------------------------------------------------- def print_setpoint_selector(sp): print """ setpoint: <select name="setpointval">""" if sp not none: if sp == "18": print "<option value=\"18\" selected=\"selected\">18</option>" else: print "<option value=\"18\">18</option>" if sp == "19": print "<option value=\"19\" selected=\"selected\">19</option>" else: print "<option value=\"19\">19</option>" if sp == "20": print "<option value=\"20\" selected=\"selected\">20</option>" else: print "<option value=\"20\">20</option>" if sp == "21": print "<option value=\"21\" selected=\"selected\">21</option>" else: print "<option value=\"21\">21</option>" if sp == "22": print "<option value=\"22\" selected=\"selected\">22</option>" else: print "<option value=\"22\">22</option>" if sp == "23": print "<option value=\"23\" selected=\"selected\">23</option>" else: print "<option value=\"23\">23</option>" if sp == "24": print "<option value=\"24\" selected=\"selected\">24</option>" else: print "<option value=\"24\">24</option>" else: print """<option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option>""" print """ </select> <input type="submit" value="display"> </form>""" #--------------------------------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------------------------------------- # check option valid # , not sql injection def validate_input(option_str): # check option string represents number if option_str.isalnum(): # check option within specific range if int(option_str) > 0 , int(option_str) <= 240: return option_str else: return none else: return none #------------------------------------------------------------------------------------ #--------------------------------------------------------------------------------------------------------------------------------- # check option valid # , not sql injection #def validate_input_sp(setpoint_sp): # # check option string represents number # if setpoint_sp.isalnum(): # check option within specific range # if int(setpoint_sp) > 0 , int(setpoint_sp) <= 24: # return setpoint_sp # else: # return none # else: # return none #--------------------------------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------------------------------------- #return sp passed script def get_sp(): form=cgi.fieldstorage() if "setpointval" in form: sp = form["setpointval"].value return validate_input(sp) # return sp else: # print """ hello world""" return none #--------------------------------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------------------------------------- #return option passed script def get_option(): form=cgi.fieldstorage() # print form.keys if "timeinterval" in form: option = form["timeinterval"].value return validate_input(option) else: return none #--------------------------------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------------------------------------- # main function # program starts def main(): cgitb.enable() # options may have been passed script option=get_option() if option none: option = str(24) #------------------------------------------------ # options may have been passed script sp=get_sp() # print sp setp=sp # if sp none: # print "none" # sp = str(20) #------------------------------------------------ # data database records=get_data(option) # print http header printhttpheader() if len(records) != 0: # convert data table table=create_table(records) else: print "no data found" return # start printing page print "<html>" # print head section including table # used javascript chart printhtmlhead("temperature logger", table) # print page body print "<body>" print "<h1>temperature logger</h1>" print "<hr>" print_time_selector(option) print_setpoint_selector(sp) show_graph() show_stats(option,sp) print "</body>" print "</html>" sys.stdout.flush() if __name__=="__main__": main() #---------------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment