在 python 上使用 html 格式化 flask-table 输出
Formatting flask-table output with html on python
在 python 方面不是很有经验的用户,刚在家时收到 python。我正在研究将 flask-table 的输出整合到网站中。以下是我的 python 代码,它为我的网站创建了一个简单的 table。
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 来显示。
<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>
我没有用数据显示 table,而是在黑色背景中显示以下输出
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 代码由此产生的。
<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/>
<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>
</body>
</html>
我的问题是如何在我的 python 脚本中正确格式化它,以便它发送 < 和 > < 和 >
如果您确信 table 中的数据 none 有 un-validated 用户输入,那么告诉 Jinja 不要转义 html:
{{ tStrToLoad | safe }}
但是您可能希望避免使用 Flask-tables,而只是自己传递项目列表。如果您还在单独的列表中传递一些 headers,模板代码可以变得更通用:
headers = ['Name','Description']
return render_template('index.html',
headers = headers,
objects = items)
然后手动构建 table:
{% 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 %}
如您所见,这使您不必 hard-code 将任何值添加到模板中。
这个 table 的语法也与数据 tables 兼容,所以如果你按照 zero configuration 的例子加载 CSS 和 JS 文件,那么你您的 table 变得更漂亮并且可以有一个内置的搜索栏和分页。如果您需要有关如何执行此操作的进一步指导,请告诉我。
在 python 方面不是很有经验的用户,刚在家时收到 python。我正在研究将 flask-table 的输出整合到网站中。以下是我的 python 代码,它为我的网站创建了一个简单的 table。
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 来显示。
<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>
我没有用数据显示 table,而是在黑色背景中显示以下输出
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 代码由此产生的。
<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/>
<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>
</body>
</html>
我的问题是如何在我的 python 脚本中正确格式化它,以便它发送 < 和 > < 和 >
如果您确信 table 中的数据 none 有 un-validated 用户输入,那么告诉 Jinja 不要转义 html:
{{ tStrToLoad | safe }}
但是您可能希望避免使用 Flask-tables,而只是自己传递项目列表。如果您还在单独的列表中传递一些 headers,模板代码可以变得更通用:
headers = ['Name','Description']
return render_template('index.html',
headers = headers,
objects = items)
然后手动构建 table:
{% 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 %}
如您所见,这使您不必 hard-code 将任何值添加到模板中。
这个 table 的语法也与数据 tables 兼容,所以如果你按照 zero configuration 的例子加载 CSS 和 JS 文件,那么你您的 table 变得更漂亮并且可以有一个内置的搜索栏和分页。如果您需要有关如何执行此操作的进一步指导,请告诉我。