Solved: how to make dinamic table in jinja

Jinja is a powerful and flexible templating engine for Python. One common use case in web development is to create dynamic tables that display data in a structured way. In this article, we will explore how to create a dynamic table using Jinja, focusing on generating table structure from a list of data along with implementing interactivity and sorting options. Read on to discover how this can be achieved with Jinja and Python.

First, let’s start with defining our problem. We want the ability to display a dynamic table on a webpage using Jinja as our templating engine. The table will have sortable columns and possibly interactive elements for the user to interact with, such as edit or delete buttons. To solve this problem, we will need to use Python, Jinja, as well as some additional libraries and techniques.

Setting up the environment

Before we dive into writing the code, we must first set up our environment. We will need the following libraries:

  • Flask – A lightweight web framework for Python.
  • Jinja – Our desired templating engine for rendering dynamic tables.
  • Flask-SQLAlchemy – A Flask extension for our data management using SQLAlchemy.

Make sure to install these libraries using pip:

pip install flask jinja2 flask-sqlalchemy

Creating the table data

Now that we have our environment set up, let’s create some data to display in our dynamic table. For this example, we’ll use a simple list of dictionaries containing information about fashion items, such as their style, color, and history.

fashion_items = [
    "style": "Grunge",
    "color": "Dark tones",
    "history": "Originated in the late 1980s and reached its peak in the 1990s.",
    "style": "Bohemian",
    "color": "Earthy tones",
    "history": "Has a long history dating back to the early 19th century.",
    "style": "Preppy",
    "color": "Pastel colors",
    "history": "Began in the early 20th century and enjoyed a resurgence in the 1980s.",

Creating the Jinja template

Now that we have our data, we need to create the Jinja template for rendering our dynamic table. This will involve writing HTML code along with Jinja2 template tags, which will process the Python data we pass to the template. Let’s start by creating our table structure.

{% extends "base.html" %}

{% block content %}
      {% for item in fashion_items %}
          <td>{{ }}</td>
          <td>{{ item.color }}</td>
          <td>{{ item.history }}</td>
      {% endfor %}
{% endblock %}

The code above creates an HTML table using tags like <table>, <thead>, and <tbody>. Inside the Jinja2 template tags {% for item in fashion_items %} and {% endfor %}, we loop through each item in the fashion_items list and create a new table row containing the item’s data.

Rendering the table with Flask

Now that we have our template and data ready, we’ll use Flask to render the table on a webpage. First, create a Flask application, and then pass in your data and render your Jinja template.

from flask import Flask, render_template

app = Flask(__name__)

def index():
    return render_template('index.html', fashion_items=fashion_items)

if __name__ == '__main__':

With this code, we’re passing the fashion_items list to our Jinja template, and it will be rendered in our table, making it fully dynamic and interactive.

In summary, we’ve learned how to create a dynamic table in Jinja by using a Python list, rendering it in a Jinja template, and then displaying it on a webpage using Flask. Jinja templates offer powerful features and flexibility for building dynamic tables, which can be useful for various applications in web development. With the knowledge gained from this article, you can now create tables with sortable columns and interactivity to take your web development skills to the next level.

Related posts:

Leave a Comment