Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Move all script generation to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmasson committed Apr 2, 2017
1 parent 1d27bd0 commit 5c1c232
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 29 deletions.
16 changes: 2 additions & 14 deletions src/sage/plot/plot3d/base.pyx
Expand Up @@ -356,7 +356,7 @@ cdef class Graphics3d(SageObject):
EXAMPLES::
sage: sphere()._rich_repr_threejs()
sage: sphere(online=True)._rich_repr_threejs()
OutputSceneThreejs container
"""
options = {}
Expand All @@ -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 = """
<script src="https://cdn.rawgit.com/mrdoob/three.js/{0}/build/three.min.js"></script>
<script src="https://cdn.rawgit.com/mrdoob/three.js/{0}/examples/js/controls/OrbitControls.js"></script>
""".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(
Expand Down
33 changes: 31 additions & 2 deletions 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
Expand Down Expand Up @@ -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)
'...<script ...</script>...'
"""
if online:
from sage.misc.package import installed_packages
version = installed_packages()['threejs']
return """
<script src="https://cdn.rawgit.com/mrdoob/three.js/{0}/build/three.min.js"></script>
<script src="https://cdn.rawgit.com/mrdoob/three.js/{0}/examples/js/controls/OrbitControls.js"></script>
""".format(version)
else:
raise ValueError('online should be true for the backend base')


class BackendSimple(BackendBase):
"""
Expand Down
48 changes: 35 additions & 13 deletions src/sage/repl/rich_output/backend_ipython.py
Expand Up @@ -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)
'...<script ...</script>...'
"""
from sage.env import SAGE_SHARE
return """
if online:
return super(BackendIPythonCommandline, self).threejs_scripts(online)
else:
from sage.env import SAGE_SHARE
return """
<script src="{0}/threejs/three.min.js"></script>
<script src="{0}/threejs/OrbitControls.js"></script>
""".format(SAGE_SHARE)
""".format(SAGE_SHARE)



Expand Down Expand Up @@ -572,25 +583,36 @@ 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)
'...<script ...</script>...'
"""
from sage.misc.package import installed_packages
version = installed_packages()['threejs']
return """
if online:
return super(BackendIPythonNotebook, self).threejs_scripts(online)
else:
from sage.misc.package import installed_packages
version = installed_packages()['threejs']
return """
<script src="/nbextensions/threejs/three.min.js"></script>
<script src="/nbextensions/threejs/OrbitControls.js"></script>
<script>
if ( !window.THREE ) document.write('\
<script src="https://cdn.rawgit.com/mrdoob/three.js/{0}/build/three.min.js"><\/script>\
<script src="https://cdn.rawgit.com/mrdoob/three.js/{0}/examples/js/controls/OrbitControls.js"><\/script>');
</script>
""".format(version)
""".format(version)
24 changes: 24 additions & 0 deletions src/sage/repl/rich_output/backend_sagenb.py
Expand Up @@ -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)
'...<script ...</script>...'
"""
if online:
return super(BackendSageNB, self).threejs_scripts(online)
else:
return super(BackendSageNB, self).threejs_scripts(True)

0 comments on commit 5c1c232

Please sign in to comment.