title: Python: How to display data from a MySQL query in a table on a Flask App
body: <p>I'm relatively new to Python/flask and I'm having trouble with some database stuff.</p>
  
  <p>I was able to connect to a MySQL db in a different function and get form inputs from a user inserted into the db using wtforms. Now I want to display the data stored in the db on the 'dashboard' page in table format using jinja templating. Every time someone submits a new form, I want the table to expand to include that data.</p>
  
  <p>However, nothing seems to be showing up at all, even the existing data which leads me to believe I'm doing something wrong in the second code snippet below. I'm not getting any errors when I run the python file so perhaps I'm doing the HTML templating wrong?</p>
  
  <p>Database connection (dbconnect.py):</p>
  
  <pre><code>import MySQLdb
  
  def connection():
      # Edited out actual values
      conn = MySQLdb.connect(host="",
                             user="",
                             passwd="",
                             db = "")
      c = conn.cursor()
  
      return c, conn
  </code></pre>
  
  <p>Main python code (don't know if try/except is necessary):</p>
  
  <pre><code>from dbconnect import connection
  from flask import Flask, render_template
  
  @app.route('/dashboard/')       
  def display_deals():
  
      try:
          c, conn = connection()
  
          query = "SELECT * from submissions"
          c.execute(query)
  
          data = c.fetchall()
  
          conn.close()
  
          return data
  
          return render_template("dashboard.html", data=data)
  
      except Exception as e:
          return (str(e))
  </code></pre>
  
  <p>HTML:</p>
  
  <pre><code>&lt;table border="1" cellpadding="5" cellspacing="5"&gt;
      {% for row in data %}
          &lt;tr&gt;
          {% for d in row %}
              &lt;td&gt;{{ d }}&lt;/td&gt;
          {% endfor %}
          &lt;/tr&gt;
      {% endfor %}
  &lt;/table&gt;
  </code></pre>