tzo*_*zou 5 python json datatables server-side-scripting flask
我正在尝试使用 Flask 应用程序上的 sqlite 数据实现服务器端处理。我是新手,所以我无法弄清楚出了什么问题。到目前为止,我已经到了这个:
HTML :
<table id="myTable" class="table table-striped" style="width:100%" >
<thead>
<tr>
<th>Time</th>
<th>Mean Current</th>
<th>Vapour Pressure</th>
<th>Mean Voltage</th>
<th>Temperature</th>
<th>Humidity</th>
<th>Bar Pressure</th>
<th>RPM</th>
<th>Wind Sector</th>
<th>Wind Speed</th>
<th>Air Density</th>
<th>DC Voltage</th>
<th>Power Sector</th>
<th>Furling Angle</th>
<th>Yaw angle</th>
</tr>
</thead>
</table>
Run Code Online (Sandbox Code Playgroud)
Javascript :
$(document).ready(function() {
$('#myTable').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "/page_test"
} );
});
Run Code Online (Sandbox Code Playgroud)
查看功能:
@app.route('/page_test')
def page_test():
data = json.dumps(meas[2])
print data
return data
Run Code Online (Sandbox Code Playgroud)
meas[2] 是我的字典:
[dict((c.description[i][0], value) \
for i, value in enumerate(row)) for row in c.fetchall()]
Run Code Online (Sandbox Code Playgroud)
在“打印数据”中,一切都打印得很好,如下所示:
{"MeanCurrent": 0.05933, "Temperature": 15.095, "YawAngle": 0.0, "MeanVoltage": 0.67367, "VoltageDC": 3.18309, "PowerSec": 0.06923, "FurlingAngle": -0.2266828184, "WindSpeed": 1.884, "VapourPressure": 1649.25948, "Humidity": 0.4266, "WindSector": 0, "AirDensity": 1.23051, "BarPressure": 1020.259, "time": "2015-04-22 20:58:28", "RPM": 0.0, "ID": 1357}
Run Code Online (Sandbox Code Playgroud)
这乘以行数。
但是,当我运行应用程序并插入查询时,表格仅显示<th>标签,并且表格顶部写有“正在处理...”而没有显示数据。在我的烧瓶应用程序的终端,显示了一个巨大的字符串,这是一个小样本:
/page_test?draw=2&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5
Run Code Online (Sandbox Code Playgroud)
这是一个屏幕截图:
每次我单击th标签时,都会再次出现相同的字符串。似乎我遗漏了一些重要的东西,但由于这是我的第一个应用程序,我无法弄清楚它是什么。任何修改代码的建议将不胜感激。
服务器端处理是一种设置,要求您拥有一个能够在您自己的服务器/数据库上复制 DataTables 的许多核心功能的数据库脚本,以管理非常大的数据集。
传递到脚本的所有信息(例如,长信息字符串)都是您需要用来查询数据库以返回 DataTables 呈现结果的输入。
如果您希望 DataTables 从 Flask 端点加载数据,然后在内部管理所有处理,请进行以下修改:删除设置serverSide并添加列配置,以便您的数据最终位于正确的位置:
JavaScript:
$(document).ready(function() {
$('#myTable').DataTable( {
"processing": true,
"ajax": "/page_test",
// add column definitions to map your json to the table
"columns": [
{data: "time"},
{data: "MeanCurrent"},
...
]
} );
});
Run Code Online (Sandbox Code Playgroud)
数据表初始化选项:如果单击“列”按钮,它会向您显示每个“列”接受的各种配置,无论是可排序、可排序、自定义渲染等...
Python:
from flask import jsonify
@app.route('/page_test')
def page_test():
return jsonify(meas[2])
Run Code Online (Sandbox Code Playgroud)