Skip to content

Commit

Permalink
threejs: access files through features, for simpler configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
tornaria committed Jan 9, 2024
1 parent 469a087 commit 0b907cf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/sage/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def var(key: str, *fallbacks: Optional[str], force: bool = False) -> Optional[st
JMOL_DIR = var("JMOL_DIR")
MATHJAX_DIR = var("MATHJAX_DIR", join(SAGE_SHARE, "mathjax"))
MTXLIB = var("MTXLIB", join(SAGE_SHARE, "meataxe"))
THREEJS_DIR = var("THREEJS_DIR", join(SAGE_SHARE, "threejs-sage"))
THREEJS_DIR = var("THREEJS_DIR")
PPLPY_DOCS = var("PPLPY_DOCS", join(SAGE_SHARE, "doc", "pplpy"))
MAXIMA = var("MAXIMA", "maxima")
MAXIMA_FAS = var("MAXIMA_FAS")
Expand Down
47 changes: 47 additions & 0 deletions src/sage/features/threejs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os

from . import StaticFile


class Threejs(StaticFile):
r"""
A :class:`~sage.features.Feature` which describes the presence of
threejs-sage in a few standard locations.
EXAMPLES::
sage: from sage.features.threejs import Threejs
sage: bool(Threejs().is_present()) # needs threejs
True
"""

def __init__(self):
r"""
TESTS::
sage: from sage.features.threejs import Threejs
sage: isinstance(Threejs(), Threejs)
True
"""
from sage.env import SAGE_SHARE, THREEJS_DIR
from sage.repl.rich_output import display_manager

version = display_manager._required_threejs_version()

threejs_search_path = THREEJS_DIR or (
os.path.join(SAGE_SHARE, "jupyter", "nbextension", "threejs-sage"),
os.path.join(SAGE_SHARE, "sagemath", "threejs-sage"),
os.path.join(SAGE_SHARE, "threejs-sage")
)

StaticFile.__init__(
self, name="threejs",
filename=os.path.join(version, "three.min.js"),
spkg="threejs",
type="standard",
search_path=threejs_search_path,
description="JavaScript library to display 3D graphics")


def all_features():
return [Threejs()]
7 changes: 5 additions & 2 deletions src/sage/repl/ipython_kernel/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
SAGE_EXTCODE,
SAGE_VENV,
SAGE_VERSION,
THREEJS_DIR,
)


Expand Down Expand Up @@ -123,14 +122,18 @@ def use_local_threejs(self):
EXAMPLES::
sage: # needs threejs
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: spec = SageKernelSpec(prefix=tmp_dir())
sage: spec.use_local_threejs()
sage: threejs = os.path.join(spec.nbextensions_dir, 'threejs-sage')
sage: os.path.isdir(threejs)
True
"""
src = THREEJS_DIR
from sage.features.threejs import Threejs
if not Threejs().is_present():
return

Check warning on line 135 in src/sage/repl/ipython_kernel/install.py

View check run for this annotation

Codecov / codecov/patch

src/sage/repl/ipython_kernel/install.py#L135

Added line #L135 was not covered by tests
src = os.path.dirname(os.path.dirname(Threejs().absolute_filename()))
dst = os.path.join(self.nbextensions_dir, 'threejs-sage')
self.symlink(src, dst)

Expand Down
9 changes: 6 additions & 3 deletions src/sage/repl/rich_output/backend_ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,18 @@ def threejs_offline_scripts(self):
EXAMPLES::
sage: # needs threejs
sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
sage: backend = BackendIPythonCommandline()
sage: backend.threejs_offline_scripts() # needs sage.plot
sage: backend.threejs_offline_scripts()
'...<script ...</script>...'
"""
from sage.env import THREEJS_DIR
from sage.features.threejs import Threejs
from sage.repl.rich_output.display_manager import _required_threejs_version
if not Threejs().is_present():
return ''

Check warning on line 421 in src/sage/repl/rich_output/backend_ipython.py

View check run for this annotation

Codecov / codecov/patch

src/sage/repl/rich_output/backend_ipython.py#L421

Added line #L421 was not covered by tests

script = os.path.join(THREEJS_DIR, '{}/three.min.js'.format(_required_threejs_version()))
script = Threejs().absolute_filename()

return '\n<script src="{0}"></script>'.format(script)

Expand Down

0 comments on commit 0b907cf

Please sign in to comment.