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

Using ipyleaflet on Google Colab #864

Closed
giswqs opened this issue Sep 2, 2021 · 2 comments
Closed

Using ipyleaflet on Google Colab #864

giswqs opened this issue Sep 2, 2021 · 2 comments

Comments

@giswqs
Copy link
Contributor

giswqs commented Sep 2, 2021

Exciting news! Goolge Colab now supports custom widgets like ipyleaflet (googlecolab/colabtools#498 (comment))

One caveat is that we have to add the following code block when displaying a map. I am trying to embed this code block into the Map class so that users don't have to add this long string of code manually.

from IPython.display import Javascript
display(Javascript('''
  google.colab.widgets.installCustomManager('https://ssl.gstatic.com/colaboratory-static/widgets/colab-cdn-widget-manager/6a14374f468a145a/manager.min.js');
'''))

Based on the ipython repr_mimebundle() function, I tried to add some code to this function, but it has no effect. Any advice? @martinRenou @davidbrochart @SylvainCorlay @jasongrout @maartenbreddels @blois

Relevant issues:

A Colab example: https://colab.research.google.com/drive/1DIjJP4HrbsmGsDluKYKSQceFWnhj2wPz

import ipyleaflet

class Map(ipyleaflet.Map):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def _repr_mimebundle_(self, **kwargs):

        # the code below has no effect.
        layer_control = ipyleaflet.LayersControl(position="topright")
        self.add_control(layer_control)

        # display(Javascript('''
        # google.colab.widgets.installCustomManager('https://ssl.gstatic.com/colaboratory-static/widgets/colab-cdn-widget-manager/6a14374f468a145a/manager.min.js');
        # '''))

m = Map()
m

@martinRenou
Copy link
Member

I guess you could add this code snippet in Leaflet's __init__.py instead of under the _repr_mimebundle_?

Something like:

from IPython.display import Javascript

if colab:
    display(Javascript('''
      google.colab.widgets.installCustomManager('https://ssl.gstatic.com/colaboratory- 
   static/widgets/colab-cdn-widget-manager/6a14374f468a145a/manager.min.js');
    '''))

Overwriting the _repr_mimebundle_ function might break the widget's display logic.

@blois
Copy link

blois commented Sep 2, 2021

Colab will be iterating on the snippet required and will make it a bit easier to use (including making it one-time opt-in for the notebook).

This was published in googlecolab/colabtools#498 (comment) to allow collecting feedback on how well the API worked for various widget packages before we attempt to call it stable.

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