Skip to content

Commit

Permalink
Add support for custom chromium commandline arguments (#53)
Browse files Browse the repository at this point in the history
* Support custom chromium arguments
* Fix old usage of _extra_chromium_args
* Add chromium argument tests
* Add --disable-dev-shm-usage chromium flag to defaults
  • Loading branch information
jonmmease authored Oct 23, 2020
1 parent 812acf4 commit 32d2b03
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 15 deletions.
68 changes: 56 additions & 12 deletions repos/kaleido/py/kaleido/scopes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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("_", "-")
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions repos/kaleido/py/kaleido/scopes/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions repos/kaleido/tests/test_py/test_plotly/test_plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion repos/linux_scripts/launch_script
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@
2 changes: 1 addition & 1 deletion repos/mac_scripts/build_kaleido
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion repos/win_scripts/kaleido.cmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@echo off
setlocal
chdir /d "%~dp0"
.\bin\kaleido.exe --no-sandbox --allow-file-access-from-files --disable-breakpad %*
.\bin\kaleido.exe %*

0 comments on commit 32d2b03

Please sign in to comment.