title: No data available in table | Flask/Python/Sqlite
body: <p>I am trying to use a simple app to display database information using Python, Flask and SQLite. I have three files server_table.html, server_table.py and base.html. My SQLite database testdb.db is constructed successfully and I am able to pull/push data to it. The columns and data type are correct for the database model.</p>
  <p>However, when I run server_table.py, the webpage renders successfully but I receive a <code>No data available in table</code> message where the table data should be displayed.</p>
  <p>No other messages are received.</p>
  <p>Code I am using:</p>
  <p>server_table.html</p>
  <pre><code>{% extends &quot;base.html&quot; %}
  
  {% block content %}
  &lt;table id=&quot;data&quot; class=&quot;table table-striped&quot;&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;Age&lt;/th&gt;
              &lt;th&gt;Address&lt;/th&gt;
              &lt;th&gt;Phone Number&lt;/th&gt;
              &lt;th&gt;Email&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  {% endblock %}
  
  {% block scripts %}
  &lt;script&gt;
      $(document).ready(function () {
          $('#data').DataTable({
              ajax: '/api/data',
              serverSide: true,
              columns: [
                  { data: 'name' },
                  { data: 'age' },
                  { data: 'address', orderable: false },
                  { data: 'phone', orderable: false },
                  { data: 'email' }
              ],
          });
      });
  &lt;/script&gt;
  {% endblock %}
  </code></pre>
  <p>base.html</p>
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  
  &lt;head&gt;
      &lt;title&gt;{{ title }}&lt;/title&gt;
      &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;
          integrity=&quot;sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x&quot; crossorigin=&quot;anonymous&quot;&gt;
      &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.datatables.net/1.10.25/css/dataTables.bootstrap5.css&quot;&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;div class=&quot;container&quot;&gt;
          &lt;h1&gt;{{ title }}&lt;/h1&gt;
          &lt;hr&gt;
          {% block content %}{% endblock %}
      &lt;/div&gt;
      &lt;script type=&quot;text/javascript&quot; charset=&quot;utf8&quot; src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
      &lt;script type=&quot;text/javascript&quot; charset=&quot;utf8&quot;
          src=&quot;https://cdn.datatables.net/1.10.25/js/jquery.dataTables.js&quot;&gt;&lt;/script&gt;
      &lt;script type=&quot;text/javascript&quot; charset=&quot;utf8&quot;
          src=&quot;https://cdn.datatables.net/1.10.25/js/dataTables.bootstrap5.js&quot;&gt;&lt;/script&gt;
      {% block scripts %}{% endblock %}
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  <p>server_table.py</p>
  <pre><code>from flask import Flask, render_template, request
  
  from flask_sqlalchemy import SQLAlchemy
  
  app = Flask(__name__)
  app.config[&quot;SQLALCHEMY_DATABASE_URI&quot;] = &quot;sqlite:///testdb.db&quot;
  app.config[&quot;SQLALCHEMY_TRACK_MODIFICATIONS&quot;] = False
  db = SQLAlchemy(app)
  
  
  class User(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(64), index=True)
      age = db.Column(db.Integer, index=True)
      address = db.Column(db.String(256))
      phone = db.Column(db.String(20))
      email = db.Column(db.String(120))
  
      def to_dict(self):
          return {
              &quot;name&quot;: self.name,
              &quot;age&quot;: self.age,
              &quot;address&quot;: self.address,
              &quot;phone&quot;: self.phone,
              &quot;email&quot;: self.email,
          }
  
  
  db.create_all()
  
  
  @app.route(&quot;/&quot;)
  def index():
      return render_template(&quot;server_table.html&quot;, title=&quot;Server-Driven Table&quot;)
  
  
  @app.route(&quot;/api/data&quot;)
  def data():
      query = User.query
  
      # search filter
      search = request.args.get(&quot;search[value]&quot;)
      if search:
          query = query.filter(
              db.or_(User.name.like(f&quot;%{search}%&quot;), User.email.like(f&quot;%{search}%&quot;))
          )
      total_filtered = query.count()
  
      # sorting
      order = []
      i = 0
      while True:
          col_index = request.args.get(f&quot;order[{i}][column]&quot;)
          if col_index is None:
              break
          col_name = request.args.get(f&quot;columns[{col_index}][data]&quot;)
          if col_name not in [&quot;name&quot;, &quot;age&quot;, &quot;email&quot;]:
              col_name = &quot;name&quot;
          descending = request.args.get(f&quot;order[{i}][dir]&quot;) == &quot;desc&quot;
          col = getattr(User, col_name)
          if descending:
              col = col.desc()
          order.append(col)
          i += 1
      if order:
          query = query.order_by(*order)
  
      # pagination
      start = request.args.get(&quot;start&quot;, type=int)
      length = request.args.get(&quot;length&quot;, type=int)
      query = query.offset(start).limit(length)
  
      # response
      return {
          &quot;data&quot;: [user.to_dict() for user in query],
          &quot;recordsFiltered&quot;: total_filtered,
          &quot;recordsTotal&quot;: User.query.count(),
          &quot;draw&quot;: request.args.get(&quot;draw&quot;, type=int),
      }
  
  
  if __name__ == &quot;__main__&quot;:
      app.run()
  </code></pre>