ipythonでdatatablesを使う

何かと便利なipythonですが、datatablesも使えます。
datatablesとは、https://datatables.net/
です。

多分見てもらう方が早いと思います。
早速使い方ですが、

from IPython.display import HTML
%%javascript
require.config({
  paths: {
    dataTables: '//cdn.datatables.net/1.10.12/js/jquery.dataTables.min'
  }
});
HTML('''
<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css">
<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
    </tr>
    <tr>
      <td>Garrett Winters</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>63</td>
      <td>2011/07/25</td>
      <td>$170,750</td>
    </tr>
    <tr>
      <td>Ashton Cox</td>
      <td>Junior Technical Author</td>
      <td>San Francisco</td>
      <td>66</td>
      <td>2009/01/12</td>
      <td>$86,000</td>
    </tr>
  </tbody>
</table>
<script>
  require(['dataTables'], function(){
    $('#example').DataTable();
  });
</script>
''')

とりあえず、これだけでも動きます。

デフォルトで、インクリメンタルサーチとページャが付きます。
他にもオプションで、いくつか機能があります。
CSVダウンロードとか便利で使っています。


今回のサンプルでは、tableタグを直書きしていますが
pandasのDataFrameから、「to_html」するとtableタグが出力されるので
それに、datatablesをかませるとかいう使い方も出来ます。


便利ですね。
それでは、、、