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

Python 3 issues on Windows when placing images in base64 in a panel #985

Closed
Jacob-Barhak opened this issue Jan 22, 2020 · 1 comment · Fixed by #986
Closed

Python 3 issues on Windows when placing images in base64 in a panel #985

Jacob-Barhak opened this issue Jan 22, 2020 · 1 comment · Fixed by #986

Comments

@Jacob-Barhak
Copy link
Contributor

The code below generates an html file that contains an image in it and allows linking it. This technique is useful to generate static html files with links and some hovering text.

However, this technique works only on python 2 and breaks on python 3.

import holoviews as hv
import panel as pn
import bokeh
import urllib
import base64
import sys

print ("Python version is: %s"%sys.version)
for library in [hv,pn,bokeh]:
	print ("%s version %s"% ( library, library.__version__))

hv.extension('bokeh')

ImageDir = 'Images'
CommonResourceDir = 'https://jacob-barhak.github.io/CommonResources/'
ExternalResourcesIMAG2019 = 'https://jacob-barhak.github.io/PosterIMAG2019Resources/'

def ConstractImageLinkAnchor(Link, ImageURL, Text, Width):
    try:
        # for python 2
        DataFile = urllib.urlopen(ImageURL)
    except:
        # for python 3
        DataFile = urllib.request.urlopen(ImageURL)
    Data = DataFile.read()
    DataFile.close()
    EncodedImage=base64.b64encode(Data)

    RetStr = '<a title="%s" target="_blank" href="%s"><img src="data:image/png;base64,%s" alt="%s" width="%i"/> </a>'%(Text,Link,EncodedImage,Text,Width)
    return RetStr

Out = pn.panel(ConstractImageLinkAnchor('https://panel.holoviz.org/reference/panes/PNG.html','https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png','This is text to hover over the image',500), width=500, height=500)
Out.save('TestPython3.html') 

The program works fine on python 2 with the following libraries:

Python version is: 2.7.16 |Anaconda, Inc.| (default, Mar 14 2019, 15:42:17) [MSC v.1500 64 bit (AMD64)]
holoviews version 1.13.0a21.post10+gdf1a5803
panel version 0.8.0a2.post3+g82f5514
bokeh version 1.4.0

The program breaks on python 3 with the following output:

Python version is: 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)]
holoviews version 1.12.1
panel version 0.5.1
bokeh version 1.1.0
Traceback (most recent call last):
  File "HoloBug3.py", line 32, in <module>
    Out = pn.panel(ConstractImageLinkAnchor('https://panel.holoviz.org/reference/panes/PNG.html','https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png','This is text to hover over the image',500), width=500, height=500)
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\panel\pane\__init__.py", line 44, in panel
    pane = PaneBase.get_pane_type(obj)(obj, **kwargs)
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\panel\pane\base.py", line 226, in get_pane_type
    applies = pane_type.applies(obj)
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\panel\pane\image.py", line 162, in applies
    return (super(SVG, cls).applies(obj) or
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\panel\pane\image.py", line 44, in applies
    ((os.path.isfile(obj) and obj.endswith('.'+imgtype)) or
  File "C:\Users\Work\Anaconda2\envs\py3\lib\genericpath.py", line 30, in isfile
    st = os.stat(path)
ValueError: stat: path too long for Windows

In short : os.path.isfile breaks on long strings on windows.

@Jacob-Barhak
Copy link
Contributor Author

Fix verified. Script was modified to handle more python 3 issues with bytes. Here is the script that successfully executed on both Python 2 and 3:

import holoviews as hv
import panel as pn
import bokeh
import urllib
import base64
import sys

print ("Python version is: %s"%sys.version)
for library in [hv,pn,bokeh]:
	print ("%s version %s"% ( library.__name__, library.__version__))

hv.extension('bokeh')

ImageDir = 'Images'
CommonResourceDir = 'https://jacob-barhak.github.io/CommonResources/'
ExternalResourcesIMAG2019 = 'https://jacob-barhak.github.io/PosterIMAG2019Resources/'

def ConstractImageLinkAnchor(Link, ImageURL, Text, Width):
    if sys.version[0] == '2':
        # for python 2
        DataFile = urllib.urlopen(ImageURL)
    else:
        # for python 3
        DataFile = urllib.request.urlopen(ImageURL)
    Data = DataFile.read()
    DataFile.close()
    EncodedImage=base64.b64encode(Data)

    if sys.version[0] == '2':
        # for python 2
        EncodedImageAsBytes = EncodedImage
    else:
        # for python 3
        EncodedImageAsBytes = EncodedImage.decode('utf-8')

    RetStr = '<a title="%s" target="_blank" href="%s"><img src="data:image/png;base64,%s" alt="%s" width="%i"/> </a>'%(Text,Link,EncodedImageAsBytes,Text,Width)
    return RetStr

Out = pn.panel(ConstractImageLinkAnchor('https://panel.holoviz.org/reference/panes/PNG.html','https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png','This is text to hover over the image',500), width=500, height=500)
Out.save('TestPython3.html') 

The versions that successfully executed the script were:

Python version is: 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)]
holoviews version 1.13.0a21.post10+gdf1a5803
panel version 0.8.0b1.post4+geaecd6b
bokeh version 1.4.0

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

Successfully merging a pull request may close this issue.

1 participant