Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a config to enable retina quality images in screenshots #17409

Merged
merged 3 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,11 @@ def SQL_QUERY_MUTATOR( # pylint: disable=invalid-name,unused-argument
WEBDRIVER_TYPE = "firefox"

# Window size - this will impact the rendering of the data
WEBDRIVER_WINDOW = {"dashboard": (1600, 2000), "slice": (3000, 1200)}
WEBDRIVER_WINDOW = {
"dashboard": (1600, 2000),
"slice": (3000, 1200),
"pixel_density": 1,
}

# An optional override to the default auth hook used to provide auth to the
# offline webdriver
Expand Down
15 changes: 9 additions & 6 deletions superset/utils/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
TimeoutException,
WebDriverException,
)
from selenium.webdriver import chrome, firefox
from selenium.webdriver import chrome, firefox, FirefoxProfile
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions as EC
Expand All @@ -51,22 +51,26 @@ class DashboardStandaloneMode(Enum):


class WebDriverProxy:
def __init__(
self, driver_type: str, window: Optional[WindowSize] = None,
):
def __init__(self, driver_type: str, window: Optional[WindowSize] = None):
self._driver_type = driver_type
self._window: WindowSize = window or (800, 600)
self._screenshot_locate_wait = current_app.config["SCREENSHOT_LOCATE_WAIT"]
self._screenshot_load_wait = current_app.config["SCREENSHOT_LOAD_WAIT"]

def create(self) -> WebDriver:
pixel_density = current_app.config["WEBDRIVER_WINDOW"].get("pixel_density", 1)
if self._driver_type == "firefox":
driver_class = firefox.webdriver.WebDriver
options = firefox.options.Options()
profile = FirefoxProfile()
profile.set_preference("layout.css.devPixelsPerPx", str(pixel_density))
kwargs: Dict[Any, Any] = dict(options=options, firefox_profile=profile)
elif self._driver_type == "chrome":
driver_class = chrome.webdriver.WebDriver
options = chrome.options.Options()
options.add_argument(f"--force-device-scale-factor={pixel_density}")
options.add_argument(f"--window-size={self._window[0]},{self._window[1]}")
kwargs = dict(options=options)
else:
raise Exception(f"Webdriver name ({self._driver_type}) not supported")
# Prepare args for the webdriver init
Expand All @@ -75,7 +79,6 @@ def create(self) -> WebDriver:
for arg in current_app.config["WEBDRIVER_OPTION_ARGS"]:
options.add_argument(arg)

kwargs: Dict[Any, Any] = dict(options=options)
kwargs.update(current_app.config["WEBDRIVER_CONFIGURATION"])
logger.info("Init selenium driver")

Expand All @@ -102,7 +105,7 @@ def destroy(driver: WebDriver, tries: int = 2) -> None:
pass

def get_screenshot(
self, url: str, element_name: str, user: "User",
self, url: str, element_name: str, user: "User"
) -> Optional[bytes]:
params = {"standalone": DashboardStandaloneMode.REPORT.value}
req = PreparedRequest()
Expand Down