title: How to call a python script through an AJAX call?
body: <p>I have the following python script:</p>
  
  <pre><code>from flask import Flask, render_template, json, request
  from flask import Flask, request, abort, jsonify
  from flaskext.mysql import MySQL
  import MySQLdb
  app = Flask(__name__)
  @app.route("/")
  def main():
      return render_template('testFirst.html')
  
  @app.route("/form_submit/", methods=['POST'])
      print ("outside function")
      def connect():
          import json
          dtb = request.select['value']    #i want the selected drop down value to be set here which will be used to connect to the database
          db = MySQLdb.connect("localhost","root","",dtb)
          cursor =  db.cursor()
          cursor.execute("SELECT * FROM REPORT_SUITE")
          results = cursor.fetchall()
          json_return_value =[]
  
          for result in results:
              table_data = {'REPORTSUITE_ID' : result[0], 'REPORTSUITE_NAME' :     result[1], 'STAGING_DATABASE' : result[2], 'DWH_DATABASE' : result[3], 'TRANS_TABLE' : result[4]}
              json_return_value.append(table_data)
          print ("hi")
          print json.dumps(json_return_value)
          return json.dumps(json_return_value)    
      connect()    
  
  if __name__ == "__main__":
      app.run(debug = True)
  </code></pre>
  
  <p>It renders the following html file:
      
      
      
           
          
              $(document).ready(function(){
                  $("button").click(function(){
                      $.ajax({
                    url : "/form_submit/",
                    data : $('#databases').val(),
                    type : 'POST',
                    success : alert("Hi dear count ")
                  });
                  });
              });
          
      </p>
  
  <pre><code>&lt;body&gt;
      &lt;select id ="databases"&gt;
          &lt;option value = ""&gt;--Select From following--&lt;/option&gt;
          &lt;option value = "OMTSL_QBO"&gt;OMTSL_QBO&lt;/option&gt;
          &lt;option value = "OMTSL_SBG"&gt;OMTSL_SBG&lt;/option&gt;
          &lt;option value = "OMTSL_PTX"&gt;OMTSL_PTX&lt;/option&gt;
      &lt;/select&gt;
      &lt;br&gt;&lt;br&gt;
      &lt;button type = "button"&gt; Click Me!!&lt;/button&gt;
      &lt;div id ="placeholder"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The HTML form should call the python script through an ajax call by passing the selected value from the drop down to the connect() method.</p>
  
  <p>when i run main.py, i get the following error</p>
  
  <pre><code>Traceback (most recent call last):
    File "main.py", line 31, in &lt;module&gt;
      connect()    
    File "main.py", line 16, in connect
      dtb = request.select['value']
    File "/Library/Python/2.7/site-packages/werkzeug/local.py", line 338, in   __getattr__
      return getattr(self._get_current_object(), name)
    File "/Library/Python/2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object
      return self.__local()
    File "/Library/Python/2.7/site-packages/flask/globals.py", line 20, in _lookup_req_object
      raise RuntimeError('working outside of request context')
  RuntimeError: working outside of request context
  </code></pre>
  
  <p>I do not know how to pass the drop down value to the called python fucntion. I tried to look for it but i have still not found the way to 
  do it. Please help me through this </p>