Skip to content

Commit 5ba3276

Browse files
committed
serious effort to make javascript table viewer usable in ipython
notebook. Still has issues: the table appears, then disappears!! I don't really know what IPython.kernel.execute is doing; just relying on @jakevdp's blog post: http://jakevdp.github.io/blog/2013/06/01/ipython-notebook-javascript-python-communication/
1 parent 81174d5 commit 5ba3276

2 files changed

Lines changed: 54 additions & 16 deletions

File tree

‎astropy/table/pprint.py‎

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ def _more_tabcol(tabcol, max_lines=None, max_width=None, show_name=True,
431431

432432

433433
def _jsviewer(display_length=50,
434+
ipynb=False,
435+
tableid=None,
434436
css_urls=("http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css",
435437
"http://jquery-datatables-editable.googlecode.com/svn/trunk/media/css/demo_page.css",
436438
"http://jquery-datatables-column-filter.googlecode.com/svn/trunk/media/css/demo_table.css")):
@@ -440,20 +442,53 @@ def _jsviewer(display_length=50,
440442
for L in display_length_menu:
441443
if display_length not in L:
442444
L.insert(0,display_length)
443-
js = (['<link rel="stylesheet" href="{css}" type="text/css">'.format(css=css) for css in css_urls] +
444-
['<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>',
445-
'<script class="jsbin" src="http://datatables.net/download/build/jquery.dataTables.nightly.js"></script>',
446-
'''<script>
447-
$(document).ready(function() {{
448-
$('#table').dataTable({{
449-
"iDisplayLength": {display_length},
450-
"aLengthMenu": {display_length_menu},
451-
"bJQueryUI": true,
452-
"sPaginationType": "full_numbers"
453-
}});
454-
}} );
455-
</script>'''.format(display_length=display_length,
456-
display_length_menu=display_length_menu)
457-
])
445+
js = ['<link rel="stylesheet" href="{css}" type="text/css">'.format(css=css) for css in css_urls]
446+
if ipynb:
447+
js += ['<script class="jsbin" src="http://datatables.net/download/build/jquery.dataTables.nightly.js"></script>',
448+
'''<script>
449+
function html_repr_full() {{
450+
var kernel = IPython.notebook.kernel;
451+
var button = $("#MakeTableBrowseable{tid}");
452+
var tablename = button.parents()[4].getElementsByClassName("input_area")[0].innerText;
453+
var command = tablename + ".pformat(html=True, max_lines=1000, max_width=1000)";
454+
var result = kernel.execute(command, {{'output': callback}}, {{silent:true}});
455+
}};
456+
function callback(output_type, out) {{
457+
var button = $("#MakeTableBrowseable{tid}");
458+
button[0].parentNode.innerHTML = out.data;
459+
}};
460+
function make_table() {{
461+
$('#table').dataTable({{
462+
"iDisplayLength": {display_length},
463+
"aLengthMenu": {display_length_menu},
464+
"bJQueryUI": true,
465+
"sPaginationType": "full_numbers"
466+
}});
467+
}};
468+
function replace_table() {{
469+
html_repr_full();
470+
make_table();
471+
}};
472+
</script>
473+
<button id='MakeTableBrowseable{tid}' onclick="replace_table()">Make Table Browseable</button>
474+
'''.format(display_length=display_length,
475+
display_length_menu=display_length_menu,
476+
tid=tableid)
477+
]
478+
else:
479+
js += ['<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>']
480+
js += ['<script class="jsbin" src="http://datatables.net/download/build/jquery.dataTables.nightly.js"></script>',
481+
'''<script>
482+
$(document).ready(function() {{
483+
$('#table').dataTable({{
484+
"iDisplayLength": {display_length},
485+
"aLengthMenu": {display_length_menu},
486+
"bJQueryUI": true,
487+
"sPaginationType": "full_numbers"
488+
}});
489+
}} );
490+
</script>'''.format(display_length=display_length,
491+
display_length_menu=display_length_menu)
492+
]
458493

459494
return js

‎astropy/table/table.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,8 @@ def show_in_browser(self,
13551355

13561356
if jsviewer:
13571357
js = _jsviewer(**jskwargs)
1358+
else:
1359+
js = []
13581360

13591361
css = ["<style>{0}</style>".format(css)]
13601362
html = "\n".join(['<!DOCTYPE html>','<html>'] + css + js + linelist + ['</html>'])
@@ -1440,7 +1442,8 @@ def more(self, max_lines=None, max_width=None, show_name=True,
14401442
show_unit)
14411443

14421444
def _repr_html_(self):
1443-
js = _jsviewer()
1445+
self._id = id(self)
1446+
js = _jsviewer(ipynb=True, tableid=self._id)
14441447
lines = self.pformat(html=True)
14451448
return ''.join(js+lines)
14461449

0 commit comments

Comments
 (0)