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.

class sage.misc.html.HTMLFragmentFactory

Bases: sage.structure.sage_object.SageObject

eval(*args, **kwds)
iframe(*args, **kwds)
table(*args, **kwds)
class sage.misc.html.HtmlFragment

Bases: str, sage.structure.sage_object.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>
_rich_repr_(display_manager, **kwds)

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
class sage.misc.html.WarnIfNotPrinted

Bases: sage.structure.sage_object.SageObject

To be removed when the deprecation for trac ticket #18292 expires.

classmethod skip_pretty_print(obj)
sage.misc.html.math_parse(s)

Replace TeX-$ with Mathjax equations.

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 HtmlFragment instance.

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: pretty_print(sage.misc.html.math_parse('This is $2+2$.'))
This is <script type="math/tex">2+2</script>.
sage: pretty_print(sage.misc.html.math_parse('This is $$2+2$$.'))
This is <script type="math/tex; mode=display">2+2</script>.
sage: pretty_print(sage.misc.html.math_parse('This is \\[2+2\\].'))
This is <script type="math/tex; mode=display">2+2</script>.
sage: pretty_print(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>.
sage.misc.html.old_and_deprecated_wrapper(method)

Wrapper to reinstate the old behavior of html

See trac ticket #18292.

EXAMPLES:

sage: from sage.misc.html import HtmlFragment, old_and_deprecated_wrapper
sage: @old_and_deprecated_wrapper
....: def foo(): 
....:     return HtmlFragment('foo')

The old behavior is to print and return nothing:

sage: import sage.misc.html
sage: sage.misc.html._old_and_deprecated_behavior = True
sage: f = foo()
foo
sage: f
<BLANKLINE>
sage: type(f)
<class 'sage.misc.html.WarnIfNotPrinted'>
sage: import sage.misc.html

The new behavior will be to return a HTML fragment:

sage: sage.misc.html._old_and_deprecated_behavior = False
sage: f = foo()
sage: f 
foo
sage: type(f)
<class 'sage.misc.html.HtmlFragment'>

A deprecation warning is generated if the html output is not printed:

sage: sage.misc.html._old_and_deprecated_behavior = True
sage: def html_without_print():
....:    html('output without pretty_print')     
sage: html_without_print()
output without pretty_print
doctest:...: DeprecationWarning:  html(...) will change soon to return HTML instead of printing it. Instead use pretty_print(html(...)) for strings or just pretty_print(...) for math. 
See http://trac.sagemath.org/18292 for details.

sage: def html_with_print():
....:    pretty_print(html('output with pretty_print'))
sage: html_with_print()
output with pretty_print