【问题标题】:Problem to show data dynamically table in python flask在python烧瓶中动态显示数据表的问题
【发布时间】:2022-01-04 20:13:28
【问题描述】:

我想使用 python 烧瓶框架在 html 中显示一个表格。我有两个数组。一个用于列标题,另一个用于数据记录。列标题和数据记录的长度是动态的。我可以动态管理列“标题”。但只是将信息附加到“数据”数组中并不能正确显示数据。请帮我解决这个问题。是否应该更改 html 文件中的某些内容以使其动态化? Python 文件 from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/')
def my_form():

    #headings = ("name", "role", "salary")
    headings = []
    headings.append("name")   
    headings.append("role")   
    
    data1 = ("rolf", "software engineer", "4500"), ("neu", "civil engineer", "1500"), ("neu", "civil engineer", "1500")
# =============================================================================
    data = []
    data.append("rolf")
    data.append("software engineer")
    data.append("neu")
    data.append("civil engineer")
    
    
# =============================================================================
    print (data1)
    print (data)
    
    #ss =  '('+'('+ "rolf"+ ','+ "software engineer" + ',' + "4500" + ')'+','+')'
    return render_template('table2.html', data=data, headings=headings)
    
if __name__ == '__main__':
   app.run()

html文件

<table>
<tr>
{% for header in headings %}
       <th>{{ header }}</th>
        {% endfor %}
    </tr>
    {% for row in data %}
    <tr>
    {% for cell in row %}
    <td>{{ cell }}</td>
    {% endfor %}
    </tr>
    {% endfor %}
</table>

【问题讨论】:

    标签: python flask


    【解决方案1】:

    data1 是一个元组的元组

    >>> data1 = ("rolf", "software engineer", "4500"), ("neu", "civil engineer", "1500"), ("neu", "civil engineer", "1500")
    >>> data1
    (('rolf', 'software engineer', '4500'), ('neu', 'civil engineer', '1500'), ('neu', 'civil engineer', '1500'))
    
    

    data,变成了一个元组列表,这样构建时具有完全不同的结构:

    >>> data = []
    >>> data.append("rolf")
    >>> data.append("software engineer")
    >>> data.append("neu")
    >>> data.append("civil engineer") 
    >>> data
    ['rolf', 'software engineer', 'neu', 'civil engineer']
    >>> 
    

    我想你正在寻找:

    >>> data = []
    >>> data.append(("rolf","software engineer", "4500"))
    >>> data.append(("neu","civil engineer", "1500"))
    >>> data
    [('rolf', 'software engineer', '4500'), ('neu', 'civil engineer', '1500')]
    

    也许这不是表示数据的最佳方式。不过,在这个问题的背景下很难给出进一步的建议。

    我猜这只是为了教育目的,您知道在其他类型的实现中,此信息可能是从数据库中检索的,或者来自 API 响应,而不是在 @987654326 中静态定义像这样的@文件。

    【讨论】:

    • 非常感谢
    【解决方案2】:

    对于您的数据,显然您希望生成一个像这样的元组列表:

    >>> data = []
    >>> row = tuple()
    >>> row += ('rolf',)
    >>> row += ('software engineer',)
    >>> row += (4500,)
    >>> row
    ('rolf', 'software engineer', 4500)
    >>> data.append(row)
    >>> data
    [('rolf', 'software engineer', 4500)]
    >>> 
    

    还有你的 HTML 代码:

    <table>
        <thead>
            <tr>
                {% for header in headings %}
                    <th>{{ header }}</th>
                {% endfor %}
            </tr>
        </thead>
        <tbody>
            {% for row in data %}
                <tr>
                    {% for cell in row %}
                        <td>{{ cell }}</td>
                    {% endfor %}
                </tr>
            {% endfor %}
        </tbody>
    </table>
    

    【讨论】:

    • 看起来不错。谢谢。
    猜你喜欢
    • 2019-02-24
    • 2013-01-29
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多