title: Using asynchronous POST requests with Python's Bottle?
body: <p>I'm trying to create a turn-based strategy game (think Dominion) in Python. The core game objects and methods are Python classes with methods in them (just typical OO stuff). The UI is an HTML client using Bottle. I'm aiming for an entirely asynchronous approach. So the sole page's content is generated from the Python objects, and I want submits from the page to update those objects without ever leaving the page by going back through the bottle webserver (using jQuery AJAX for this).</p>
  
  <p>At the moment I'm working on a basic chat system that retrieves player-written messages and stores them as Chat objects (containing player and text data, nothing else). These objects are then written to a chat window using AJAX that updates the windows once every second. The HTML format of the chat lines is <code>&lt;div class="chatLine"&gt;&lt;p&gt;Player &gt; &lt;/p&gt;&lt;p&gt;Text&lt;/p&gt;&lt;/div&gt;</code> Pretty standard stuff.</p>
  
  <p>This basic diagram might make it a little clearer, even though it isn't really technical, more conceptual:</p>
  
  <hr>
  
  <h2><img src="https://i.stack.imgur.com/oIPFQ.jpg" alt="Communication cycle diagram"></h2>
  
  <hr>
  
  <p>My BottleUI.py (this is what I run to start the server):</p>
  
  <pre><code>from Populate import *  # Everything in Populate can now be directly called.
  # NOTE: Populate allows access to "bottle" and "Main"
  
  # This ensures the css file is available
  @route('/theCSS')
  def static():
      return static_file('main.css', root='static')
  
  @route('/citadel/:game/:player')
  def gamePage(game, player):
      if not (game.isdigit()) or not (player.isdigit()):
          return "Invalid page."
      game = int(game)
      player = int(player)
      if ((game &gt;= 0) and (game &lt; listOfGames.__len__())) and ((player &gt;= 0) and (player &lt; listOfGames[game].listOfPlayers.__len__())):
  
          return '''
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;link rel="stylesheet" type="text/css" href="/theCSS"&gt;
      &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"&gt;&lt;/script&gt;
      &lt;!-- Sample AJAX script below, change as needed --&gt;
      &lt;script type="text/javascript"&gt;
          $(document).ready(function() {
              $('#chatForm').submit(function(e) {
                  $.ajax({
                      type: 'POST',
                      url: "/AddToChat/''' + str(game) + '''/''' + str(player) + '''",
                      success: function() {
                          $('#chatInput').val("");
                      }
                  });
                  e.preventDefault();
              });
          });
          setInterval("updateChat();", 1000);
          $(function() {
              updateChat = function() {
                  $('#chatText').load('/GenerateChat/''' + str(game) + '''');
              };
          });
      &lt;/script&gt;
      &lt;!-- Script to scroll to bottom of divs - needs to be changed into called function --&gt;
      &lt;script type="text/javascript"&gt;
          window.onload = function () {
               var objDiv = document.getElementById("gameFlow");
               objDiv.scrollTop = objDiv.scrollHeight;
               objDiv = document.getElementById("chatText");
               objDiv.scrollTop = objDiv.scrollHeight;
          };
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="container"&gt;
          &lt;!-- Make this have background-image with the game number displaying programmatically --&gt;
          &lt;div id="banner"&gt;
              &lt;h1&gt;Citadel - Game ''' + str(game) + ''', Player ''' + str(player) + '''&lt;/h1&gt;
          &lt;/div&gt;
          &lt;div id="main"&gt;
              &lt;div id="leftPanel"&gt;
                  &lt;div id="playerTotals"&gt;
                      &lt;h4&gt;Player Totals:&lt;/h4&gt;
                      &lt;div id="totalsText"&gt;
                          &lt;p&gt;Money:&lt;/p&gt;
                          &lt;p&gt;Population:&lt;/p&gt;
                          &lt;p&gt;Troops:&lt;/p&gt;
                          &lt;p&gt;Friend:&lt;/p&gt;
                          &lt;p&gt;Enemy:&lt;/p&gt;
                      &lt;/div&gt;
                      &lt;!-- Player totals go here (money, population/limit, troops, friend, enemy) --&gt;
                      &lt;div id="totalsNums"&gt;
  
                      &lt;/div&gt;
                      &lt;div class="clear"&gt;&lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="leftSegment"&gt;
                      &lt;h4&gt;Troop Cards:&lt;/h4&gt;
                      &lt;!-- Player's troopCards here --&gt;
                      &lt;select size=2&gt;
  
                      &lt;/select&gt;
                  &lt;/div&gt;
                  &lt;div class="leftSegment"&gt;
                      &lt;h4&gt;Territory Cards:&lt;/h4&gt;
                      &lt;!-- Player's territoryCards here --&gt;
                      &lt;select size=2&gt;
  
                      &lt;/select&gt;
                  &lt;/div&gt;
                  &lt;div class="leftSegment"&gt;
                      &lt;h4&gt;Region Cards:&lt;/h4&gt;
                      &lt;!-- Player's regionCards here --&gt;
                      &lt;select size=2&gt;
  
                      &lt;/select&gt;
                  &lt;/div&gt;
                  &lt;div class="leftSegment"&gt;
                      &lt;h4&gt;Resource Cards:&lt;/h4&gt;
                      &lt;!-- Player's resourceCards here --&gt;
                      &lt;select size=2&gt;
  
                      &lt;/select&gt;
                  &lt;/div&gt;
                  &lt;div class="leftSegment"&gt;
                      &lt;h4&gt;Diplomacy Cards:&lt;/h4&gt;
                      &lt;!-- Player's diplomacyCards here --&gt;
                      &lt;select size=2&gt;
  
                      &lt;/select&gt;
                  &lt;/div&gt;
                  &lt;div id="chatPane"&gt;
                      &lt;form id="chatForm" method="POST" action="/AddToChat/''' + str(game) + '''/''' + str(player) + '''"&gt;
                          &lt;textarea name="theChatText" id="chatInput"&gt;&lt;/textarea&gt;
                          &lt;input id="chatSubmit" class="button" type="submit" value="Send" /&gt;
                      &lt;/form&gt;
                  &lt;/div&gt;
                  &lt;div class="clear"&gt;&lt;/div&gt;
              &lt;/div&gt;
              &lt;div id="rightPanel"&gt;
                  &lt;!-- Game flow goes here (shows current player/phase, attacks with results, etc) --&gt;
                  &lt;div id="gameFlow"&gt;
  
                  &lt;/div&gt;
                  &lt;!-- Player turn stuff goes here (changes depending on which phase, etc) --&gt;
                  &lt;div id="playerActions"&gt;
  
                  &lt;/div&gt;
                  &lt;!-- Chat goes here (implement last) --&gt;
                  &lt;div id="chatText"&gt;
  
                  &lt;/div&gt;
                  &lt;div class="clear"&gt;&lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
      '''
  
      else:
          return "Invalid page."
  
  run(host='localhost', port=8080)
  </code></pre>
  
  <hr>
  
  <p>And here's my Populate.py (this is where my AJAX @route methods are stored):</p>
  
  <pre><code>    """
  This module contains the bottle routs for AJAX population of the various parts
  of the game page.
  """
  
  from bottle import route, run, template, static_file, request
  from Main import *  # Everything in Main can now be directly called.
  
  globalBegin()
  
  @route('/AddToChat/:game/:player', method='POST')
  def AddToChat(game, player):
      theText = request.POST.get('theChatText', '').strip()
      game = int(game)
      player = int(player)
      listOfGames[game].listOfPlayers[player].addChat(theText)
  
  @route('/GenerateChat/:game')
  def GenerateChat(game):
      chatText = ""
      game = int(game)
      for line in listOfGames[game].chatList:
          chatText += '&lt;div class="chatLine"&gt;&lt;p&gt;'
          chatText += line.player.name
          chatText += ' &gt; &lt;/p&gt;&lt;p&gt;'
          chatText += line.text
          chatText += '&lt;/p&gt;&lt;/div&gt;'
      return chatText
  </code></pre>
  
  <hr>
  
  <p>The problem is, the 'chatForm' form isn't working as intended. <code>AddToChat()</code> seems to think that <code>request.POST.get('theChatText', '')</code> is a <code>NoneType</code> when I try to submit text.</p>
  
  <p>So yeah, I'm stumped as to why it's doing this. As far as I can see, <code>'theChatText'</code> should be a valid key in the POST dict.</p>
  
  <p>I'll also just state that all my core game logic works (even though it's pretty clear that isn't the problem here).</p>
  
  <p>Any help is appreciated.</p>