2012年12月14日金曜日

web2pyでdatatablesを使う。ついでに、mongodb

staticフォルダにdatatablesのアーカイブを解凍した物をおいてある。

==ビューの部分ここから==

<html>
<head>
<script src="{{=URL(r=request,c='static',f='DataTables-1.9.3/media/js/jquery.js')}}" type="text/javascript"></script>
<script src="{{=URL(r=request,c='static',f='DataTables-1.9.3/media/js/jquery.dataTables.js')}}" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" type="text/css">

<style type="text/css" title="currentStyle">
    @import "{{=URL(r=request,c='static',f='DataTables-1.9.3/media/css/demo_page.css')}}";
    @import "{{=URL(r=request,c='static',f='DataTables-1.9.3/media/css/jquery.dataTables_themeroller.css')}}";
    @import "{{=URL(r=request,c='static',f='DataTables-1.9.3/examples/examples_support/themes/smoothness/jquery-ui-1.8.4.custom.css')}}";
</style>

<script type="text/javascript" charset="utf-8">
         $(document).ready(function() {
            oTable = $('#example').dataTable({
               "bJQueryUI": true,
               "sPaginationType": "full_numbers",
               "aaSorting": [[0,"desc"]]
            });
         });
</script>
</head>
<body>
<table width="100%" class="display" id="example" border="0" cellpadding="5" cellspacing="5">
   <thead>
      <tr>
        <th>Collection stop date</th>
        <th>Collection stop time</th>
        <th>Category</th>
        <th>ID</th>
        <th>Station</th>
        <th>Detected Nuclide</th>
        <th>Link data</th>
     </tr>
  </thead>
<tbody>
{{
    import datetime
    
    old_id = 0
    for col in records:
      current_id = col["rn_sampleid"]
      utime=col["rn_unixtime"]
      dt=datetime.datetime.fromtimestamp(utime)
    if current_id != old_id:
       old_id = current_id
}}
<tr>
    <td>{{=dt.strftime('%Y/%m/%d')}}</td>
    <td>{{=dt.strftime('%H:%M:%S')}}</td>
    <td>{{=col["rn_category"]}}</td>
    <td>{{=col["rn_sampleid"]}}</td>
    <td>{{=col["rn_station"]}}</td>
    <td>{{=col["rn_nucl"].replace('+',' ')}}</td>
    <td>{{="S"}}</td>
</tr>{{pass}}{{pass}}
</tbody>
  <tfoot>
    <tr>
       <th>Collection stop date</th>
       <th>Collection stop time</th>
       <th>Category</th>
       <th>ID</th>
       <th>Station</th>
       <th>Detected Nuclide</th>
       <th>Link data</th>
    </tr>
  </tfoot>
</table>
</body>
</html>
==ビューの部分ここまで==
==pythonの部分ここから==
import pymongo
import time

def index():

    conn = pymongo.Connection('ip.to.your.machine',27017)
    db   = conn.datafusion
    col  = db.overlaps

    start_time_tuple = (2012,9 ,1,0,0,0,0,0,0)
    end_time_tuple   = (2012,12,1,0,0,0,0,0,0)
    start_unixtime = time.mktime(start_time_tuple)
    end_unixtime   = time.mktime(end_time_tuple)

    data_full = col.find({"rn_unixtime":{"$gte":start_unixtime,"$lt":end_unixtime}})
    conn.disconnect()
        
    return dict(records=data_full)
==pythonの部分ここまで==

0 件のコメント:

コメントを投稿