From 5c1c232846797d6fcfae891eef85019e4f532a02 Mon Sep 17 00:00:00 2001 From: paulmasson Date: Sat, 1 Apr 2017 17:58:40 -0700 Subject: [PATCH] Move all script generation to backend --- src/sage/plot/plot3d/base.pyx | 16 +------ src/sage/repl/rich_output/backend_base.py | 33 +++++++++++++- src/sage/repl/rich_output/backend_ipython.py | 48 ++++++++++++++------ src/sage/repl/rich_output/backend_sagenb.py | 24 ++++++++++ 4 files changed, 92 insertions(+), 29 deletions(-) diff --git a/src/sage/plot/plot3d/base.pyx b/src/sage/plot/plot3d/base.pyx index 36ec4d36247..983500a1351 100644 --- a/src/sage/plot/plot3d/base.pyx +++ b/src/sage/plot/plot3d/base.pyx @@ -356,7 +356,7 @@ cdef class Graphics3d(SageObject): EXAMPLES:: - sage: sphere()._rich_repr_threejs() + sage: sphere(online=True)._rich_repr_threejs() OutputSceneThreejs container """ options = {} @@ -372,19 +372,7 @@ cdef class Graphics3d(SageObject): from sage.repl.rich_output import get_display_manager backend = get_display_manager()._backend - from sage.repl.rich_output.backend_sagenb import BackendSageNB - if isinstance(backend, BackendSageNB): - options['online'] = True - - if options['online']: - from sage.misc.package import installed_packages - version = installed_packages()['threejs'] - scripts = """ - - - """.format(version) - else: - scripts = backend.threejs_offline_scripts() + scripts = backend.threejs_scripts(options['online']) b = self.bounding_box() bounds = '[{{"x":{}, "y":{}, "z":{}}}, {{"x":{}, "y":{}, "z":{}}}]'.format( diff --git a/src/sage/repl/rich_output/backend_base.py b/src/sage/repl/rich_output/backend_base.py index e45fca731f0..381e299a672 100644 --- a/src/sage/repl/rich_output/backend_base.py +++ b/src/sage/repl/rich_output/backend_base.py @@ -1,9 +1,9 @@ # -*- encoding: utf-8 -*- r""" -Base class for Backends +Base Class for Backends The display backends are the commandline, the SageNB notebook, the -ipython notebook, the Emacs sage mode, the Sage doctester, .... All of +IPython notebook, the Emacs sage mode, the Sage doctester, .... All of these have different capabilities for what they can display. To implement a new display backend, you need to subclass @@ -594,6 +594,35 @@ def display_immediately(self, plain_text, rich_output): """ raise NotImplementedError('derived classes must implement this method') + def threejs_scripts(self, online): + """ + Three.js scripts for the backend base + + INPUT: + + - ``online`` -- Boolean determining script usage context + + OUTPUT: + + String containing script tags + + EXAMPLES:: + + sage: from sage.repl.rich_output.backend_base import BackendBase + sage: backend = BackendBase() + sage: backend.threejs_scripts(True) + '... + + """.format(version) + else: + raise ValueError('online should be true for the backend base') + class BackendSimple(BackendBase): """ diff --git a/src/sage/repl/rich_output/backend_ipython.py b/src/sage/repl/rich_output/backend_ipython.py index fa98be8bb0b..aab32c26eb2 100644 --- a/src/sage/repl/rich_output/backend_ipython.py +++ b/src/sage/repl/rich_output/backend_ipython.py @@ -396,22 +396,33 @@ def is_in_terminal(self): """ return True - def threejs_offline_scripts(self): + def threejs_scripts(self, online): """ - Three.js offline scripts for the IPython command line + Three.js scripts for the IPython command line + + INPUT: + + - ``online`` -- Boolean determining script usage context + + OUTPUT: + + String containing script tags EXAMPLES:: sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline sage: backend = BackendIPythonCommandline() - sage: backend.threejs_offline_scripts() + sage: backend.threejs_scripts(True) '... - """.format(SAGE_SHARE) + """.format(SAGE_SHARE) @@ -572,20 +583,31 @@ def displayhook(self, plain_text, rich_output): else: raise TypeError('rich_output type not supported') - def threejs_offline_scripts(self): + def threejs_scripts(self, online): """ - Three.js offline scripts for the IPython notebook + Three.js scripts for the IPython notebook + + INPUT: + + - ``online`` -- Boolean determining script usage context + + OUTPUT: + + String containing script tags EXAMPLES:: sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook sage: backend = BackendIPythonNotebook() - sage: backend.threejs_offline_scripts() + sage: backend.threejs_scripts(True) '... - """.format(version) + """.format(version) diff --git a/src/sage/repl/rich_output/backend_sagenb.py b/src/sage/repl/rich_output/backend_sagenb.py index 7b3abc584ad..d276f4f16d5 100644 --- a/src/sage/repl/rich_output/backend_sagenb.py +++ b/src/sage/repl/rich_output/backend_sagenb.py @@ -452,3 +452,27 @@ def embed_video(self, video_output): url='cell://' + filename, link_attrs='class="file_link"', )) + + def threejs_scripts(self, online): + """ + Three.js scripts for the Sage notebook + + INPUT: + + - ``online`` -- Boolean determining script usage context + + OUTPUT: + + String containing script tags + + EXAMPLES:: + + sage: from sage.repl.rich_output.backend_sagenb import BackendSageNB + sage: backend = BackendSageNB() + sage: backend.threejs_scripts(True) + '...