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

Binder env not working for local COG #1

Closed
giswqs opened this issue Jan 12, 2022 · 25 comments
Closed

Binder env not working for local COG #1

giswqs opened this issue Jan 12, 2022 · 25 comments

Comments

@giswqs
Copy link

giswqs commented Jan 12, 2022

The binder env works well when rending a remote COG. However, it does not seem to work for a local COG (a file in the repo). The tile layer won't show up.

!wget https://github.com/giswqs/data/raw/main/raster/srtm90.tif
from localtileserver import get_leaflet_tile_layer, TileClient
from ipyleaflet import Map

# First, create a tile server from local raster file
tile_client = TileClient('srtm90.tif')

# Create ipyleaflet tile layer from that server
t = get_leaflet_tile_layer(tile_client)

# Create ipyleaflet map, add tile layer, and display
m = Map(center=tile_client.center())
m.add_layer(t)
m
@banesullivan
Copy link
Owner

This is known/expected because of banesullivan/localtileserver#29

I do not know of a way to expose a server from the binder env that is accessible in the user's browser

@banesullivan
Copy link
Owner

TileClient will not work in this sort of environment, only RemoteTileClient which is connecting to a production server I have running (not very performant)

@giswqs
Copy link
Author

giswqs commented Jan 12, 2022

I was exploring the following repo this morning. It can run a streamlit web app on Binder. Not sure if it would be useful in your case.

https://github.com/chekos/testing-streamlit-mybinder
https://mybinder.org/v2/gh/chekos/testing-streamlit-mybinder/master?urlpath=proxy/8501/

@banesullivan
Copy link
Owner

Ah, that demo is just what I needed to see! I will see if I can do something similar with localtileserver

@banesullivan
Copy link
Owner

banesullivan commented Feb 6, 2022

This is now working using jupyter-server-proxy. Please note the line at the top of example.ipynb that enables this to work:

# Set host forwarding for MyBinder
import os
# Make sure `notebooks.gesis.org/binder/jupyter/user/` matches your URL address
os.environ['LOCALTILESERVER_CLIENT_HOST'] = f"notebooks.gesis.org/binder/jupyter/user/{os.environ['JUPYTERHUB_USER']}"

That HOST URL might change on different mybinder or jupyterhub deployments

@giswqs
Copy link
Author

giswqs commented Feb 6, 2022

I just tested it on binder with the latest localtilesever. It does not seem to work.

https://gishub.org/leafmap-binder

image

@banesullivan
Copy link
Owner

banesullivan commented Feb 6, 2022

Make sure to use the --pre flag for pip as I only released a dev version for this 0.4.1.dev0

I want to double check things before doing an actual release

@banesullivan
Copy link
Owner

banesullivan commented Feb 6, 2022

Also, you'll need to install https://jupyter-server-proxy.readthedocs.io/en/latest/ and make sure its extensions are enabled

@banesullivan
Copy link
Owner

Ignore the --pre flag comment. Just noticed you're installing from git

@giswqs
Copy link
Author

giswqs commented Feb 6, 2022

This is what I tried with my leafmap binder. Still not working. Not sure what I did wrong.

https://gishub.org/leafmap-binder

!pip install git+https://github.com/banesullivan/localtileserver.git
!pip install jupyter-server-proxy
!jupyter serverextension enable --sys-prefix jupyter_server_proxy

image

@giswqs
Copy link
Author

giswqs commented Feb 6, 2022

Your gesis binder works fine. So the issue is probably related to how the binder env is set up. Your uses docker, mine uses environment.yml

image

@banesullivan
Copy link
Owner

banesullivan commented Feb 6, 2022

I think you'll have to relaunch Jupyter after enabling the server extension

Also make sure that code block at the top setting LOCALTILESERVER_CLIENT_HOST is uncommented and that variable is set

@giswqs
Copy link
Author

giswqs commented Feb 6, 2022

Yes, i did restart the kernel several times. Also uncommented that line. Still not working. Will look into it tomorrow. Thanks

@banesullivan
Copy link
Owner

After uncommenting that line, make sure that value matches the URL in your browser

@giswqs
Copy link
Author

giswqs commented Feb 6, 2022

Is this correct?

if os.environ.get('JUPYTERHUB_USER'):  
    os.environ['LOCALTILESERVER_CLIENT_HOST'] = f"hub.gke2.mybinder.org/user/{os.environ['JUPYTERHUB_USER']}"
else:
    os.environ['LOCALTILESERVER_CLIENT_PORT'] = '8888'
    os.environ['LOCALTILESERVER_CLIENT_HOST'] = "127.0.0.0"
    os.environ['LOCALTILESERVER_CLIENT_PREFIX'] = 'proxy/{port}'

@banesullivan
Copy link
Owner

You need to set os.environ['LOCALTILESERVER_CLIENT_PREFIX'] = 'proxy/{port}' in both cases

note that MyBinder's host can change anytime you launch, so hub.gke2.mybinder.org/user/ will not always be correct

@giswqs
Copy link
Author

giswqs commented Feb 6, 2022

Is there a way to retrieve the host URL programmatically?

@banesullivan
Copy link
Owner

There's always a way! ....but I couldn't figure it out... 😞

The host is not listed in os.environ but it could be somewhere...

I tried:

from notebook import notebookapp
servers = list(notebookapp.list_running_servers())
print(servers)

... 'hostname': '0.0.0.0' ...

But there's another webserver on top that manages the traffic.

I bet we could make a super hacky way of getting this through a custom ipython widget where the widget accesses window.location.href in the browser and then sends that data back to Python. However, this would be quite hacky and would require the user to display the widget.

This question might be something https://discourse.jupyter.org/c/binder/12 could help address

@banesullivan
Copy link
Owner

ACTUALLY! I just found a much, much simpler way to do this.

@banesullivan
Copy link
Owner

I will follow up in a PR in localtileserver

@banesullivan
Copy link
Owner

banesullivan commented Feb 6, 2022

See banesullivan/localtileserver#67

After that, the following should be sufficient:

# Set host forwarding for MyBinder
import os
os.environ['LOCALTILESERVER_CLIENT_PREFIX'] = f"{os.environ['JUPYTERHUB_SERVICE_PREFIX'].lstrip('/')}/proxy/{{port}}"

@giswqs
Copy link
Author

giswqs commented Feb 7, 2022

Fantastic! I can confirm that v0.4.3 works like a charm with Binder. How to make it work with other remote environments not using JupyterHub (e.g., streamlit)?

# Set host forwarding for MyBinder
import os
os.environ['LOCALTILESERVER_CLIENT_PREFIX'] = f"{os.environ['JUPYTERHUB_SERVICE_PREFIX'].lstrip('/')}/proxy/{{port}}"

@banesullivan
Copy link
Owner

How to make it work with other remote environments not using JupyterHub (e.g., streamlit)?

Let's open a new issue in localtileserver to address this

It should be a simple configuration with the settings I added for JupyterHub

@wzp8023391
Copy link

how to display raster image in remote environments, especially for streamlit?

@giswqs
Copy link
Author

giswqs commented Aug 10, 2022

@wzp8023391 Unfortunately, localtileserver does not support streamlit. It is a streamlit problem rather than localtileserver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants