Skip to content

improve the way we get the base url #41

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

Closed
wants to merge 5 commits into from
Closed
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
129 changes: 129 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
40 changes: 23 additions & 17 deletions xarray_leaflet/xarray_leaflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@ def plot(self,
if 'proj4def' in m.crs:
# it's a custom projection
if dynamic:
raise RuntimeError('Dynamic maps are only supported for Web Mercator (EPSG:3857), not {}'.format(m.crs))
raise RuntimeError(f'Dynamic maps are only supported for Web Mercator (EPSG:3857), not {m.crs}')
self.dst_crs = m.crs['proj4def']
self.web_mercator = False
self.custom_proj = True
elif m.crs['name'].startswith('EPSG'):
epsg = m.crs['name'][4:]
if dynamic and epsg != '3857':
raise RuntimeError('Dynamic maps are only supported for Web Mercator (EPSG:3857), not {}'.format(m.crs))
raise RuntimeError(f'Dynamic maps are only supported for Web Mercator (EPSG:3857), not {m.crs}')
self.dst_crs = 'EPSG:' + epsg
self.web_mercator = epsg == '3857'
self.custom_proj = False
else:
raise RuntimeError('Unsupported map projection: {}'.format(m.crs))
raise RuntimeError(f'Unsupported map projection: {m.crs}')

self.nodata = self._da.rio.nodata
var_dims = self._da.dims
Expand All @@ -143,8 +143,8 @@ def plot(self,
if set(var_dims) != set(expected_dims):
raise ValueError(
"Invalid dimensions in DataArray: "
"should include only {}, found {}."
.format(tuple(expected_dims), var_dims)
f"should include only {tuple(expected_dims)}, found {var_dims}."

)

if rgb_dim is not None and colormap is not None:
Expand Down Expand Up @@ -272,21 +272,27 @@ def _start(self):
self._da, self.transform0_args = get_transform(self.transform0(self._da, debug_output=self.debug_output))

self.url = self.m.window_url

# the jupyter key we look for
# keeping the last "/" to avoid bug with a (strange) filename as "jupyter.ipynb"
key = '/jupyter/'

# A custom function have been set
if self.get_base_url is not None:
self.base_url = self.get_base_url(self.url)

# Search for the first "jupyter" in url name
elif self.url.find(key) != -1:
i_jupyter = self.url.find(key)
self.base_url = self.url[:i_jupyter + len(key) -1]

# We are in an "exotic" environment and a get_base_url function must be set
else:
if self.url.endswith('/lab'):
# we are in JupyterLab
self.base_url = self.url[:-4]
else:
i_notebooks = self.url.find('/notebooks/')
i_voila = self.url.find('/voila/')
if i_notebooks != -1 and (i_voila == -1 or i_voila > i_notebooks):
# we are in classical Notebooks
self.base_url = self.url[:i_notebooks]
elif i_voila != -1 and (i_notebooks == -1 or i_notebooks > i_voila):
# we are in Voila
self.base_url = self.url[:i_voila]
raise ValueError(f'''
xarray-leaflet default use is in a Jupyter environment.
"jupyter" was not part of {self.url}.
Please provide a get_base_url function.
''')

if self.tile_dir is None:
self.tile_temp_dir = tempfile.TemporaryDirectory(prefix='xarray_leaflet_')
Expand Down