geoplotlib icon indicating copy to clipboard operation
geoplotlib copied to clipboard

Jupyter integration

Open thomasjungblut opened this issue 8 years ago • 7 comments

Hey Andrea,

Do you have any idea how to embed the pyglet window into a jupyter notebook? Currently I output as an image and just reference it from there. Any better idea or integration available?

thomasjungblut avatar Jul 26 '16 11:07 thomasjungblut

Unfortunately you cannot embed a live pyglet canvas, but you can inline a screenshot using geoplotlib.inline() in a cell within your notebook.

andrea-cuttone avatar Jul 26 '16 12:07 andrea-cuttone

Good idea, thanks! I get that exception when doing it from my jupyter notebook:

C:\Anaconda3\lib\site-packages\geoplotlib\__init__.py in inline(width)
     69     if os.path.isfile(fname + '.png'):
     70         with open(fname + '.png', 'rb') as fin:
---> 71             base64 = urllib.parse.quote(fin.read().encode("base64"))
     72 
     73         image_html = "<img style='width: %dpx; margin: 0px; float: left; border: 1px solid black;' src='data:image/png;base64,%s' />" % (width, base64)

AttributeError: 'bytes' object has no attribute 'encode'

Python 3.5.2 and geoplotlib (0.3.1) if that matters.

thomasjungblut avatar Jul 26 '16 13:07 thomasjungblut

I think it is an issue with the encode method which was discontinued in python 3.

I will try to take a look.

andrea-cuttone avatar Jul 27 '16 08:07 andrea-cuttone

Related question - I am able to use geoplotlib.inline() successfully. However, if I export the notebook to pdf, the plots do not show. I am wondering if there is any solution to this.

willjhenry avatar Oct 06 '16 16:10 willjhenry

Here is a fix for the error in python 3.5.2 and geoplotlib 0.3.2:

$ diff /usr/local/lib/python3.5/dist-packages/geoplotlib/__init__.py /usr/local/lib/python3.5/dist-packages/geoplotlib/__init__.py.bak
1d0
< import base64
72,75c71
<             encoded = base64.b64encode(fin.read())
<             b64 = urllib.parse.quote(encoded)
< 
<         image_html = "<img style='width: %dpx; margin: 0px; float: left; border: 1px solid black;' src='data:image/png;base64,%s' />" % (width, b64)
---
>             base64 = urllib.parse.quote(fin.read().encode("base64"))
76a73,74
>         image_html = "<img style='width: %dpx; margin: 0px; float: left; border: 1px solid black;' src='data:image/png;base64,%s' />" % (width, base64)
>         

kkaiser avatar Feb 28 '17 09:02 kkaiser

@kkaiser Could you PR those changes so we might get a working version for python3, please?

timgrossmann avatar Nov 15 '18 10:11 timgrossmann

Done: https://github.com/andrea-cuttone/geoplotlib/pull/44

kkaiser avatar Nov 26 '18 21:11 kkaiser