Html creates tables of different lengths (preferred in flask)
P粉133321839
P粉133321839 2023-08-01 20:40:21
0
1
508
<p>I want to get the data from the database and import it into the website table. Assume there are 50 rows in the sql database. Then there must be 50 rows in the table. However, when I try to create a table, I have to manually add labels for each row and color. So should I add a few dozen sound lines and achieve this by controlling their visibility? I'm currently using flask, but if there's no way to implement it with flask, other ways are accepted. </p>
P粉133321839
P粉133321839

reply all(1)
P粉401901266

In Flask, you can dynamically generate HTML tables with different lengths based on data in a SQL database. You don't need to manually create thousands of rows and control their visibility. Instead, you can easily achieve this using the template engine integrated into Flask. Hope this helps you

Retrieve data from a SQL database: Get data from a database using Flask's database integration.

Pass data to the template: In your Flask route, pass the data retrieved from the database as variables to the HTML template.

Use templates: In HTML templates, use syntax to traverse data and dynamically generate table rows and cells.


from flask import Flask, render_template

app = Flask(__name__)

# Replace this with your database connection and query code to fetch data
# For demonstration purposes, let's assume you have fetched data in the 'rows' variable
rows = [
    {'id': 1, 'name': 'John', 'age': 25},
    {'id': 2, 'name': 'Jane', 'age': 30},
    # Add more rows as needed
]

@app.route('/')
def index():
    return render_template('table_template.html', rows=rows)

if __name__ == '__main__':
    app.run(debug=True)

HTMLFILE

<!DOCTYPE html>
<html>
<head>
    <title>Dynamic Table</title>
    <style>
        /* Add border to the table */
        table {
            border-collapse: collapse;
            width: 100%;
            border: 1px solid black;
        }

        /* Add bold font style to the header row */
        th {
            font-weight: bold;
        }

        /* Add border to table cells (optional) */
        td, th {
            border: 1px solid black;
            padding: 8px;
        }
    </style>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Age</th>
            </tr>
        </thead>
        <tbody>
            {% for row in rows %}
                <tr>
                    <td>{{ row.id }}</td>
                    <td>{{ row.name }}</td>
                    <td>{{ row.age }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!