def displayhook(self, plain_text, rich_output): """ Backend implementation of the displayhook INPUT: - ``plain_text`` -- instance of :class:`~sage.repl.rich_output.output_basic.OutputPlainText`. The plain text version of the output. - ``rich_output`` -- instance of an output container class (subclass of :class:`~sage.repl.rich_output.output_basic.OutputBase`). Guaranteed to be one of the output containers returned from :meth:`supported_output`, possibly the same as ``plain_text``. OUTPUT: The IPython notebook display hook returns the IPython display data, a pair of dictionaries. The first dictionary contains mime types as keys and the respective output as value. The second dictionary is metadata. EXAMPLES:: sage: from sage.repl.rich_output.output_basic import OutputPlainText sage: plain_text = OutputPlainText.example() sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook sage: backend = BackendIPythonNotebook() sage: backend.displayhook(plain_text, plain_text) ({u'text/plain': u'Example plain text output'}, {}) """ if isinstance(rich_output, OutputPlainText): return ({u"text/plain": rich_output.text.get_unicode()}, {}) elif isinstance(rich_output, OutputAsciiArt): return ({u"text/plain": rich_output.ascii_art.get_unicode()}, {}) elif isinstance(rich_output, OutputUnicodeArt): return ({u"text/plain": rich_output.unicode_art.get_unicode()}, {}) elif isinstance(rich_output, OutputLatex): return ({u"text/html": rich_output.mathjax(), u"text/plain": plain_text.text.get_unicode()}, {}) elif isinstance(rich_output, OutputHtml): return ({u"text/html": rich_output.html.get(), u"text/plain": plain_text.text.get()}, {}) elif isinstance(rich_output, OutputImagePng): return ({u"image/png": rich_output.png.get(), u"text/plain": plain_text.text.get_unicode()}, {}) elif isinstance(rich_output, OutputImageJpg): return ({u"image/jpeg": rich_output.jpg.get(), u"text/plain": plain_text.text.get_unicode()}, {}) elif isinstance(rich_output, OutputImageSvg): return ({u"image/svg+xml": rich_output.svg.get(), u"text/plain": plain_text.text.get_unicode()}, {}) elif isinstance(rich_output, OutputImagePdf): return ({u"image/png": rich_output.png.get(), u"text/plain": plain_text.text.get_unicode()}, {}) elif isinstance(rich_output, OutputSceneJmol): from sage.repl.display.jsmol_iframe import JSMolHtml jsmol = JSMolHtml(rich_output, height=500) return ({u"text/html": jsmol.iframe(), u"text/plain": plain_text.text.get_unicode()}, {}) else: raise TypeError("rich_output type not supported")
def displayhook(self, plain_text, rich_output): """ Backend implementation of the displayhook INPUT: - ``plain_text`` -- instance of :class:`~sage.repl.rich_output.output_basic.OutputPlainText`. The plain text version of the output. - ``rich_output`` -- instance of an output container class (subclass of :class:`~sage.repl.rich_output.output_basic.OutputBase`). Guaranteed to be one of the output containers returned from :meth:`supported_output`, possibly the same as ``plain_text``. OUTPUT: The IPython notebook display hook returns the IPython display data, a pair of dictionaries. The first dictionary contains mime types as keys and the respective output as value. The second dictionary is metadata. EXAMPLES:: sage: from sage.repl.rich_output.output_basic import OutputPlainText sage: plain_text = OutputPlainText.example() sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook sage: backend = BackendIPythonNotebook() sage: backend.displayhook(plain_text, plain_text) ({u'text/plain': u'Example plain text output'}, {}) """ if isinstance(rich_output, OutputPlainText): return ({u'text/plain': rich_output.text.get_unicode()}, {}) elif isinstance(rich_output, OutputAsciiArt): return ({u'text/plain': rich_output.ascii_art.get_unicode()}, {}) elif isinstance(rich_output, OutputUnicodeArt): return ({u'text/plain': rich_output.unicode_art.get_unicode()}, {}) elif isinstance(rich_output, OutputLatex): return ({ u'text/html': rich_output.mathjax(), u'text/plain': plain_text.text.get_unicode(), }, {}) elif isinstance(rich_output, OutputImagePng): return ({ u'image/png': rich_output.png.get(), u'text/plain': plain_text.text.get_unicode(), }, {}) elif isinstance(rich_output, OutputImageJpg): return ({ u'image/jpeg': rich_output.jpg.get(), u'text/plain': plain_text.text.get_unicode(), }, {}) elif isinstance(rich_output, OutputImageSvg): return ({ u'image/svg+xml': rich_output.svg.get(), u'text/plain': plain_text.text.get_unicode(), }, {}) elif isinstance(rich_output, OutputImagePdf): return ({ u'image/png': rich_output.png.get(), u'text/plain': plain_text.text.get_unicode(), }, {}) elif isinstance(rich_output, OutputSceneJmol): from sage.repl.display.jsmol_iframe import JSMolHtml jsmol = JSMolHtml(rich_output, height=500) return ({ u'text/html': jsmol.iframe(), u'text/plain': plain_text.text.get_unicode(), }, {}) else: raise TypeError('rich_output type not supported')
def displayhook(self, plain_text, rich_output): """ Backend implementation of the displayhook INPUT: - ``plain_text`` -- instance of :class:`~sage.repl.rich_output.output_basic.OutputPlainText`. The plain text version of the output. - ``rich_output`` -- instance of an output container class (subclass of :class:`~sage.repl.rich_output.output_basic.OutputBase`). Guaranteed to be one of the output containers returned from :meth:`supported_output`, possibly the same as ``plain_text``. OUTPUT: The IPython notebook display hook returns the IPython display data, a pair of dictionaries. The first dictionary contains mime types as keys and the respective output as value. The second dictionary is metadata. EXAMPLES:: sage: from sage.repl.rich_output.output_basic import OutputPlainText sage: plain_text = OutputPlainText.example() sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook sage: backend = BackendIPythonNotebook() sage: backend.displayhook(plain_text, plain_text) ({'text/plain': 'Example plain text output'}, {}) """ if isinstance(rich_output, OutputPlainText): return ({'text/plain': rich_output.text.get_str()}, {}) elif isinstance(rich_output, OutputAsciiArt): return ({'text/plain': rich_output.ascii_art.get_str()}, {}) elif isinstance(rich_output, OutputUnicodeArt): return ({'text/plain': rich_output.unicode_art.get_str()}, {}) elif isinstance(rich_output, OutputLatex): return ({ 'text/latex': rich_output.latex.get_str(), 'text/plain': plain_text.text.get_str(), }, {}) elif isinstance(rich_output, OutputHtml): data = { 'text/html': rich_output.html.get_str(), 'text/plain': plain_text.text.get_str() } if rich_output.latex: data['text/latex'] = rich_output.latex.get_str() return (data, {}) elif isinstance(rich_output, OutputImagePng): return ({ 'image/png': rich_output.png.get(), 'text/plain': plain_text.text.get_str(), }, {}) elif isinstance(rich_output, OutputImageGif): return ({ 'text/html': rich_output.html_fragment(), 'text/plain': plain_text.text.get_str(), }, {}) elif isinstance(rich_output, OutputImageJpg): return ({ 'image/jpeg': rich_output.jpg.get(), 'text/plain': plain_text.text.get_str(), }, {}) elif isinstance(rich_output, OutputImageSvg): return ({ 'image/svg+xml': rich_output.svg.get(), 'text/plain': plain_text.text.get_str(), }, {}) elif isinstance(rich_output, OutputImagePdf): return ({ 'image/png': rich_output.png.get(), 'text/plain': plain_text.text.get_str(), }, {}) elif isinstance(rich_output, OutputSceneJmol): from sage.repl.display.jsmol_iframe import JSMolHtml jsmol = JSMolHtml(rich_output, height=500) return ({ 'text/html': jsmol.iframe(), 'text/plain': plain_text.text.get_str(), }, {}) elif isinstance(rich_output, OutputSceneThreejs): escaped_html = rich_output.html.get_str().replace('"', '"') iframe = IFRAME_TEMPLATE.format( escaped_html=escaped_html, width='100%', height=400, ) return ({ 'text/html': iframe, 'text/plain': plain_text.text.get_str(), }, {}) else: raise TypeError('rich_output type not supported')