HTML Fragments¶
This module defines a HTML fragment class, which holds a piece of HTML. This is primarily used in browser-based notebooks, though it might be useful for creating static pages as well.
This module defines MathJax
, an object of which performs the task of
producing an HTML representation of any object. The produced HTML is
renderable in a browser-based notebook with the help of MathJax.
- class sage.misc.html.HTMLFragmentFactory[source]¶
Bases:
SageObject
- eval(s, locals=None)[source]¶
Evaluate embedded <sage> tags.
INPUT:
s
– stringglobals
– dictionary; the global variables when evaluatings
. Default: the current global variables.
OUTPUT:
HtmlFragment
EXAMPLES:
sage: a = 123 sage: html.eval('<sage>a</sage>') \(123\) sage: html.eval('<sage>a</sage>', locals={'a': 456}) \(456\)
>>> from sage.all import * >>> a = Integer(123) >>> html.eval('<sage>a</sage>') \(123\) >>> html.eval('<sage>a</sage>', locals={'a': Integer(456)}) \(456\)
- iframe(url, height=400, width=800)[source]¶
Generate an iframe HTML fragment.
INPUT:
url
– string; a url, either with or without URI scheme (defaults to “http”), or an absolute file pathheight
– the number of pixels for the page height Defaults to 400width
– the number of pixels for the page width Defaults to 800
OUTPUT:
HtmlFragment
EXAMPLES:
sage: pretty_print(html.iframe("sagemath.org")) <iframe height="400" width="800" src="http://sagemath.org"></iframe> sage: pretty_print(html.iframe("http://sagemath.org",30,40)) <iframe height="30" width="40" src="http://sagemath.org"></iframe> sage: pretty_print(html.iframe("https://sagemath.org",30)) <iframe height="30" width="800" src="https://sagemath.org"></iframe> sage: pretty_print(html.iframe("/home/admin/0/data/filename")) <iframe height="400" width="800" src="file:///home/admin/0/data/filename"></iframe> sage: pretty_print(html.iframe('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA' ....: 'AUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBA' ....: 'AO9TXL0Y4OHwAAAABJRU5ErkJggg=="')) <iframe height="400" width="800" src="http://data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==""></iframe>
>>> from sage.all import * >>> pretty_print(html.iframe("sagemath.org")) <iframe height="400" width="800" src="http://sagemath.org"></iframe> >>> pretty_print(html.iframe("http://sagemath.org",Integer(30),Integer(40))) <iframe height="30" width="40" src="http://sagemath.org"></iframe> >>> pretty_print(html.iframe("https://sagemath.org",Integer(30))) <iframe height="30" width="800" src="https://sagemath.org"></iframe> >>> pretty_print(html.iframe("/home/admin/0/data/filename")) <iframe height="400" width="800" src="file:///home/admin/0/data/filename"></iframe> >>> pretty_print(html.iframe('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA' ... 'AUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBA' ... 'AO9TXL0Y4OHwAAAABJRU5ErkJggg=="')) <iframe height="400" width="800" src="http://data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==""></iframe>
- class sage.misc.html.HtmlFragment[source]¶
Bases:
str
,SageObject
A HTML fragment.
This is a piece of HTML, usually not a complete document. For example, just a
<div>...</div>
piece and not the entire<html>...</html>
.EXAMPLES:
sage: from sage.misc.html import HtmlFragment sage: HtmlFragment('<b>test</b>') <b>test</b>
>>> from sage.all import * >>> from sage.misc.html import HtmlFragment >>> HtmlFragment('<b>test</b>') <b>test</b>
- _rich_repr_(display_manager, **kwds)[source]¶
Rich Output Magic Method.
See
sage.repl.rich_output
for details.EXAMPLES:
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: h = sage.misc.html.HtmlFragment('<b>old</b>') sage: h._rich_repr_(dm) # the doctest backend does not support html OutputPlainText container
>>> from sage.all import * >>> from sage.repl.rich_output import get_display_manager >>> dm = get_display_manager() >>> h = sage.misc.html.HtmlFragment('<b>old</b>') >>> h._rich_repr_(dm) # the doctest backend does not support html OutputPlainText container
- class sage.misc.html.MathJax[source]¶
Bases:
object
Render LaTeX input using MathJax. This returns a
MathJaxExpr
.EXAMPLES:
sage: from sage.misc.html import MathJax sage: MathJax()(3) <html>\[3\]</html> sage: MathJax()(ZZ) <html>\[\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}\]</html>
>>> from sage.all import * >>> from sage.misc.html import MathJax >>> MathJax()(Integer(3)) <html>\[3\]</html> >>> MathJax()(ZZ) <html>\[\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}\]</html>
- eval(x, globals=None, locals=None, mode='display', combine_all=False)[source]¶
Render LaTeX input using MathJax. This returns a
MathJaxExpr
.INPUT:
x
– a Sage objectglobals
– a globals dictionarylocals
– extra local variables used when evaluating Sage code inx
mode
– string (default:'display'
);'display'
for displaymath,'inline'
for inline math, or'plain'
for just the LaTeX code without the surrounding html and script tagscombine_all
– boolean (default:False
); ifcombine_all
isTrue
and the input is a tuple, then it does not return a tuple and instead returns a string with all the elements separated by a single space
OUTPUT:
MathJaxExpr
EXAMPLES:
sage: from sage.misc.html import MathJax sage: MathJax().eval(3, mode='display') <html>\[3\]</html> sage: MathJax().eval(3, mode='inline') <html>\(3\)</html> sage: MathJax().eval(type(3), mode='inline') <html>\(\verb|<class|\verb| |\verb|'sage.rings.integer.Integer'>|\)</html>
>>> from sage.all import * >>> from sage.misc.html import MathJax >>> MathJax().eval(Integer(3), mode='display') <html>\[3\]</html> >>> MathJax().eval(Integer(3), mode='inline') <html>\(3\)</html> >>> MathJax().eval(type(Integer(3)), mode='inline') <html>\(\verb|<class|\verb| |\verb|'sage.rings.integer.Integer'>|\)</html>
- class sage.misc.html.MathJaxExpr(y)[source]¶
Bases:
object
An arbitrary MathJax expression that can be nicely concatenated.
EXAMPLES:
sage: from sage.misc.html import MathJaxExpr sage: MathJaxExpr("a^{2}") + MathJaxExpr("x^{-1}") a^{2}x^{-1}
>>> from sage.all import * >>> from sage.misc.html import MathJaxExpr >>> MathJaxExpr("a^{2}") + MathJaxExpr("x^{-1}") a^{2}x^{-1}
- sage.misc.html.html(obj, concatenate=True, strict=False)[source]¶
Construct a HTML fragment.
INPUT:
obj
– anything. An object for which you want an HTML representationconcatenate
– ifTrue
, combine HTML representations of elements of the containerobj
strict
– ifTrue
, construct an HTML representation ofobj
even ifobj
is a string
OUTPUT:
HtmlFragment
EXAMPLES:
sage: h = html('<hr>'); pretty_print(h) <hr> sage: type(h) <class 'sage.misc.html.HtmlFragment'> sage: html(1/2) <html>\(\displaystyle \frac{1}{2}\)</html> sage: html('<a href="http://sagemath.org">sagemath</a>') <a href="http://sagemath.org">sagemath</a> sage: html('<a href="http://sagemath.org">sagemath</a>', strict=True) <html>\(\displaystyle \verb|<a|\verb| |\verb|href="http://sagemath.org">sagemath</a>|\)</html>
>>> from sage.all import * >>> h = html('<hr>'); pretty_print(h) <hr> >>> type(h) <class 'sage.misc.html.HtmlFragment'> >>> html(Integer(1)/Integer(2)) <html>\(\displaystyle \frac{1}{2}\)</html> >>> html('<a href="http://sagemath.org">sagemath</a>') <a href="http://sagemath.org">sagemath</a> >>> html('<a href="http://sagemath.org">sagemath</a>', strict=True) <html>\(\displaystyle \verb|<a|\verb| |\verb|href="http://sagemath.org">sagemath</a>|\)</html>
Display preference
align_latex
affects rendering of LaTeX expressions:sage: from sage.repl.rich_output.display_manager import get_display_manager sage: dm = get_display_manager() sage: dm.preferences.align_latex = 'left' sage: html(1/2) <html>\(\displaystyle \frac{1}{2}\)</html> sage: dm.preferences.align_latex = 'center' sage: html(1/2) <html>\[\frac{1}{2}\]</html> sage: dm.preferences.align_latex = None # same with left sage: html(1/2) <html>\(\displaystyle \frac{1}{2}\)</html>
>>> from sage.all import * >>> from sage.repl.rich_output.display_manager import get_display_manager >>> dm = get_display_manager() >>> dm.preferences.align_latex = 'left' >>> html(Integer(1)/Integer(2)) <html>\(\displaystyle \frac{1}{2}\)</html> >>> dm.preferences.align_latex = 'center' >>> html(Integer(1)/Integer(2)) <html>\[\frac{1}{2}\]</html> >>> dm.preferences.align_latex = None # same with left >>> html(Integer(1)/Integer(2)) <html>\(\displaystyle \frac{1}{2}\)</html>
- sage.misc.html.math_parse(s)[source]¶
Transform the string
s
with TeX maths to an HTML string renderable by MathJax.INPUT:
s
– string
OUTPUT:
HtmlFragment
Specifically this method does the following:
Replace all
$text$
's by\(text\)
Replace all
$$text$$
's by\[text\]
Replace all
$
's by$
's. Note that this has precedence over the above two cases.
EXAMPLES:
sage: print(sage.misc.html.math_parse('This is $2+2$.')) This is \(2+2\). sage: print(sage.misc.html.math_parse('This is $$2+2$$.')) This is \[2+2\]. sage: print(sage.misc.html.math_parse('This is \\[2+2\\].')) This is \[2+2\]. sage: print(sage.misc.html.math_parse(r'$2+2$ is rendered to $2+2$.')) <span>$</span>2+2<span>$</span> is rendered to \(2+2\).
>>> from sage.all import * >>> print(sage.misc.html.math_parse('This is $2+2$.')) This is \(2+2\). >>> print(sage.misc.html.math_parse('This is $$2+2$$.')) This is \[2+2\]. >>> print(sage.misc.html.math_parse('This is \\[2+2\\].')) This is \[2+2\]. >>> print(sage.misc.html.math_parse(r'$2+2$ is rendered to $2+2$.')) <span>$</span>2+2<span>$</span> is rendered to \(2+2\).
- sage.misc.html.pretty_print_default(enable=True)[source]¶
Enable or disable default pretty printing.
Pretty printing means rendering things in HTML and by MathJax so that a browser-based frontend can render real math.
This function is pretty useless without the notebook, it should not be in the global namespace.
INPUT:
enable
– boolean (default:True
); ifTrue
, turn on pretty printing. IfFalse
, turn it off.
EXAMPLES:
sage: pretty_print_default(True) sage: 'foo' # the doctest backend does not support html 'foo' sage: pretty_print_default(False) sage: 'foo' 'foo'
>>> from sage.all import * >>> pretty_print_default(True) >>> 'foo' # the doctest backend does not support html 'foo' >>> pretty_print_default(False) >>> 'foo' 'foo'