|
1 | 1 | from pathlib import Path |
| 2 | +from urllib.parse import urlparse |
2 | 3 |
|
3 | 4 | import logistro |
4 | 5 |
|
|
10 | 11 | KJS_PATH = Path(__file__).resolve().parent / "vendor" / "kaleido_scopes.js" |
11 | 12 |
|
12 | 13 |
|
| 14 | +def _ensure_path(path: Path): |
| 15 | + if urlparse(path).scheme: # is url |
| 16 | + return |
| 17 | + if not path.exists(): |
| 18 | + raise ValueError(f"{path} does not seem to be a valid path.") |
| 19 | + |
| 20 | + |
13 | 21 | class PageGenerator: |
14 | 22 | """ |
15 | 23 | A page generator can set the versions of the js libraries used to render. |
@@ -45,13 +53,14 @@ def __init__(self, *, plotly=None, mathjax=None, others=None, force_cdn=False): |
45 | 53 | Create a PageGenerator. |
46 | 54 |
|
47 | 55 | Args: |
48 | | - plotly: the url to the plotly script to use. The default is the one |
49 | | - plotly.py is using, if not installed, it uses the constant declared. |
50 | | - mathjax: the url to the mathjax script. By default is constant above. |
51 | | - Can be set to false to turn off. |
52 | | - others: a list of other script urls to include. Usually strings, but can be |
53 | | - (str, str) where its (url, encoding). |
54 | | - force_cdn: (default False) Don't use plotly import, use CDN |
| 56 | + plotly: The url to the plotly.js to use. Defaults to plotly.js |
| 57 | + present in plotly.py, if installed. Otherwise fallback to |
| 58 | + global constant. |
| 59 | + mathjax: The url to the mathjax script. Defaults to global constant. |
| 60 | + Can be set to false to turn off. |
| 61 | + others: A list of other script urls to include. Usually strings, but |
| 62 | + can be (str, str) where it's (url, encoding). |
| 63 | + force_cdn: Set True to force CDN use, defaults to False. |
55 | 64 |
|
56 | 65 | """ |
57 | 66 | self._scripts = [] |
@@ -81,14 +90,19 @@ def __init__(self, *, plotly=None, mathjax=None, others=None, force_cdn=False): |
81 | 90 | _logger.info("Plotly not installed. Using CDN.") |
82 | 91 | plotly = (DEFAULT_PLOTLY, "utf-8") |
83 | 92 | elif isinstance(plotly, str): |
| 93 | + _ensure_path(plotly) |
84 | 94 | plotly = (plotly, "utf-8") |
85 | 95 | _logger.debug(f"Plotly script: {plotly}") |
86 | 96 | self._scripts.append(plotly) |
87 | 97 | if mathjax is not False: |
88 | 98 | if not mathjax: |
89 | 99 | mathjax = DEFAULT_MATHJAX |
| 100 | + else: |
| 101 | + _ensure_path(mathjax) |
90 | 102 | self._scripts.append(mathjax) |
91 | 103 | if others: |
| 104 | + for o in others: |
| 105 | + _ensure_path(o) |
92 | 106 | self._scripts.extend(others) |
93 | 107 |
|
94 | 108 | def generate_index(self, path=None): |
|
0 commit comments