A bit more robust in escaping:

try:
    from html import escape  # python 3.x
except ImportError:
    from cgi import escape  # python 2.x
from IPython.display import HTML

def table(data):
    rows = [["<th>%s</th>"%escape(str(i)) for i in data[0]]]
    rows.extend([["<td>%s</td>"%escape(str(i)) for i in row] for row in
data[1:]])
    s = '\n'.join(['<tr>%s</tr>'%(''.join(row)) for row in rows])
    return HTML('<table>%s</table>'%s)

table([('Header 1', 'Header 2'), (1,'<'), (3,4)])

On Fri, Sep 9, 2016 at 9:28 AM Jason Grout <ja...@jasongrout.org> wrote:

> I don't think we have a good table-printing function built into IPython. A
> simple one is probably pretty straightforward:
>
> try:
>     from html import escape  # python 3.x
> except ImportError:
>     from cgi import escape  # python 2.x
> from IPython.display import HTML
>
> def table(data):
>     rows = [["<th>%s</th>"%escape(i) for i in data[0]]]
>     rows.extend([["<td>%s</td>"%i for i in row] for row in data[1:]])
>     s = '\n'.join(['<tr>%s</tr>'%(''.join(row)) for row in rows])
>     return HTML('<table>%s</table>'%s)
>
> table([('Header 1', 'Header 2'), (1,2), (3,4)])
>
>
> On Fri, Sep 9, 2016 at 8:53 AM kcrisman <kcris...@gmail.com> wrote:
>
>> Sorry for the necropost, but I couldn't find any other canonical place to
>> mention this:
>>
>> https://ask.sagemath.org/question/34782/how-to-render-html-or-latex-tables-in-jupyter-notebook/
>> Likely this is a fairly easy hook thing or the pretty_print issue but
>> thought it should be here for completeness.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "sage-devel" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to sage-devel+unsubscr...@googlegroups.com.
>> To post to this group, send email to sage-devel@googlegroups.com.
>> Visit this group at https://groups.google.com/group/sage-devel.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to