Created
September 13, 2017 20:57
-
-
Save saimn/31375465514debbc3b4e1ec941d936af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-09-13T20:52:24.515612Z", | |
"start_time": "2017-09-13T20:52:24.512281Z" | |
}, | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import sys\n", | |
"from astropy.table import Table" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-09-13T20:52:30.485872Z", | |
"start_time": "2017-09-13T20:52:30.446976Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<Table length=1>\n", | |
"<table id=\"table140451023928344\" class=\"table-striped table-bordered table-condensed\">\n", | |
"<thead><tr><th>col0</th><th>col1</th></tr></thead>\n", | |
"<thead><tr><th>str37</th><th>int64</th></tr></thead>\n", | |
"<tr><td><a href="http://coucou.fr">foobar</a></td><td>1</td></tr>\n", | |
"</table>" | |
], | |
"text/plain": [ | |
"<Table length=1>\n", | |
" col0 col1\n", | |
" str37 int64\n", | |
"------------------------------------- -----\n", | |
"<a href=\"http://coucou.fr\">foobar</a> 1" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"t = Table([['<a href=\"http://coucou.fr\">foobar</a>'], [1]])\n", | |
"t" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Problem : link is escaped" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-09-13T20:52:42.586245Z", | |
"start_time": "2017-09-13T20:52:42.571924Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<html>\n", | |
" <head>\n", | |
" <meta charset=\"utf-8\"/>\n", | |
" <meta content=\"text/html;charset=UTF-8\" http-equiv=\"Content-type\"/>\n", | |
" </head>\n", | |
" <body>\n", | |
" <table>\n", | |
" <thead>\n", | |
" <tr>\n", | |
" <th>col0</th>\n", | |
" <th>col1</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tr>\n", | |
" <td><a href=\"http://coucou.fr\">foobar</a></td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" </table>\n", | |
" </body>\n", | |
"</html>\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"t.write(sys.stdout, format='html')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## The HTML writer has an option for this" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-09-13T20:53:16.285225Z", | |
"start_time": "2017-09-13T20:53:16.245765Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<html>\n", | |
" <head>\n", | |
" <meta charset=\"utf-8\"/>\n", | |
" <meta content=\"text/html;charset=UTF-8\" http-equiv=\"Content-type\"/>\n", | |
" </head>\n", | |
" <body>\n", | |
" <table>\n", | |
" <thead>\n", | |
" <tr>\n", | |
" <th>col0</th>\n", | |
" <th>col1</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tr>\n", | |
" <td><a href=\"http://coucou.fr\">foobar</a></td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" </table>\n", | |
" </body>\n", | |
"</html>\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"t.write(sys.stdout, format='html', htmldict=dict(raw_html_cols=['col0']))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## But not the jsviewer writer ..." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-09-13T20:54:26.703570Z", | |
"start_time": "2017-09-13T20:54:26.666401Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"ename": "TypeError", | |
"evalue": "write_table_jsviewer() got an unexpected keyword argument 'htmldict'", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-6-a8724cb71d5e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mformat\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'jsviewer'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhtmldict\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mraw_html_cols\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'col0'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m~/dev/astropy/astropy/table/table.py\u001b[0m in \u001b[0;36mwrite\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 2548\u001b[0m \u001b[0mpassed\u001b[0m \u001b[0mthrough\u001b[0m \u001b[0mto\u001b[0m \u001b[0mthe\u001b[0m \u001b[0munderlying\u001b[0m \u001b[0mdata\u001b[0m \u001b[0mreader\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;34m~\u001b[0m\u001b[0mastropy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mascii\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2549\u001b[0m \"\"\"\n\u001b[0;32m-> 2550\u001b[0;31m \u001b[0mio_registry\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2551\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2552\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy_data\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m~/dev/astropy/astropy/io/registry.py\u001b[0m in \u001b[0;36mwrite\u001b[0;34m(data, *args, **kwargs)\u001b[0m\n\u001b[1;32m 579\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 580\u001b[0m \u001b[0mwriter\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_writer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__class__\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 581\u001b[0;31m \u001b[0mwriter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 582\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 583\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mTypeError\u001b[0m: write_table_jsviewer() got an unexpected keyword argument 'htmldict'" | |
] | |
} | |
], | |
"source": [ | |
"t.write(sys.stdout, format='jsviewer', htmldict=dict(raw_html_cols=['col0']))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Redefine the jsviewer writer to fix that" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-09-13T20:55:40.465726Z", | |
"start_time": "2017-09-13T20:55:40.434693Z" | |
}, | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from astropy.table.jsviewer import DEFAULT_CSS, JSViewer\n", | |
"\n", | |
"def write_table_jsviewer(table, filename, table_id=None, max_lines=5000,\n", | |
" table_class=\"display compact\", jskwargs=None,\n", | |
" css=DEFAULT_CSS, html_kwargs=None):\n", | |
" if table_id is None:\n", | |
" table_id = 'table{id}'.format(id=id(table))\n", | |
"\n", | |
" jskwargs = jskwargs or {}\n", | |
" jsv = JSViewer(**jskwargs)\n", | |
"\n", | |
" sortable_columns = [i for i, col in enumerate(table.columns.values())\n", | |
" if col.dtype.kind in 'iufc']\n", | |
" htmldict = {\n", | |
" 'table_id': table_id,\n", | |
" 'table_class': table_class,\n", | |
" 'css': css,\n", | |
" 'cssfiles': jsv.css_urls,\n", | |
" 'jsfiles': jsv.jquery_urls,\n", | |
" 'js': jsv.html_js(table_id=table_id, sort_columns=sortable_columns)\n", | |
" }\n", | |
" if html_kwargs:\n", | |
" htmldict.update(html_kwargs)\n", | |
"\n", | |
" if max_lines < len(table):\n", | |
" table = table[:max_lines]\n", | |
" table.write(filename, format='html', htmldict=htmldict)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-09-13T20:55:45.356402Z", | |
"start_time": "2017-09-13T20:55:45.344016Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<html>\n", | |
" <head>\n", | |
" <meta charset=\"utf-8\"/>\n", | |
" <meta content=\"text/html;charset=UTF-8\" http-equiv=\"Content-type\"/>\n", | |
" <style>\n", | |
"body {font-family: sans-serif;}\n", | |
"table.dataTable {width: auto !important; margin: 0 !important;}\n", | |
".dataTables_filter, .dataTables_paginate {float: left !important; margin-left:1em}\n", | |
" </style>\n", | |
" <link href=\"https://cdn.datatables.net/1.10.12/css/jquery.dataTables.css\" rel=\"stylesheet\" type=\"text/css\"/>\n", | |
" <script src=\"https://code.jquery.com/jquery-3.1.1.min.js\">\n", | |
" </script>\n", | |
" <script src=\"https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js\">\n", | |
" </script>\n", | |
" </head>\n", | |
" <body>\n", | |
" <script>\n", | |
"var astropy_sort_num = function(a, b) {\n", | |
" var a_num = parseFloat(a);\n", | |
" var b_num = parseFloat(b);\n", | |
"\n", | |
" if (isNaN(a_num) && isNaN(b_num))\n", | |
" return ((a < b) ? -1 : ((a > b) ? 1 : 0));\n", | |
" else if (!isNaN(a_num) && !isNaN(b_num))\n", | |
" return ((a_num < b_num) ? -1 : ((a_num > b_num) ? 1 : 0));\n", | |
" else\n", | |
" return isNaN(a_num) ? -1 : 1;\n", | |
"}\n", | |
"\n", | |
"jQuery.extend( jQuery.fn.dataTableExt.oSort, {\n", | |
" \"optionalnum-asc\": astropy_sort_num,\n", | |
" \"optionalnum-desc\": function (a,b) { return -astropy_sort_num(a, b); }\n", | |
"});\n", | |
"\n", | |
"$(document).ready(function() {\n", | |
" $('#table140451023928344').dataTable({\n", | |
" order: [],\n", | |
" pageLength: 50,\n", | |
" lengthMenu: [[10, 25, 50, 100, 500, 1000, -1], [10, 25, 50, 100, 500, 1000, 'All']],\n", | |
" pagingType: \"full_numbers\",\n", | |
" columnDefs: [{targets: [1], type: \"optionalnum\"}]\n", | |
" });\n", | |
"} ); </script>\n", | |
" <table class=\"display compact\" id=\"table140451023928344\">\n", | |
" <thead>\n", | |
" <tr>\n", | |
" <th>col0</th>\n", | |
" <th>col1</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tr>\n", | |
" <td><a href=\"http://coucou.fr\">foobar</a></td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" </table>\n", | |
" </body>\n", | |
"</html>\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"write_table_jsviewer(t, sys.stdout, html_kwargs=dict(raw_html_cols=['col0']))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment