本文介绍了在python上使用html格式化flask-table输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里不是使用python的非常有经验的用户,只是在家里呆着时才拿起python.我当时正在考虑将flask-table的输出整合到一个网站中.以下是我的python代码,可为我的网站创建一个简单的表格.

Not a very experienced user with python here, just picked up python while staying at home. I was looking into integrating the output of flask-table to a website. The following is my python code that creates a simple table for my website.

    from flask_table import Table, Col

    app = Flask(__name__)

    class ItemTable(Table):
        name = Col('Name')
        description = Col('Description')

    Class Item(object):
        def __init__(self, name, description):
            self.name = name
            self.description = description

    items = [dict(name='Name1', description='Description1'),
             dict(name='Name2', description='Description2'),
             dict(name='Name3', description='Description3')]

    table = ItemTable(items)


    @app.route("/")
    def hello():
        return render_template('index.html', tStrToLoad=table.__html__())

    if __name__ == "__main__":
        app.run()

和我的html代码,它们需要从上面的python代码中显示tStrToLoad.

and my html code that takes tStrToLoad from the python code above to display.

<html>
    <head>
        <title>Test Flask Table</title>
        <style>
            body 
            {
                background-color: #000000;
                color: #FFFFFF;
                font-family:Verdana;
                font-size:16px;
            }
            table, th, td
            {
                border: 1px solid #0088ff;
                border-collapse: collapse;
                padding: 3px;
                font-family:Verdana;
                font-size:12px;
                text-align: left;
            }

        </style>
    </head>
    <body>
        a simple test table
        <br/><br/>

        {{ tStrToLoad }}

    </body>
</html>

我没有在表中显示数据,而是在黑色背景中显示了以下输出

And instead of showing a table with the data, I have the following output in a black background

a simple test table

<table> <thead><tr><th>Name</th><th>Description</th></tr></thead> <tbody> <tr><td>Name1</td><td>Description1</td></tr> <tr><td>Name2</td><td>Description2</td></tr> <tr><td>Name3</td><td>Description3</td></tr> </tbody> </table>

进一步调查后,我做了一个视图页面源代码,这是我由此产生的实际html代码.

Upon further investigating, I did a view page source, this is my actual html code that arises from this.

<html>
    <head>
        <title>Test Flask Table</title>
        <style>
            body 
            {
                background-color: #000000;
                color: #FFFFFF;
                font-family:Verdana;
                font-size:16px;
            }
            table, th, td
            {
                border: 1px solid #0088ff;
                border-collapse: collapse;
                padding: 3px;
                font-family:Verdana;
                font-size:12px;
                text-align: left;
            }

        </style>
    </head>
    <body>
        a simple test table
        <br/><br/>

        &lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Name1&lt;/td&gt;&lt;td&gt;Description1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Name2&lt;/td&gt;&lt;td&gt;Description2&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Name3&lt;/td&gt;&lt;td&gt;Description3&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

    </body>
</html>

我的问题是如何在python脚本中正确设置其格式,以使其发送< > 而不是&lt; &gt;

My question is how do I format it correctly in my python script such that it sends < and > instead of & lt; and & gt;

推荐答案

如果您确信表中的数据都没有未经验证的用户输入,请告诉Jinja不要转义html:

If you're confident none of the data in the table has un-validated user input then tell Jinja not to escape the html:

{{ tStrToLoad | safe }}

但是,您可能希望避免使用Flask-table,而只需自己传递项目列表即可.如果您还在单独的列表中传递一些标头,则模板代码可以变得更加通用:

However you may wish to avoid using Flask-tables, and just pass the items list yourself. The template code can become more generic if you also pass some headers in a separate list:

        headers = ['Name','Description']
        return render_template('index.html',
                               headers = headers,
                               objects = items)

然后手动构建表:

{% if objects %}
  <table id='result' class='display'>
    <thead>
      <tr>
      {% for header in headers %}
        <th>{{header}}</th>
      {% endfor %}
      </tr>
    </thead>

    <tbody>
    {% for object in objects %} 
      <tr>
    {% for k, val in object.items() %}
        <td>{{val}}</td>
      {% endfor %}
      </tr>
    {% endfor %}        
    </tbody>
  </table>
{% endif %}

如您所见,这使您不必将任何值硬编码到模板中.

As you can see, this prevents you from having to hard-code any values into the template.

此表的语法也与数据表兼容,因此,如果您按照零配置示例,则您的表会变得更漂亮,并且可以具有内置的搜索栏和分页功能.让我知道您是否需要进一步的指导.

The syntax of this table is also compatible with datatables, so you if you load the CSS and JS files as per the zero configuration example then your table becomes prettier and can have a builtin search bar and pagination. Let me know if you need further direction on how to do this.

这篇关于在python上使用html格式化flask-table输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 11:19