title: Using web.py to dynamically output values from process initiated by form submission
body: <p>I'm new to web servers, so I need some guidance as to how to achieve the following:</p>
  
  <ol>
  <li>User that's on the same network as server opens a page on the server (eg 10.42.68.130:8080/cycle).</li>
  <li>User is presented with a form, that asks him to enter a number of cycles.</li>
  <li>User clicks submit. The submission activates another function that executes a for-loop the specified number of times (I simplified all of this, so it's really supposed to fire GPIO pins on a beaglebone black)</li>
  <li>The user receives progress feedback, so a counter that shows how many cycles have been completed.</li>
  </ol>
  
  <p>Any help would be much appreciated.</p>
  
  <p>I'm going off the tutorials in learnpythonthehardway.com lessons 50 and 51. This is what I managed to get going. </p>
  
  <p>bin/app.py</p>
  
  <pre><code>import web
  import time
  
  urls = ('/cycle', 'Index')
  
  app = web.application(urls, globals())
  
  render = web.template.render('templates/', base="layout")
  
  class Index(object):
      def GET(self):
          return render.hello_form2()
  
      def POST(self):
          form = web.input(cycles=0)
          cycleparams = "%s" % (form.cycles)
          cycleGPIO(form.cycles)
          return render.index2(cycleparams = cycleparams)
  
  if __name__ == "__main__":
      app.run()
  
  def cycleGPIO(cycles):
      for i in range(int(cycles)):
          print "cycle " + str(i+1)
          time.sleep(1)
  </code></pre>
  
  <p>templates/layout.html</p>
  
  <pre><code>$def with (content)
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Test Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  $:content
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>templates/index2.html</p>
  
  <pre><code>$def with (cycleparams)
  
  $if cycleparams:
      Test completed with the following parameters:
      &lt;br&gt; 
      $cycleparams
  $else:
      No parameters specified
  &lt;br&gt;&lt;br&gt;
  &lt;a href="/cycle"&gt;Return to form&lt;/a&gt;
  </code></pre>
  
  <p>templates/hello_form2.html</p>
  
  <pre><code>&lt;fieldset&gt;
  &lt;legend&gt;Output Cycling&lt;/legend&gt;
  
  &lt;form action="/cycle" method="POST"&gt;
      &lt;br&gt;Number of cycles:
      &lt;br&gt;&lt;input type="number" name="cycles"&gt;
      &lt;input type="submit"&gt;
  &lt;/form&gt;
  &lt;/fieldset&gt;
  </code></pre>