Sharedsupport / 2017-01-02-105313-html.sagewsOpen in CoCalc
Examples for support purposes...
html??
   File: /projects/sage/sage-7.3/local/lib/python2.7/site-packages/smc_sagews/sage_salvus.py
   Source:
   class HTML:
    """
    Cell mode that renders everything after %html as HTML

    EXAMPLES::

        ---
        %html
        <h1>A Title</h1>
        <h2>Subtitle</h2>

        ---
        %html(hide=True)
        <h1>A Title</h1>
        <h2>Subtitle</h2>

        ---
        %html("<h1>A title</h1>", hide=False)

        ---
        %html(hide=False) <h1>Title</h1>

    """
    def __init__(self, hide=False):
        self._hide = hide

    def __call__(self, *args, **kwds):
        if len(kwds) > 0 and len(args) == 0:
            return HTML(**kwds)
        if len(args) > 0:
            self._render(args[0], **kwds)

    def _render(self, s, hide=None):
        if hide is None:
            hide = self._hide
        if hide:
            salvus.hide('input')
        salvus.html(s)

    def table(self, rows = None, header=False):
        """
        Renders a given matrix or nested list as an HTML table.

        Arguments::

        * **rows**: the rows of the table as a list of lists
        * **header**: if True, the first row is formatted as a header (default: False)
        """
        # TODO: support columns as in http://doc.sagemath.org/html/en/reference/misc/sage/misc/table.html
        assert rows is not None, '"rows" is a mandatory argument, should be a list of lists'

        from sage.matrix.matrix import is_Matrix
        import numpy as np

        if is_Matrix(rows):
            table = list(rows) # list of Sage Vectors
        elif isinstance(rows, np.ndarray):
            table = rows.tolist()
        else:
            table = rows

        assert isinstance(table, (tuple, list)), '"rows" must be a list of lists'

        def as_unicode(s):
            '''
            This not only deals with unicode strings, but also converts e.g. `Integer` objects to a str
            '''
            if not isinstance(s, unicode):
                try:
                    return unicode(s, 'utf8')
                except:
                    return unicode(str(s), 'utf8')
            return s

        def mk_row(row, header=False):
            is_vector = hasattr(row, 'is_vector') and row.is_vector()
            assert isinstance(row, (tuple, list)) or is_vector, '"rows" must contain lists or vectors for each row'
            tag = 'th' if header else 'td'
            row = [u'<{tag}>{}</{tag}>'.format(as_unicode(_), tag = tag) for _ in row]
            return u'<tr>{}</tr>'.format(u''.join(row))

        thead = u'<thead>{}</thead>'.format(mk_row(table.pop(0), header=True)) if header else ''
        h_rows = [mk_row(row) for row in table]
        html_table = u'<table style="width: auto;" class="table table-bordered">{}<tbody>{}</tbody></table>'
        self(html_table.format(thead, ''.join(h_rows)))

salvus.html??
   File: /projects/sage/sage-7.3/local/lib/python2.7/site-packages/smc_sagews/sage_server.py
   Source:
       def html(self, html, done=False, once=None):
        """
        Display html in the output stream.

        EXAMPLE:

            salvus.html("<b>Hi</b>")
        """
        self._flush_stdio()
        self._send_output(html=unicode8(html), id=self._id, done=done, once=once)