Skip to content

Commit

Permalink
fix pyppeteer bug preventing saving of png
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgitonga committed Sep 15, 2023
1 parent de5abcb commit bf95814
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 56 deletions.
46 changes: 8 additions & 38 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, selenium=False, force=False):
def init(silent=False, pyppeteer=False, force=False):
"""
Initializes the environment with ecoscope-specific customizations.
Parameters
----------
silent : bool, optional
Removes console output
selenium : bool, optional
Installs selenium webdriver in a colab environment
pyppeteer : bool, optional
Installs Pyppeteer and Chrome in a colab environment
force : bool, optional
Ignores `__initialized`
Expand Down Expand Up @@ -76,46 +76,16 @@ def explore(data, *args, **kwargs):

import sys

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

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
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
import nest_asyncio

Package: *
Pin: origin "deb.debian.org"
Pin-Priority: 300
Package: chromium*
Pin: origin "deb.debian.org"
Pin-Priority: 700
EOF
nest_asyncio.apply()

shell_text = """\
apt-get update
apt-get install chromium chromium-driver
pip install selenium
apt-get install libxtst6
"""

if silent:
Expand Down
16 changes: 8 additions & 8 deletions ecoscope/mapping/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,20 @@ 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.
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()
html_string = self.get_root().render()

async def capture_screenshot(html_string,outfile):
browser = await launch()
async def capture_screenshot(html_string, outfile):
browser = await launch(options={"args": ["--no-sandbox"]})
page = await browser.newPage()
await page.setViewport({'width': self.px_width, 'height': self.px_height}) # Set the viewport size as needed
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 page.screenshot({"path": outfile})
await browser.close()

loop = asyncio.get_event_loop()
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.0"
__version__ = "1.4.1"
6 changes: 3 additions & 3 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: ecoscope
channels:
- conda-forge
dependencies:
- python==3.7.12 # Fixed to mirror version on Google Colab
- python==3.10.12 # Fixed to mirror version on Google Colab
- pip==22.3
- git==2.38.1
- affine==2.3.1
Expand All @@ -11,6 +11,7 @@ dependencies:
- backoff==2.2.1
- branca==0.5.0
- earthengine-api==0.1.328
- earthranger-client
- geopandas==0.10.2
- ipywidgets==8.0.2
- jinja2==3.1.2
Expand All @@ -27,6 +28,7 @@ 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 @@ -37,12 +39,10 @@ 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
- xyzservices==2022.9.0
- earthranger-client
- pip:
- coverage[toml]
- nbsphinx
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"backoff",
"branca",
"earthengine-api",
"earthranger-client",
"folium>=0.11.0",
"geopandas",
"igraph",
Expand All @@ -31,17 +32,16 @@
"pandas",
"plotly",
"pyarrow",
"pyppeteer",
"pyproj",
"pytest-mock",
"rasterio",
"scikit-image",
"scikit-learn",
"scipy",
"selenium",
"shapely",
"tqdm",
"xyzservices",
"earthranger-client",
]

setup(
Expand Down Expand Up @@ -76,6 +76,7 @@
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
include_package_data=True,
)
Binary file modified tests/test_output/png_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions tests/test_png.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import sys
import os
sys.path.append('../ecoscope')
from ecoscope.mapping.map import EcoMap

m=EcoMap(draw_control=False)
sys.path.append("../ecoscope")

relative_path=os.path.join('tests/test_output/png_map.png')
m.to_png(relative_path)

m = EcoMap(draw_control=False)

relative_path = os.path.join("tests/test_output/png_map.png")
m.to_png(relative_path)

0 comments on commit bf95814

Please sign in to comment.