MAINT: make errors more informative for savechart()
@@ -10,10 +10,19 @@ import tempfile
import six
-from .importing import attempt_import
from .core import write_file_or_filename
from ._py3k_compat import urlopen, HTTPError, URLError
+try:
+ from selenium import webdriver
+except ImportError:
+ webdriver = None
+
+try:
+ from selenium.webdriver.chrome.options import Options as ChromeOptions
+except ImportError:
+ ChromeOptions = None
+
def connection_ok():
"""Check web connection.
@@ -125,10 +134,10 @@ def save_spec(spec, fp, mode=None, format=None, driver_timeout=10):
if format not in ['png', 'svg']:
raise NotImplementedError("save_spec only supports 'svg' and 'png'")
- webdriver = attempt_import('selenium.webdriver',
- 'save_spec requires the selenium package')
- Options = attempt_import('selenium.webdriver.chrome.options',
- 'save_spec requires the selenium package').Options
+ if webdriver is None:
+ raise ValueError("selenium package is required for saving chart as {0}".format(format))
+ if ChromeOptions is None:
+ raise ValueError("chromedriver is required for saving chart as {0}".format(format))
if mode is None:
if '$schema' in spec:
@@ -139,7 +148,7 @@ def save_spec(spec, fp, mode=None, format=None, driver_timeout=10):
raise ValueError("mode must be 'vega' or 'vega-lite'")
try:
- chrome_options = Options()
+ chrome_options = ChromeOptions()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.set_page_load_timeout(driver_timeout)
@@ -149,6 +158,9 @@ def save_spec(spec, fp, mode=None, format=None, driver_timeout=10):
with open(htmlfile, 'w') as f:
f.write(HTML_TEMPLATE)
driver.get("file://" + htmlfile)
+ online = driver.execute_script("return navigator.onLine")
+ if not online:
+ raise ValueError("Internet connection required for saving chart as {0}".format(format))
render = driver.execute_async_script(EXTRACT_CODE[format],
spec, mode)
finally:
