Skip to content

Commit 934f830

Browse files
committed
moved jsviewer code to its own jsviewer.py file and cleaned up a little.
The viewer can now be used without internet access
1 parent 3e05c95 commit 934f830

4 files changed

Lines changed: 112 additions & 76 deletions

File tree

‎astropy/table/jsviewer.py‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

‎astropy/table/pprint.py‎

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -432,75 +432,3 @@ def _more_tabcol(tabcol, max_lines=None, max_width=None, show_name=True,
432432
if i0 >= len(tabcol) - delta_lines:
433433
i0 = len(tabcol) - delta_lines
434434
print "\n"
435-
436-
437-
def _jsviewer(display_length=50,
438-
ipynb=False,
439-
tableid=None,
440-
css_urls=("http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css",
441-
"http://jquery-datatables-editable.googlecode.com/svn/trunk/media/css/demo_page.css",
442-
"http://jquery-datatables-column-filter.googlecode.com/svn/trunk/media/css/demo_table.css")):
443-
444-
display_length_menu = [[10, 25, 50, 100, 500, 1000, -1],
445-
[10, 25, 50, 100, 500, 1000, "All"]]
446-
for L in display_length_menu:
447-
if display_length not in L:
448-
L.insert(0,display_length)
449-
js = ['<link rel="stylesheet" href="{css}" type="text/css">'.format(css=css) for css in css_urls]
450-
if ipynb:
451-
js += ['<script class="jsbin" src="http://datatables.net/download/build/jquery.dataTables.nightly.js"></script>',
452-
'''<script>
453-
function html_repr_full() {{
454-
var kernel = IPython.notebook.kernel;
455-
var button = $("#MakeTableBrowseable{tid}");
456-
var tablename = button.parents()[4].getElementsByClassName("input_area")[0].innerText;
457-
tablename = tablename.replace(/\s+/g, '');
458-
var command = "print ''.join(" + tablename + ".pformat(html=True, max_lines=1000, max_width=1000, tableid={tid}))";
459-
console.log(command)
460-
var result = kernel.execute(command, {{'output': callback}}, {{silent:false}});
461-
console.log(result);
462-
}};
463-
function callback(output_type, out) {{
464-
console.log(output_type);
465-
console.log(out);
466-
var button = $("#MakeTableBrowseable{tid}");
467-
button[0].parentNode.innerHTML = out.data;
468-
return out.data;
469-
}};
470-
function make_table_browseable() {{
471-
console.log("$('#table{tid}').dataTable()");
472-
$('#table{tid}').dataTable({{
473-
"iDisplayLength": {display_length},
474-
"aLengthMenu": {display_length_menu},
475-
"bJQueryUI": true,
476-
"sPaginationType": "full_numbers"
477-
}});
478-
}};
479-
function replace_table() {{
480-
html_repr_full();
481-
make_table_browseable();
482-
}};
483-
</script>
484-
<button id='MakeTableBrowseable{tid}' onclick="make_table_browseable()">Make Table Browseable</button>
485-
'''.format(display_length=display_length,
486-
display_length_menu=display_length_menu,
487-
tid=tableid)
488-
]
489-
else:
490-
js += ['<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>']
491-
js += ['<script class="jsbin" src="http://datatables.net/download/build/jquery.dataTables.nightly.js"></script>',
492-
'''<script>
493-
$(document).ready(function() {{
494-
$('#table{tid}').dataTable({{
495-
"iDisplayLength": {display_length},
496-
"aLengthMenu": {display_length_menu},
497-
"bJQueryUI": true,
498-
"sPaginationType": "full_numbers"
499-
}});
500-
}} );
501-
</script>'''.format(display_length=display_length,
502-
display_length_menu=display_length_menu,
503-
tid=tableid)
504-
]
505-
506-
return js

‎astropy/table/setup_package.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ def get_extensions():
1717
libraries=libraries,)
1818

1919
return [table_ext]
20+
21+
def get_package_data():
22+
paths = [os.path.join('data', '*.js'),
23+
os.path.join('data', '*.css'),
24+
]
25+
return {'astropy.table': paths}
26+

‎astropy/table/table.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from ..units import Unit
1313
from .. import log
1414
from ..utils import OrderedDict, isiterable, deprecated, deprecated_attribute
15-
from .pprint import _pformat_table, _pformat_col, _pformat_col_iter, _more_tabcol, _jsviewer
15+
from .pprint import _pformat_table, _pformat_col, _pformat_col_iter, _more_tabcol
16+
from jsviewer import JSViewer
1617
from ..utils.console import color_print
1718
from ..config import ConfigurationItem
1819
from ..io import registry as io_registry
@@ -1337,9 +1338,9 @@ def show_in_browser(self,
13371338
jsviewer : bool
13381339
If True, prepends some javascript headers so that the table is
13391340
rendered as a https://datatables.net data table. This allows
1340-
in-browser searching & sorting
1341+
in-browser searching & sorting. See `JSViewer`
13411342
jskwargs : dict
1342-
Passed to the `_jsviewer` generator function
1343+
Passed to the `JSViewer` init
13431344
13441345
Returns
13451346
-------
@@ -1355,7 +1356,8 @@ def show_in_browser(self,
13551356
tableid=id(self))
13561357

13571358
if jsviewer:
1358-
js = _jsviewer(tableid=id(self), **jskwargs)
1359+
jsv = JSViewer(**jskwargs)
1360+
js = jsv.command_line(tableid=id(self))
13591361
else:
13601362
js = []
13611363

0 commit comments

Comments
 (0)