diff --git a/repos/kaleido/py/kaleido/scopes/base.py b/repos/kaleido/py/kaleido/scopes/base.py index 783f515d..043e72d7 100644 --- a/repos/kaleido/py/kaleido/scopes/base.py +++ b/repos/kaleido/py/kaleido/scopes/base.py @@ -16,17 +16,47 @@ class BaseScope(object): # Subclasses may override to specify a custom JSON encoder for input data _json_encoder = None - # Tuple of class properties that will be passed as - # command-line flags to configure chromium - _chromium_flags = ("disable_gpu",) - # Tuple of class properties that will be passed as command-line # flags to configure scope _scope_flags = () - def __init__(self, disable_gpu=True): - # Collect chromium flag properties - self._disable_gpu = disable_gpu + _default_chromium_args = ( + "--disable-gpu", + "--allow-file-access-from-files", + "--disable-breakpad", + "--disable-dev-shm-usage", + ) + + _scope_chromium_args = () + + @classmethod + def default_chromium_args(cls): + """ + Get tuple containing the default chromium arguments that will be passed to chromium if not overridden. + + chromium arguments can be overridden in the Scope constructor using the chromium_args argument, or they + can be overridden by assigning a tuple to the chromium_args property of an already constructed Scope instance + + :return: tuple of str + """ + return cls._default_chromium_args + cls._scope_chromium_args + + def __init__( + self, + disable_gpu=True, + chromium_args=True, + ): + if chromium_args is True: + chromium_args = self.default_chromium_args() + elif chromium_args is False: + chromium_args = () + + # Handle backward compatibility for disable_gpu flag + if disable_gpu is False: + # If disable_gpu is set to False, then remove corresponding flag from extra_chromium_args + chromium_args = [arg for arg in chromium_args if arg != "--disable-gpu"] + + self._chromium_args = tuple(chromium_args) # Internal Properties self._std_error = io.BytesIO() @@ -76,12 +106,12 @@ def executable_path(cls): def _build_proc_args(self): """ Build list of kaleido command-line arguments based on current values of - the properties specified by self._chromium_flags and self._scope_flags + the properties specified by self._scope_flags and self.chromium_args :return: list of flags """ proc_args = [self.executable_path(), self.scope_name] - for k in self._chromium_flags + self._scope_flags: + for k in self._scope_flags: v = getattr(self, k) if v is True: flag = '--' + k.replace("_", "-") @@ -93,6 +123,9 @@ def _build_proc_args(self): flag = '--' + k.replace("_", "-") + "=" + repr(str(v)) proc_args.append(flag) + # Append self.chromium_args + proc_args.extend(self.chromium_args) + return proc_args def _collect_standard_error(self): @@ -107,7 +140,7 @@ def _collect_standard_error(self): self._std_error.write(val) else: # Due to concurrency the process may be killed while this loop is still running - # in this case break the loop + # in this case break the loop return def _ensure_kaleido(self): @@ -216,11 +249,22 @@ def scope_name(self): @property def disable_gpu(self): """ If True, asks chromium to disable GPU hardware acceleration with --disable-gpu flag""" - return self._disable_gpu + return "--disable-gpu" in self.chromium_args @disable_gpu.setter def disable_gpu(self, val): - self._disable_gpu = val + new_args = [arg for arg in self.chromium_args if arg != "--disable-gpu"] + if val: + new_args.append("--disable-gpu") + self.chromium_args = tuple(new_args) + + @property + def chromium_args(self): + return self._chromium_args + + @chromium_args.setter + def chromium_args(self, val): + self._chromium_args = tuple(val) self._shutdown_kaleido() def _perform_transform(self, data, **kwargs): diff --git a/repos/kaleido/py/kaleido/scopes/plotly.py b/repos/kaleido/py/kaleido/scopes/plotly.py index 79d36e94..a25474b6 100644 --- a/repos/kaleido/py/kaleido/scopes/plotly.py +++ b/repos/kaleido/py/kaleido/scopes/plotly.py @@ -13,6 +13,7 @@ class PlotlyScope(BaseScope): _text_formats = ("svg", "json", "eps") _scope_flags = ("plotlyjs", "mathjax", "topojson", "mapbox_access_token") + _scope_chromium_args = ("--no-sandbox",) def __init__(self, plotlyjs=None, mathjax=None, topojson=None, mapbox_access_token=None, **kwargs): # TODO: validate args diff --git a/repos/kaleido/tests/test_py/test_plotly/test_plotly.py b/repos/kaleido/tests/test_py/test_plotly/test_plotly.py index 42099d5c..faf7fda2 100644 --- a/repos/kaleido/tests/test_py/test_plotly/test_plotly.py +++ b/repos/kaleido/tests/test_py/test_plotly/test_plotly.py @@ -161,3 +161,28 @@ def test_figure_size(): transform_mock.assert_called_once_with( fig.to_dict(), format="svg", scale=2, width=987, height=876 ) + + +def test_gpu_arg(): + # --disable-gpu is a default + assert "--disable-gpu" in PlotlyScope.default_chromium_args() + + # Check that --disable-gpu is in scope instance chromium_args + scope = PlotlyScope() + assert "--disable-gpu" in scope.chromium_args + assert "--disable-gpu" in scope._build_proc_args() + + # Check that --disable-gpu is in scope instance chromium_args + scope = PlotlyScope(disable_gpu=False) + assert "--disable-gpu" not in scope.chromium_args + assert "--disable-gpu" not in scope._build_proc_args() + scope.disable_gpu = True + assert "--disable-gpu" in scope.chromium_args + assert "--disable-gpu" in scope._build_proc_args() + + +def test_custopm_chromium_arg(): + # Check that --disable-gpu is in scope instance chromium_args + chromium_args = PlotlyScope.default_chromium_args() + ("--single-process",) + scope = PlotlyScope(chromium_args=chromium_args) + assert "--single-process" in scope._build_proc_args() diff --git a/repos/linux_scripts/launch_script b/repos/linux_scripts/launch_script index 8f258c11..5c440461 100755 --- a/repos/linux_scripts/launch_script +++ b/repos/linux_scripts/launch_script @@ -7,4 +7,4 @@ export XDG_DATA_HOME=$DIR/xdg unset LD_PRELOAD cd $DIR -./bin/kaleido --no-sandbox --allow-file-access-from-files --disable-breakpad $@ +./bin/kaleido $@ diff --git a/repos/mac_scripts/build_kaleido b/repos/mac_scripts/build_kaleido index ba730db8..041f0760 100755 --- a/repos/mac_scripts/build_kaleido +++ b/repos/mac_scripts/build_kaleido @@ -84,7 +84,7 @@ echo "#!/bin/bash DIR=\"\$( cd \"\$( dirname \"\${BASH_SOURCE[0]}\" )\" >/dev/null 2>&1 && pwd )\" cd \$DIR -./bin/kaleido --no-sandbox --allow-file-access-from-files --disable-breakpad \$@ +./bin/kaleido \$@ " > ../build/kaleido/kaleido chmod +x ../build/kaleido/kaleido diff --git a/repos/win_scripts/kaleido.cmd b/repos/win_scripts/kaleido.cmd index 9c7f1e64..79a25b0b 100644 --- a/repos/win_scripts/kaleido.cmd +++ b/repos/win_scripts/kaleido.cmd @@ -1,4 +1,4 @@ @echo off setlocal chdir /d "%~dp0" -.\bin\kaleido.exe --no-sandbox --allow-file-access-from-files --disable-breakpad %* \ No newline at end of file +.\bin\kaleido.exe %*