From 26571724e055d172e62159205d0adfbe22a22e46 Mon Sep 17 00:00:00 2001 From: Jeremy Tuloup Date: Thu, 29 Sep 2022 10:27:45 +0000 Subject: [PATCH] Add hatch custom plugin to download the CSS --- hatch_build.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 11 +++++++++++ 2 files changed, 62 insertions(+) create mode 100644 hatch_build.py diff --git a/hatch_build.py b/hatch_build.py new file mode 100644 index 000000000..cf7d405a2 --- /dev/null +++ b/hatch_build.py @@ -0,0 +1,51 @@ +import os +import sys + +from hatchling.builders.hooks.plugin.interface import BuildHookInterface +from urllib.request import urlopen + + +JUPYTERLAB_APPUTILS_VERSION = "3.2.8" +JUPYTERLAB_THEME_LIGHT_VERSION = "3.2.8" + +CSS_FILES = [ + ( + f"https://unpkg.com/@jupyterlab/apputils@{JUPYTERLAB_APPUTILS_VERSION}/style/materialcolors.css", + "materialcolors.css", + ), + ( + f"https://unpkg.com/@jupyterlab/theme-light-extension@{JUPYTERLAB_THEME_LIGHT_VERSION}/style/variables.css", + "labvariables.css", + ), +] + + +class CustomBuildHook(BuildHookInterface): + def initialize(self, version, build_data): + for template_name in ["classic", "reveal"]: + for url, filename in CSS_FILES: + directory = os.path.join( + "share", "jupyter", "voila", "templates", template_name, "static" + ) + dest = os.path.join(directory, filename) + if not os.path.exists(directory): + os.makedirs(directory) + if not os.path.exists(".git") and os.path.exists(dest): + # not running from git, nothing to do + return + print("Downloading CSS: %s" % url) + try: + css = urlopen(url).read() + except Exception as e: + msg = "Failed to download css from %s: %s" % (url, e) + print(msg, file=sys.stderr) + + if os.path.exists(dest): + print("Already have CSS: %s, moving on." % dest) + else: + raise OSError("Need CSS to proceed.") + return + + with open(dest, "wb") as f: + f.write(css) + print("Downloaded Notebook CSS to %s" % dest) diff --git a/pyproject.toml b/pyproject.toml index e83baf119..9fa0c18fd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -83,6 +83,9 @@ artifacts = [ "share/jupyter/voila/templates" ] +[tool.hatch.build.hooks.custom] +path = "hatch_build.py" + [tool.hatch.build.targets.wheel.shared-data] "etc/jupyter" = "etc/jupyter" "voila/static" = "share/jupyter/nbextensions/voila" @@ -102,9 +105,17 @@ dependencies = [ build-function = "hatch_jupyter_builder.npm_builder" ensured-targets = [ "voila/labextension/static/style.js", + "share/jupyter/voila/templates/classic/static/materialcolors.css", + "share/jupyter/voila/templates/classic/static/labvariables.css", + "share/jupyter/voila/templates/reveal/static/materialcolors.css", + "share/jupyter/voila/templates/reveal/static/labvariables.css", ] skip-if-exists = [ "voila/labextension/static/style.js", + "share/jupyter/voila/templates/classic/static/materialcolors.css", + "share/jupyter/voila/templates/classic/static/labvariables.css", + "share/jupyter/voila/templates/reveal/static/materialcolors.css", + "share/jupyter/voila/templates/reveal/static/labvariables.css", ] [tool.hatch.build.hooks.jupyter-builder.editable-build-kwargs]