Skip to content

Commit ec47613

Browse files
committed
Datalore/Colab: add support for env var 'LETS_PLOT_HTML_ISOLATED_FRAME' (bool)
1 parent 37a68aa commit ec47613

File tree

6 files changed

+75
-38
lines changed

6 files changed

+75
-38
lines changed

python-package/lets_plot/_global_settings.py

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,36 @@
22
# Copyright (c) 2019. JetBrains s.r.o.
33
# Use of this source code is governed by the MIT license that can be found in the LICENSE file.
44
#
5+
import os
56
from typing import Dict, Any
67

78
from ._version import __version__
89

10+
# Public environment variables
11+
ENV_HTML_ISOLATED_FRAME = 'LETS_PLOT_HTML_ISOLATED_FRAME' # bool
12+
ENV_DEV_HTML_ISOLATED_FRAME = 'LETS_PLOT_DEV_HTML_ISOLATED_FRAME' # bool
13+
ENV_OFFLINE = 'LETS_PLOT_OFFLINE' # bool
14+
ENV_DEV_OFFLINE = 'LETS_PLOT_DEV_OFFLINE' # bool
15+
16+
HTML_ISOLATED_FRAME = 'html_isolated_frame'
17+
18+
19+
def _init_value(actual_name: str, def_val: Any) -> Any:
20+
env_val = _get_env_val(actual_name)
21+
return env_val if env_val else def_val
22+
23+
24+
def _get_env_val(actual_name: str) -> Any:
25+
env_name = "LETS_PLOT_{}".format(actual_name.upper())
26+
return os.environ.get(env_name)
27+
28+
929
_settings = {
10-
'offline': False, # default: download from CDN
30+
'offline': _init_value('offline', False), # default: download from CDN
1131
'js_base_url': 'https://dl.bintray.com/jetbrains/lets-plot',
1232
'js_name': '', # default: lets-plot-<version>.min.js
1333

14-
'dev_offline': True, # default: embed js into the notebook
34+
'dev_offline': _init_value('dev_offline', True), # default: embed js into the notebook
1535
'dev_js_base_url': "http://0.0.0.0:8080",
1636
'dev_js_name': '' # default: lets-plot-<version>.js
1737
}
@@ -23,44 +43,58 @@ def apply(cls, settings: Dict):
2343
_settings.update(settings)
2444

2545

26-
def _is_production() -> bool:
46+
def _to_actual_name(name: str) -> str:
47+
if name.startswith("dev_"):
48+
return name
49+
50+
return name if is_production() else 'dev_' + name
51+
52+
53+
def _get_global_val_intern(actual_name: str) -> Any:
54+
# `settings` dict has precedence over environment variables.
55+
env_val = _get_env_val(actual_name)
56+
return _settings.get(actual_name, env_val)
57+
58+
59+
def is_production() -> bool:
2760
return 'dev' not in __version__
2861

2962

30-
def _has_global_value(name: str) -> bool:
31-
actual_name = name if _is_production() else 'dev_' + name
32-
val = _settings[actual_name]
63+
def has_global_value(name: str) -> bool:
64+
val = _get_global_val_intern(_to_actual_name(name))
65+
3366
if isinstance(val, bool):
3467
return True
3568
if isinstance(val, str) and not val.strip():
3669
return False
3770
return val
3871

3972

40-
def _to_actual_name(name: str) -> str:
41-
if name.startswith("dev_"):
42-
return name
43-
44-
return name if _is_production() else 'dev_' + name
73+
def get_global_val(name: str) -> Any:
74+
if not has_global_value(name):
75+
raise ValueError("Not defined '{}'".format(_to_actual_name(name)))
4576

77+
return _get_global_val_intern(_to_actual_name(name))
4678

47-
def _get_global_val(name: str) -> Any:
48-
actual_name = _to_actual_name(name)
49-
if not _has_global_value(name):
50-
raise ValueError("Not defined '{}'".format(actual_name))
5179

52-
return _settings[actual_name]
53-
54-
55-
def _get_global_str(name: str) -> str:
56-
val = _get_global_val(name)
80+
def get_global_str(name: str) -> str:
81+
val = get_global_val(name)
5782
if not isinstance(val, str):
5883
raise ValueError("Not string value: ['{}'] : {}".format(_to_actual_name(name), type(val)))
5984
return val
6085

6186

62-
def _get_global_bool(name: str) -> bool:
63-
val = _get_global_val(name)
64-
if not isinstance(val, bool):
65-
raise ValueError("Not boolean value: ['{}'] : {}".format(_to_actual_name(name), type(val)))
66-
return val
87+
def get_global_bool(name: str) -> bool:
88+
val = get_global_val(name)
89+
if isinstance(val, bool):
90+
return val
91+
92+
if isinstance(val, str):
93+
if val.lower() in ['true', '1', 't', 'y', 'yes']:
94+
return True
95+
elif val.lower() in ['false', '0', 'f', 'n', 'no']:
96+
return False
97+
else:
98+
raise ValueError("Can't convert str to boolean : ['{}'] : {}".format(_to_actual_name(name), val))
99+
100+
raise ValueError("Not boolean value: ['{}'] : {}".format(_to_actual_name(name), type(val)))

python-package/lets_plot/export/simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Union
77

88
from .. import _kbridge as kbr
9-
from .._global_settings import _is_production
9+
from .._global_settings import is_production
1010
from .._version import __version__
1111
from ..plot.core import PlotSpec
1212
from ..plot.plot import GGBunch
@@ -57,7 +57,7 @@ def export_html(plot: Union[PlotSpec, GGBunch], filename: str, iframe: bool = Fa
5757
if not (isinstance(plot, PlotSpec) or isinstance(plot, GGBunch)):
5858
raise ValueError("PlotSpec or GGBunch expected but was: {}".format(type(plot)))
5959

60-
version = __version__ if _is_production() else "latest"
60+
version = __version__ if is_production() else "latest"
6161
html_page = kbr._generate_static_html_page(plot.as_dict(), version, iframe)
6262
with io.open(filename, mode="w", encoding="utf-8") as f:
6363
f.write(html_page)

python-package/lets_plot/frontend_context/_configuration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from ._frontend_ctx import FrontendContext
88
from ._html_contexts import _create_html_frontend_context, _use_isolated_frame
9-
from .._global_settings import _get_global_bool
9+
from .._global_settings import get_global_bool
1010
from ..plot.core import PlotSpec
1111
from ..plot.plot import GGBunch
1212

@@ -25,7 +25,7 @@ def _setup_html_context(isolated_frame: bool = None, offline: bool = None) -> No
2525
:param embed:
2626
:return:
2727
"""
28-
embed = offline if offline is not None else _get_global_bool('offline')
28+
embed = offline if offline is not None else get_global_bool('offline')
2929
ctx = _create_html_frontend_context(isolated_frame, embed)
3030
ctx.configure(verbose=True)
3131
_frontend_contexts['html'] = ctx

python-package/lets_plot/frontend_context/_html_contexts.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import os
55

66
from ._frontend_ctx import FrontendContext
7-
from ._static_html_page_ctx import StaticHtmlPageContext
87
from ._jupyter_notebook_ctx import JupyterNotebookContext
8+
from ._static_html_page_ctx import StaticHtmlPageContext
9+
from .._global_settings import has_global_value, get_global_bool, HTML_ISOLATED_FRAME
910

1011

1112
def _create_html_frontend_context(isolated_frame: bool = None, offline: bool = None) -> FrontendContext:
@@ -25,7 +26,9 @@ def _create_html_frontend_context(isolated_frame: bool = None, offline: bool = N
2526

2627

2728
def _use_isolated_frame() -> bool:
28-
# ToDo: check env var first
29+
# check environment
30+
if has_global_value(HTML_ISOLATED_FRAME):
31+
return get_global_bool(HTML_ISOLATED_FRAME)
2932

3033
return _detect_isolated_frame()
3134

python-package/lets_plot/frontend_context/_jupyter_notebook_ctx.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from ._frontend_ctx import FrontendContext
1515
from .. import _kbridge as kbr
16-
from .._global_settings import _get_global_str, _has_global_value, _is_production
16+
from .._global_settings import get_global_str, has_global_value, is_production
1717
from .._version import __version__
1818

1919

@@ -36,11 +36,11 @@ def as_str(self, plot_spec: Dict) -> str:
3636

3737
@staticmethod
3838
def _configure_connected_script(verbose: bool) -> str:
39-
base_url = _get_global_str("js_base_url")
40-
if _has_global_value('js_name'):
41-
name = _get_global_str('js_name')
39+
base_url = get_global_str("js_base_url")
40+
if has_global_value('js_name'):
41+
name = get_global_str('js_name')
4242
else:
43-
suffix = ".min.js" if _is_production() else ".js"
43+
suffix = ".min.js" if is_production() else ".js"
4444
name = "lets-plot-{version}{suffix}".format(version=__version__, suffix=suffix)
4545

4646
url = "{base_url}/{name}".format(base_url=base_url, name=name)

python-package/lets_plot/frontend_context/_static_html_page_ctx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from ._frontend_ctx import FrontendContext
88
from .. import _kbridge as kbr
9-
from .._global_settings import _is_production
9+
from .._global_settings import is_production
1010
from .._version import __version__
1111

1212

@@ -27,5 +27,5 @@ def configure(self, verbose: bool):
2727
def as_str(self, plot_spec: Dict) -> str:
2828
# embedding js is not supported (yet) in this context,
2929
# replace `dev` version with the `latest`.
30-
version = __version__ if _is_production() else "latest"
30+
version = __version__ if is_production() else "latest"
3131
return kbr._generate_static_html_page(plot_spec, version, iframe=False)

0 commit comments

Comments
 (0)