使用数据表和烧瓶进行服务器端处理

时间:2017-02-25 22:01:13

标签: python json flask datatables server-side-scripting

我尝试使用我的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>  

使用Javascript:

$(document).ready(function() {
    $('#myTable').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "/page_test"
    } );
});

查看功能:

@app.route('/page_test')
def page_test():
    data = json.dumps(meas[2])
    print data
    return data

meas [2]是我的词:

[dict((c.description[i][0], value) \
               for i, value in enumerate(row)) for row in c.fetchall()]

在&#34;打印数据&#34;一切都打印得很好,像这样:

{&#34; MeanCurrent&#34;:0.05933,&#34; Temperature&#34;:15.095,&#34; YawAngle&#34;:0.0,&#34; MeanVoltage&#34;:0.67367,&# 34; VoltageDC&#34;:3.18309,&#34; PowerSec&#34;:0.06923,&#34; FurlingAngle&#34;:-0.2266828184,&#34; WindSpeed&#34;:1.884,&#34; VapourPressure&#34 ;:1649.25948,&#34;湿度&#34;:0.4266,&#34; WindSector&#34;:0,&#34; AirDensity&#34;:1.23051,&#34; BarPressure&#34;:1020.259,&# 34;时间&#34;:&#34; 2015-04-22 20:58:28&#34;,&#34; RPM&#34;:0.0,&#34; ID&#34;:1357}这个乘以行数

然而,当我运行应用程序并插入查询时,表格只显示&#34; th&#34;标签和#34;处理......&#34;写在桌子上面,没有显示数据。在我的烧瓶应用程序的终端,显示一个巨大的字符串,这是一个小样本:

/ page_test绘制= 2及列%5B0%5D%5Bdata%5D = 0&安培;柱%5B0%5D%5Bname%5D =安培;柱%5B0%5D%5Bsearchable%5D =真安培;柱%5B0%图5D%5Borderable%5D =真安培;柱%5B0%5D%5Bsearch%5D%5Bvalue%5D =安培;柱%5B0%5D%5Bsearch%5D%5Bregex%5D =假安培;柱%5B1%5D%5Bdata%5D = 1安培;柱%5B1%5D%5Bname%5D =安培;柱%5B1%5D%5Bsearchable%5

以下是截图:screenshot from web app

每次点击th标签,都会再次出现相同的字符串。我似乎错过了一些重要的东西,但由于这是我的第一个应用程序,我无法弄清楚它是什么。任何修改代码的建议都将受到赞赏。

3 个答案:

答案 0 :(得分:3)

Server-side processing是一个设置,要求您拥有一个数据库脚本,能够在您自己的服务器/数据库上复制DataTables的许多核心功能,以管理非常大的数据集。

传递给脚本的所有信息(例如,长信息串)是您需要用来查询数据库以返回DataTables渲染结果的输入。

如果您希望DataTables从Flask端点加载数据,然后在内部管理所有处理,请进行以下修改:删除serverSide设置并添加列配置,以便您的数据在正确的位置结束:< / p>

<强>使用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"},
            ...
        ]
    } );
});

DataTable Initialization Options:如果点击“列”按钮,它会显示每个“列”接受的各种配置,是否可排序,可订购,自定义渲染等等。

<强>的Python:

from flask import jsonify

@app.route('/page_test')
def page_test():
    return jsonify(meas[2])

答案 1 :(得分:0)

2个潜在问题:

1)确保您没有使用苗条的jQuery版本。苗条的jQuery没有ajax,因此,如果您在浏览器中进行检查,则会看到一些错误消息,例如“如果不是函数,则为h.ajax”。这将无限期地显示“正在处理...”。这让我难过了一段时间,因为引导程序默认情况下使用的是jQuery的苗条版本,而我正在使用引导程序。

2)无论如何我都不是数据表方面的专家,但是我正在研究它。要使用服务器端显示数据,数据的格式必须像这样:

return_me = json.dumps({"data": [[1, 1.3478, 23.2223, ...]]})

即,当返回列名称的字典时,我无法使它工作……我不得不专门将其称为字典字段“ data”,然后给出所有值。我可能在这里自己弄错了东西,但这是行得通的。

答案 2 :(得分:0)

Using flask, you will get a faster response than using C#.In Server Side Processing, You can send sorted/searched/header value via form in flask. 

在Flask中,您可以使用以下方法获取数据表值:

Datatabledata= request.form

在SSP中,您可以使用以下命令将其他数据发送到烧瓶:

"data":function(data){ 
data.input="Text"
}