Skip to content

Commit

Permalink
Merge pull request #49 from wildlife-dynamics/return-selenium
Browse files Browse the repository at this point in the history
revert to selenium
  • Loading branch information
ericgitonga authored Oct 5, 2023
2 parents d8e1658 + e22fd7b commit ebd1dd5
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 35 deletions.
46 changes: 38 additions & 8 deletions ecoscope/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
__initialized = False


def init(silent=False, pyppeteer=False, force=False):
def init(silent=False, selenium=False, force=False):
"""
Initializes the environment with ecoscope-specific customizations.
Parameters
----------
silent : bool, optional
Removes console output
pyppeteer : bool, optional
Installs Pyppeteer and Chrome in a colab environment
selenium : bool, optional
Installs selenium webdriver in a colab environment
force : bool, optional
Ignores `__initialized`
Expand Down Expand Up @@ -76,16 +76,46 @@ def explore(data, *args, **kwargs):

import sys

if "google.colab" in sys.modules and pyppeteer:
if "google.colab" in sys.modules and selenium:
from IPython import get_ipython

import nest_asyncio
shell_text = """\
cat > /etc/apt/sources.list.d/debian.list <<'EOF'
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-bookworm.gpg] http://deb.debian.org/debian bookworm main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-bookworm-updates.gpg]\
http://deb.debian.org/debian bookworm-updates main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-bookworm.gpg]\
http://deb.debian.org/debian-security bookworm/updates main
EOF
nest_asyncio.apply()
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 112695A0E562B32A
apt-key export 77E11517 | gpg --dearmour -o /usr/share/keyrings/debian-bookworm.gpg
apt-key export 22F3D138 | gpg --dearmour -o /usr/share/keyrings/debian-bookworm-updates.gpg
apt-key export E562B32A | gpg --dearmour -o /usr/share/keyrings/debian-security-bookworm.gpg
cat > /etc/apt/preferences.d/chromium.pref << 'EOF'
Package: *
Pin: release a=eoan
Pin-Priority: 500
Package: *
Pin: origin "deb.debian.org"
Pin-Priority: 300
Package: chromium*
Pin: origin "deb.debian.org"
Pin-Priority: 700
EOF
shell_text = """\
apt-get update
apt-get install libxtst6
apt-get install chromium chromium-driver
pip install selenium
"""

if silent:
Expand Down
37 changes: 19 additions & 18 deletions ecoscope/mapping/map.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import base64
import os
import tempfile
import time
import typing
import urllib
import warnings
Expand All @@ -10,8 +12,8 @@
import matplotlib as mpl
import numpy as np
import pandas as pd
from pyppeteer import launch
import rasterio
import selenium.webdriver
from branca.colormap import StepColormap
from branca.element import MacroElement, Template

Expand Down Expand Up @@ -183,24 +185,23 @@ def to_png(self, outfile, sleep_time=10, **kwargs):
outfile : str, Pathlike
Output destination
sleep_time : int, optional
Additional seconds to wait before taking screenshot. Should be increased
if map tiles in the output haven't fully loaded but can also be decreased
in most cases.
"""

html_string = self.get_root().render()
Additional seconds to wait before taking screenshot. Should be increased if map tiles in the output haven't
fully loaded but can also be decreased in most cases.
async def capture_screenshot(html_string, outfile):
browser = await launch(options={"args": ["--no-sandbox"]})
page = await browser.newPage()
await page.setViewport({"width": int(self.px_width), "height": int(self.px_height)})
await page.setContent(html_string)
await asyncio.sleep(sleep_time)
await page.screenshot({"path": outfile})
await browser.close()
"""

loop = asyncio.get_event_loop()
loop.run_until_complete(capture_screenshot(html_string, outfile))
with tempfile.NamedTemporaryFile(mode="w", suffix=".html") as tmp:
super().to_html(tmp.name, **kwargs)
chrome_options = selenium.webdriver.chrome.options.Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
driver = selenium.webdriver.Chrome(options=chrome_options)
if self.px_width and self.px_height:
driver.set_window_size(width=self.px_width, height=self.px_height)
driver.get(f"file://{os.path.abspath(tmp.name)}")
time.sleep(sleep_time)
driver.save_screenshot(outfile)

def add_ee_layer(self, ee_object, visualization_params, name) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion ecoscope/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.4.3"
__version__ = "1.4.4"
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ dependencies:
- pyarrow==9.0.0
- pydata-sphinx-theme==0.11.0
- pypdf2==2.10.8
- pyppeteer
- pyproj==3.2.1
- pytest-cov==4.0.0
- pytest-mock==3.10.0
Expand All @@ -39,6 +38,7 @@ dependencies:
- scikit-image==0.19.2
- scikit-learn==1.0.2
- scipy==1.7.3
- selenium==4.5.0
- shapely==1.8.2
- sphinx==5.3.0
- tqdm==4.64.1
Expand All @@ -48,3 +48,4 @@ dependencies:
- nbsphinx
- nbsphinx-multilink
- sphinx-autoapi

6 changes: 3 additions & 3 deletions notebooks/04. EcoMap & EcoPlot/EcoMap.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"source": [
"ECOSCOPE_RAW = \"https://raw.githubusercontent.com/wildlife-dynamics/ecoscope/master\"\n",
"\n",
"# !pip install ecoscope"
"%pip install ecoscope &> /dev/null"
]
},
{
Expand All @@ -48,7 +48,7 @@
"\n",
"import ecoscope\n",
"\n",
"ecoscope.init(selenium=True)"
"ecoscope.init(selenium=True, silent=True)"
]
},
{
Expand Down Expand Up @@ -558,7 +558,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
"version": "3.8.10"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
"pandas",
"plotly",
"pyarrow",
"pyppeteer",
"pyproj",
"pytest-mock",
"rasterio",
"scikit-image",
"scikit-learn",
"scipy",
"selenium",
"shapely",
"tqdm",
"xyzservices",
Expand Down
5 changes: 2 additions & 3 deletions tests/test_png.py → tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

sys.path.append("../ecoscope")


m = EcoMap(draw_control=False)

relative_path = os.path.join("tests/test_output/png_map.png")
m.to_png(relative_path)
relative_path = os.path.join("tests/test_output/new.html")
m.to_html(relative_path)
Binary file removed tests/test_output/png_map.png
Binary file not shown.

0 comments on commit ebd1dd5

Please sign in to comment.