Skip to content

Commit

Permalink
build: Only install JS with Python with env var set (#285)
Browse files Browse the repository at this point in the history
- Only register the JS if you have the environment variable
`DEEPHAVEN_ENABLE_PY_JS=True` set
- Cleaned up some of the JS in python packaging
- Now puts JS at `deephaven.plot.express.js`, so it's contained in the
deephaven express package instead of polluting the `js` namespace at the
top level

- Fixes #281
  • Loading branch information
mofojed authored Feb 16, 2024
1 parent dbff1ab commit 22662df
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
36 changes: 17 additions & 19 deletions plugins/plotly-express/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,36 @@
import os
import subprocess

# npm pack in js directory
# Uses npm pack to create a tarball of the package and unpacks it into a build directory
# Then uses that to add to the wheel

js_dir = "src/js/"
dist_dir = os.path.join(js_dir, "dist")
project = "plotly-express"
dest_dir = os.path.join(js_dir, project, "")
build_dir = os.path.join(js_dir, "build")
dest_dir = os.path.join("src/deephaven/plot/express/_js")
package_dir = os.path.join(build_dir, "package")

# copy the bundle to the plotly-express directory
# the path may not exist (e.g. when running tests)
# so it is not strictly necessary to copy the bundle
if os.path.exists(dist_dir):
os.makedirs(dest_dir, exist_ok=True)
# ignore errors as the directory may not exist
shutil.rmtree(build_dir, ignore_errors=True)
shutil.rmtree(dest_dir, ignore_errors=True)

os.makedirs(build_dir, exist_ok=True)

# pack and unpack into the js plotly-express directory
subprocess.run(
["npm", "pack", "--pack-destination", project], cwd=js_dir, check=True
["npm", "pack", "--pack-destination", "build"], cwd=js_dir, check=True
)
# it is assumed that there is only one tarball in the directory
files = os.listdir(dest_dir)
for file in files:
subprocess.run(["tar", "-xzf", file], cwd=dest_dir, check=True)
os.remove(os.path.join(dest_dir, file))

# move the contents of the package directory to the plotly-express directory
package_dir = os.path.join(dest_dir, "package")
files = os.listdir(package_dir)
files = os.listdir(build_dir)
for file in files:
source_path = os.path.join(package_dir, file)
dest_path = os.path.join(dest_dir, file)
shutil.move(source_path, dest_path)
subprocess.run(["tar", "-xzf", file], cwd=build_dir, check=True)
os.remove(os.path.join(build_dir, file))

setup(package_data={"js.plotly-express": ["**"]})
# move the package directory to the expected package location
shutil.move(package_dir, dest_dir)

# ignore errors as the directory may not exist
shutil.rmtree("src/js/plotly-express", ignore_errors=True)
setup(package_data={"deephaven.plot.express._js": ["**"]})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_js/
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def _create_from_npm_package_json(


def _resource_js_path() -> ContextManager[pathlib.Path]:
# TODO: Js content should be in same package directory
# https://github.com/deephaven/deephaven-plugins/issues/139
namespace = "deephaven.plot.express"
name = "_js"
if sys.version_info < (3, 9):
return importlib.resources.path("js", "plotly-express")
return importlib.resources.path(namespace, name)
else:
return importlib.resources.as_file(
importlib.resources.files("js").joinpath("plotly-express")
importlib.resources.files(namespace).joinpath(name)
)


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from . import DeephavenFigureType
from ._js import create_js_plugin
from ._js_plugin import create_js_plugin

from deephaven.plugin import Registration, Callback

Expand All @@ -21,4 +22,7 @@ def register_into(cls, callback: Callback) -> None:
"""
callback.register(DeephavenFigureType)
callback.register(create_js_plugin())

# Only register the JS plugins if the environment variable is set
if os.getenv("DEEPHAVEN_ENABLE_PY_JS", "False").lower() in ("true", "1"):
callback.register(create_js_plugin())

0 comments on commit 22662df

Please sign in to comment.