javascript - Send the result of python cgi script to HTML -
i have toggle button on page 'index.html'. when click on it, executes python cgi script changes state of on raspberry.
to so, :
html :
<form id="tgleq" method="post" action="/cgi-bin/remote.py" target="python_result"> <input id="toggle-eq" type="checkbox" data-toggle="toggle" name="toggle-eq" value=""> <script> $(function() { $('#toggle-eq').change(function() { tgl_state = $('#toggle-eq').prop("checked") var toggle = document.getelementbyid("toggle-eq"); toggle.value = tgl_state; document.getelementbyid("tgleq").submit(); }) }) </script>
cgi :
#!/usr/bin/env python import cgi import cgitb cgitb.enable() print "content-type: text/html\n\n" print form=cgi.fieldstorage() arg1 = form.getvalue('toggle-eq')
and want arg1.
now, want is, when open web interface page, state of raspberry component initialize toggle on right position.
to send form on page load launch script looking @ state of component. how can in html ?
i tried urllib , httplib2 nothing worked me... suggestions ?
thanks
if understand question correctly want show current-state of switchable component on webpage. looks have pure html page , cgi page, think have multiple options, 1 of them combine html , cgi 1 page.
the code below example of idea , doesn't work correctly, not copy/paste solution.
#!/usr/bin/env python import cgi import cgitb cgitb.enable() print "content-type: text/html\n\n" print active=""" <form id="tgleq" method="post" action="/cgi-bin/remote.py" target="python_result"> <input id="toggle-eq" type="checkbox" data-toggle="toggle" name="toggle-eq" value=""> """ inactive=""" <form id="tgleq" method="post" action="/cgi-bin/remote.py" target="python_result"> <input id="toggle-eq" type="checkbox" data-toggle="toggle" name="toggle-eq" value="checked"> """ generic = """ <script> $(function() { $('#toggle-eq').change(function() { tgl_state = $('#toggle-eq').prop("checked") var toggle = document.getelementbyid("toggle-eq"); toggle.value = tgl_state; document.getelementbyid("tgleq").submit(); }) }) </script> """ def set_state(option): if (option==true): actual_state_setting_here = 1 # <-magic happens here else: actual_state_setting_here = 0 # <-magic happens here def get_state(): return actual_state_setting_here # <- read actual value here form = cgi.fieldstorage() if ( form.getvalue('toggle-eq')=="checked" ): set_state(true) else: set_state(false) if ( get_state()==true ): print(active) #show active else: print(inactive) #show inactive print(generic) #show rest of page
Comments
Post a Comment