HTML typesetting for the notebook

class sage.misc.html.HTML
eval(s, globals=None, locals=None)

Return an html representation for an object s.

If s has a method _html_(), call that. Otherwise, call math_parse() on str(s), evaluate any variables in the result, and add some html preamble and postamble.

In any case, print the resulting html string. This method always returns an empty string.

EXAMPLES:

sage: html.eval('<hr>')
<html><font color='black'><hr></font></html>
''
iframe(url, height=400, width=800)

Put an existing web page into a worksheet.

INPUT:

  • url – a url string, either with or without URI scheme (defaults to “http”).
  • height – the number of pixels for the page height. Defaults to 400.
  • width – the number of pixels for the page width. Defaults to 800.

OUTPUT:

Opens the url in a worksheet. If the url is a regular web page it will appear in the worksheet. This was originally intended to bring GeoGebra worksheets into Sage, but it can be used for many other purposes.

EXAMPLES:

sage: html.iframe("sagemath.org")
<html><font color='black'><iframe height="400" width="800"
src="http://sagemath.org"></iframe></font></html>
sage: html.iframe("http://sagemath.org",30,40)
<html><font color='black'><iframe height="30" width="40"
src="http://sagemath.org"></iframe></font></html>
sage: html.iframe("https://sagemath.org",30)
<html><font color='black'><iframe height="30" width="800"
src="https://sagemath.org"></iframe></font></html>
sage: html.iframe("/home/admin/0/data/filename")
<html><font color='black'><iframe height="400" width="800"
src="/home/admin/0/data/filename"></iframe></font></html>
sage: html.iframe('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA'
... 'AUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBA'
... 'AO9TXL0Y4OHwAAAABJRU5ErkJggg=="')
<html><font color='black'><iframe height="400" width="800"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==""></iframe></font></html>

AUTHOR:

  • Bruce Cohen (2011-06-14)
table(x, header=False)

Print a nested list as a HTML table. Strings of html will be parsed for math inside dollar and double-dollar signs. 2D graphics will be displayed in the cells. Expressions will be latexed.

INPUT:

  • x – a list of lists (i.e., a list of table rows)
  • header – a row of headers. If True, then the first row of the table is taken to be the header.

EXAMPLES:

sage: html.table([(i, j, i == j) for i in [0..1] for j in [0..1]])
<html>
<div class="notruncate">
<table class="table_form">
<tbody>
<tr class ="row-a">
<td><script type="math/tex">0</script></td>
<td><script type="math/tex">0</script></td>
<td><script type="math/tex">\mathrm{True}</script></td>
</tr>
<tr class ="row-b">
<td><script type="math/tex">0</script></td>
<td><script type="math/tex">1</script></td>
<td><script type="math/tex">\mathrm{False}</script></td>
</tr>
<tr class ="row-a">
<td><script type="math/tex">1</script></td>
<td><script type="math/tex">0</script></td>
<td><script type="math/tex">\mathrm{False}</script></td>
</tr>
<tr class ="row-b">
<td><script type="math/tex">1</script></td>
<td><script type="math/tex">1</script></td>
<td><script type="math/tex">\mathrm{True}</script></td>
</tr>
</tbody>
</table>
</div>
</html>

sage: html.table([(x,n(sin(x), digits=2)) for x in [0..3]], header = ["$x$", "$\sin(x)$"])
<html>
<div class="notruncate">
<table class="table_form">
<tbody>
<tr>
<th><script type="math/tex">x</script></th>
<th><script type="math/tex">\sin(x)</script></th>
</tr>
<tr class ="row-a">
<td><script type="math/tex">0</script></td>
<td><script type="math/tex">0.00</script></td>
</tr>
<tr class ="row-b">
<td><script type="math/tex">1</script></td>
<td><script type="math/tex">0.84</script></td>
</tr>
<tr class ="row-a">
<td><script type="math/tex">2</script></td>
<td><script type="math/tex">0.91</script></td>
</tr>
<tr class ="row-b">
<td><script type="math/tex">3</script></td>
<td><script type="math/tex">0.14</script></td>
</tr>
</tbody>
</table>
</div>
</html>
class sage.misc.html.HTMLExpr

Bases: str

A class for HTML expression

sage.misc.html.html(s, globals=None, locals=None)

Display the given HTML expression in the notebook.

INPUT:

  • s – a string

OUTPUT:

  • prints a code that embeds HTML in the output.

By default in the notebook an output cell has two parts, first a plain text preformat part, then second a general HTML part (not pre). If you call html(s) at any point then that adds something that will be displayed in the preformated part in html.

EXAMPLES:

sage: html('<a href="http://sagemath.org">sagemath</a>')
<html><font color='black'><a href="http://sagemath.org">sagemath</a></font></html>
sage: html('<hr>')
<html><font color='black'><hr></font></html>
sage.misc.html.math_parse(s)

Turn the HTML-ish string s that can have $$ and $’s in it into pure HTML. See below for a precise definition of what this means.

INPUT:

  • s – a string

OUTPUT:

  • a string.

Do the following:

  • Replace all $ text $‘s by <script type="math/tex"> text </script>
  • Replace all $$ text $$‘s by <script type="math/tex; mode=display"> text </script>
  • Replace all \ $‘s by $‘s. Note that in the above two cases nothing is done if the $ is preceeded by a backslash.
  • Replace all \[ text \]‘s by <script type="math/tex; mode=display"> text </script>

EXAMPLES:

sage: sage.misc.html.math_parse('This is $2+2$.')
'This is <script type="math/tex">2+2</script>.'
sage: sage.misc.html.math_parse('This is $$2+2$$.')
'This is <script type="math/tex; mode=display">2+2</script>.'
sage: sage.misc.html.math_parse('This is \\[2+2\\].')
'This is <script type="math/tex; mode=display">2+2</script>.'
sage: sage.misc.html.math_parse(r'This is \[2+2\].')
'This is <script type="math/tex; mode=display">2+2</script>.'

TESTS:

sage: sage.misc.html.math_parse(r'This $$is $2+2$.')
'This $$is <script type="math/tex">2+2</script>.'

Previous topic

Functional notation

Next topic

Tables

This Page