from os.path import abspath, dirname, join
import textwrap
import pytest
from astropy.coordinates import SkyCoord
from astropy.time import Time
from astropy.table.table import Table
from astropy import extern
from astropy.utils.compat.optional_deps import HAS_BLEACH, HAS_IPYTHON # noqa
from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH
EXTERN_DIR = abspath(join(dirname(extern.__file__), 'jquery', 'data'))
REFERENCE = """
"""
TPL = (' \n'
' {0} | \n'
' {1} | \n'
'
')
def format_lines(col1, col2):
col1_format = getattr(col1.info, 'default_format', lambda x: x)
col2_format = getattr(col2.info, 'default_format', lambda x: x)
return '\n'.join(TPL.format(col1_format(v1), col2_format(v2))
for v1, v2 in zip(col1, col2))
def test_write_jsviewer_default(tmpdir):
t = Table()
t['a'] = [1, 2, 3, 4, 5]
t['b'] = ['a', 'b', 'c', 'd', 'e']
t['a'].unit = 'm'
tmpfile = tmpdir.join('test.html').strpath
t.write(tmpfile, format='jsviewer')
ref = REFERENCE % dict(
lines=format_lines(t['a'], t['b']),
table_class='display compact',
table_id=f'table{id(t)}',
length='50',
display_length='10, 25, 50, 100, 500, 1000',
datatables_css_url='https://cdn.datatables.net/1.10.12/css/jquery.dataTables.css',
datatables_js_url='https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js',
jquery_url='https://code.jquery.com/jquery-3.1.1.min.js'
)
with open(tmpfile) as f:
assert f.read().strip() == ref.strip()
def test_write_jsviewer_overwrite(tmpdir):
t = Table()
t['a'] = [1, 2, 3, 4, 5]
t['b'] = ['a', 'b', 'c', 'd', 'e']
t['a'].unit = 'm'
tmpfile = tmpdir.join('test.html').strpath
# normal write
t.write(tmpfile, format='jsviewer')
# errors on overwrite
with pytest.raises(OSError, match=_NOT_OVERWRITING_MSG_MATCH):
t.write(tmpfile, format='jsviewer')
# unless specified
t.write(tmpfile, format='jsviewer', overwrite=True)
@pytest.mark.parametrize('mixin', [
Time(['J2000', 'J2001']),
Time([50000., 50001.0001], format='mjd'),
SkyCoord(ra=[100., 110.], dec=[-10., 10.], unit='deg')])
def test_write_jsviewer_mixin(tmpdir, mixin):
t = Table()
t['a'] = [1, 2]
t['b'] = mixin
t['a'].unit = 'm'
tmpfile = tmpdir.join('test.html').strpath
t.write(tmpfile, format='jsviewer')
ref = REFERENCE % dict(
lines=format_lines(t['a'], t['b']),
table_class='display compact',
table_id=f'table{id(t)}',
length='50',
display_length='10, 25, 50, 100, 500, 1000',
datatables_css_url='https://cdn.datatables.net/1.10.12/css/jquery.dataTables.css',
datatables_js_url='https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js',
jquery_url='https://code.jquery.com/jquery-3.1.1.min.js'
)
with open(tmpfile) as f:
assert f.read().strip() == ref.strip()
@pytest.mark.skipif('not HAS_BLEACH')
def test_write_jsviewer_options(tmpdir):
t = Table()
t['a'] = [1, 2, 3, 4, 5]
t['b'] = ['a', 'b', 'c', 'd', 'e']
t['a'].unit = 'm'
tmpfile = tmpdir.join('test.html').strpath
t.write(tmpfile, format='jsviewer', table_id='test', max_lines=3,
jskwargs={'display_length': 5}, table_class='display hover',
htmldict=dict(raw_html_cols='b'))
ref = REFERENCE % dict(
lines=format_lines(t['a'][:3], t['b'][:3]),
table_class='display hover',
table_id='test',
length='5',
display_length='5, 10, 25, 50, 100, 500, 1000',
datatables_css_url='https://cdn.datatables.net/1.10.12/css/jquery.dataTables.css',
datatables_js_url='https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js',
jquery_url='https://code.jquery.com/jquery-3.1.1.min.js'
)
with open(tmpfile) as f:
assert f.read().strip() == ref.strip()
def test_write_jsviewer_local(tmpdir):
t = Table()
t['a'] = [1, 2, 3, 4, 5]
t['b'] = ['a', 'b', 'c', 'd', 'e']
t['a'].unit = 'm'
tmpfile = tmpdir.join('test.html').strpath
t.write(tmpfile, format='jsviewer', table_id='test',
jskwargs={'use_local_files': True})
ref = REFERENCE % dict(
lines=format_lines(t['a'], t['b']),
table_class='display compact',
table_id='test',
length='50',
display_length='10, 25, 50, 100, 500, 1000',
datatables_css_url='file://' + join(EXTERN_DIR, 'css', 'jquery.dataTables.css'),
datatables_js_url='file://' + join(EXTERN_DIR, 'js', 'jquery.dataTables.min.js'),
jquery_url='file://' + join(EXTERN_DIR, 'js', 'jquery-3.1.1.min.js')
)
with open(tmpfile) as f:
assert f.read().strip() == ref.strip()
@pytest.mark.skipif('not HAS_IPYTHON')
def test_show_in_notebook():
t = Table()
t['a'] = [1, 2, 3, 4, 5]
t['b'] = ['b', 'c', 'a', 'd', 'e']
htmlstr_windx = t.show_in_notebook().data # should default to 'idx'
htmlstr_windx_named = t.show_in_notebook(show_row_index='realidx').data
htmlstr_woindx = t.show_in_notebook(show_row_index=False).data
assert (textwrap.dedent("""
idx | a | b |
0 | 1 | b |
1 | 2 | c |
2 | 3 | a |
3 | 4 | d |
4 | 5 | e |
""").strip() in htmlstr_windx)
assert 'realidx | a | b |
' in htmlstr_windx_named
assert 'a | b |
' in htmlstr_woindx