From 335890eeb8e2700be5b61407627869f2a823c87b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Istv=C3=A1n=20Bozs=C3=B3?= Date: Tue, 18 Feb 2020 18:03:24 +0100 Subject: [PATCH] added jslibs.py --- utils/jslibs.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 utils/jslibs.py diff --git a/utils/jslibs.py b/utils/jslibs.py new file mode 100644 index 0000000..5a3dd92 --- /dev/null +++ b/utils/jslibs.py @@ -0,0 +1,60 @@ +from .html import Library +from .utils import namespace, export + +__all__ = [ + "libs", "plotly", +] + +class JSLib(Library): + __slots__ = ( + "_async", + ) + + def __init__(self, *args, **kwargs): + self._async = bool(kwargs.get("async", True)) + + Library.__init__(self, kwargs["path"]) + + def add(self, doc, *args, **kwargs): + args = set(args) + + if self._async: + args.add("async") + + kwargs["src"] = self.path + + with doc.tag("script", *args, **kwargs): + pass + + +libs = namespace( + shower=JSLib(path="https://shwr.me/shower/shower.min.js"), + mathjax=JSLib(path="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"), +) + +@export +class Plotly(JSLib): + base = "https://cdn.plot.ly/plotly-{version}.min.js" + partial = "https://cdn.plot.ly/plotly-{partial}-{version}.min.js" + + def __init__(self, *args, **kwargs): + version, partial = ( + kwargs.get("version", "latest"), + kwargs.get("partial") + ) + + if partial is not None: + path = self.partial.format(version=version, partial=partial) + else: + path = self.base.format(version=version) + + kwargs["path"] = path + + JSLib.__init__(self, *args, **kwargs) + + +plotly = namespace( + latest=Plotly(), + basic=Plotly(partial="base"), + cartesian=Plotly(partial="cartesian"), +)