title: Call python function with javascript and display returned string on web page
body: <p>I have a simple appengine/python application that allows the user to click a button to display a random-generated sentence. So far, I have pieced together the main python file as follows:</p>
  
  <pre><code>import jinja2
  import os
  import webapp2
  import generator  # My random sentence generator module; its make_sentence() function returns a string
  
  class MainPage(webapp2.RequestHandler):
  
        def get(self):
            template = jinja_environment.get_template('main.html')
            self.response.out.write(template.render(dict(sentence = generator.make_sentence())))
  
    jinja_environment = jinja2.Environment(
        loader=jinja2.FileSystemLoader(os.path.dirname(__file__))
    )
  
    application = webapp2.WSGIApplication([
        ('/', MainPage),
    ], debug=True)
  </code></pre>
  
  <p>In main.html:</p>
  
  <pre><code>   &lt;!DOCTYPE html&gt;
     &lt;html&gt;
       &lt;head&gt;
         &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
         &lt;link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"&gt;
         &lt;link rel="stylesheet" href="css/custom.css"&gt;
         &lt;title&gt;Random Sentence Generator&lt;/title&gt;
       &lt;/head&gt;
       &lt;body&gt;
        &lt;div class="jumbotron"&gt;
          &lt;p class="lead"&gt;{{ sentence }}&lt;/p&gt;
          &lt;a class="btn-lg btn-info" onclick="makeSentence();return false;" href="#"&gt;Generate sentence!&lt;/a&gt;
        &lt;/div&gt;
  
        &lt;script&gt;    
          function makeSentence(){  
            location.reload(); 
          }
        &lt;/script&gt;
        &lt;script src="http://code.jquery.com/jquery.js"&gt;&lt;/script&gt;
        &lt;script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"&gt;&lt;/script&gt;
      &lt;/body&gt;
    &lt;html&gt;
  </code></pre>
  
  <p>In case it will be important for the question, here is app.yaml as well:</p>
  
  <pre><code>   application: sentence-generator
     version: 1
     runtime: python27
     api_version: 1
     threadsafe: true
  
     handlers:
     - url: /css
       static_dir: css
  
    - url: /.*
      script: sengen.application
  
    libraries:
    - name: jinja2
      version: latest
  </code></pre>
  
  <p>This works okay (in fact, one can try it out <a href="http://sentence-generator.appspot.com/" rel="nofollow noreferrer">here</a> :-) ), but I would like to be able to generate a new sentence without refreshing the page each time. </p>
  
  <p>From <a href="https://stackoverflow.com/questions/15151133/execute-a-python-script-on-button-click">this question</a>, it looks like I can use Ajax for this. But I'm not sure how to put it all together. I'm thinking I can replace the contents of <code>makeSentence()</code> with something like the top answer in the previous link, but I'm not sure how to incorporate the "response" with what I display on the web page.</p>