|
| 1 | +# Licensed under a 3-clause BSD style license - see LICENSE.rst |
| 2 | +# TODO: need to download some images used by the jquery-ui css file: |
| 3 | +# images/ui-icons_888888_256x240.png |
| 4 | +import os |
| 5 | + |
| 6 | +data_path = os.path.join(os.path.abspath(os.path.dirname(__file__)),'data') |
| 7 | + |
| 8 | +ipynb_js_script = """ |
| 9 | +<script class="jsbin" src="{datapath}/jquery.dataTables.nightly.js"></script> |
| 10 | +<script> |
| 11 | + function html_repr_full() {{ |
| 12 | + var kernel = IPython.notebook.kernel; |
| 13 | + var button = $("#MakeTableBrowseable{tid}"); |
| 14 | + var tablename = button.parents()[4].getElementsByClassName("input_area")[0].innerText; |
| 15 | + tablename = tablename.replace(/\s+/g, ''); |
| 16 | + var command = "print ''.join(" + tablename + ".pformat(html=True, max_lines=1000, max_width=1000, tableid={tid}))"; |
| 17 | + console.log(command) |
| 18 | + var result = kernel.execute(command, {{'output': callback}}, {{silent:false}}); |
| 19 | + console.log(result); |
| 20 | + }}; |
| 21 | + function callback(output_type, out) {{ |
| 22 | + console.log(output_type); |
| 23 | + console.log(out); |
| 24 | + var button = $("#MakeTableBrowseable{tid}"); |
| 25 | + button[0].parentNode.innerHTML = out.data; |
| 26 | + return out.data; |
| 27 | + }}; |
| 28 | + function make_table_browseable() {{ |
| 29 | + console.log("$('#table{tid}').dataTable()"); |
| 30 | + $('#table{tid}').dataTable({{ |
| 31 | + "iDisplayLength": {display_length}, |
| 32 | + "aLengthMenu": {display_length_menu}, |
| 33 | + "bJQueryUI": true, |
| 34 | + "sPaginationType": "full_numbers" |
| 35 | + }}); |
| 36 | + }}; |
| 37 | + function replace_table() {{ |
| 38 | + html_repr_full(); |
| 39 | + make_table_browseable(); |
| 40 | + }}; |
| 41 | +</script> |
| 42 | +<button id='MakeTableBrowseable{tid}' onclick="make_table_browseable()">Make Table Browseable</button> |
| 43 | +""" |
| 44 | + |
| 45 | +commandline_js_script = """ |
| 46 | +<script> |
| 47 | + $(document).ready(function() {{ |
| 48 | + $('#table{tid}').dataTable({{ |
| 49 | + "iDisplayLength": {display_length}, |
| 50 | + "aLengthMenu": {display_length_menu}, |
| 51 | + "bJQueryUI": true, |
| 52 | + "sPaginationType": "full_numbers" |
| 53 | + }}); |
| 54 | + }} ); |
| 55 | +</script> |
| 56 | +""" |
| 57 | + |
| 58 | +class JSViewer(object): |
| 59 | + |
| 60 | + def __init__(self, |
| 61 | + css_files=['jquery-ui.css','demo_page.css','demo_table.css'], |
| 62 | + display_length=50): |
| 63 | + self.css_urls = ["file://"+os.path.join(data_path,c) for c in css_files] |
| 64 | + self.display_length_menu = [[10, 25, 50, 100, 500, 1000, -1], |
| 65 | + [10, 25, 50, 100, 500, 1000, "All"]] |
| 66 | + self.display_length = display_length |
| 67 | + for L in self.display_length_menu: |
| 68 | + if display_length not in L: |
| 69 | + L.insert(0,display_length) |
| 70 | + |
| 71 | + def _jquery_file(self): |
| 72 | + # downloaded from http://ajax.googleapis.com/ajax/libs/jquery/1/ |
| 73 | + return '<script src="file://{data_path}/jquery.js"></script>'.format(data_path=data_path) |
| 74 | + |
| 75 | + def _jstable_file(self): |
| 76 | + # downloaded from http://datatables.net/download/build/ |
| 77 | + return '<script class="jsbin" src="file://{data_path}/jquery.dataTables.nightly.js"></script>'.format(data_path=data_path) |
| 78 | + |
| 79 | + def _css_files(self): |
| 80 | + return ['<link rel="stylesheet" href="{css}" type="text/css">'.format(css=css) for css in self.css_urls] |
| 81 | + |
| 82 | + def ipynb(self, tableid): |
| 83 | + js = self._css_files() |
| 84 | + js.append(self._jstable_file()) |
| 85 | + js.append(ipynb_js_script.format(display_length=self.display_length, |
| 86 | + display_length_menu=self.display_length_menu, |
| 87 | + tid=tableid, |
| 88 | + data_path="file://"+data_path)) |
| 89 | + return js |
| 90 | + |
| 91 | + def command_line(self, tableid=0): |
| 92 | + js = self._css_files() |
| 93 | + js.append(self._jquery_file()) |
| 94 | + js.append(self._jstable_file()) |
| 95 | + |
| 96 | + js.append(commandline_js_script.format(display_length=self.display_length, |
| 97 | + display_length_menu=self.display_length_menu, |
| 98 | + tid=tableid)) |
| 99 | + return js |
0 commit comments