1

I am using bokeh to display pins on a map and want when hoovering to display a JPG. The code below works well when the image is from the internet (i.e. with a URL) but how can I make it work if the image is on the local drive, i.e. 'C:.....\pic.jpg'?

This works:

from bokeh.plotting import ColumnDataSource, figure, output_file, show
source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[2, 5, 8, 2, 7],
    desc=['A', 'b', 'C', 'd', 'E'],
    imgs=[
        'https://docs.bokeh.org/static/snake.jpg',
        'https://docs.bokeh.org/static/snake2.png',
        'https://docs.bokeh.org/static/snake3D.png',
        'https://docs.bokeh.org/static/snake4_TheRevenge.png',
        'https://docs.bokeh.org/static/snakebite.jpg'
    ]))

This doesn't work:

from bokeh.plotting import ColumnDataSource, figure, output_file, show
source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[2, 5, 8, 2, 7],
    desc=['A', 'b', 'C', 'd', 'E'],
    imgs=[
        'C:\snake.jpg',
        'C:\snake2.png',
        'C:\snake3D.png',
        'C:\snake4_TheRevenge.png',
        'C:\snakebite.jpg'
    ]))
gtomer
  • 5,643
  • 1
  • 10
  • 21
  • 1
    This is not possible, full stop. Browsers will not allow loading local files like this into HTML Canvas, for security reasons. There's nothing we can do about that. The files will have to be properly served from a web server, even just one running on localhost. – bigreddot Sep 23 '22 at 05:13
  • 1
    BUt if I type on a browser: 'C:...' it will work.... – gtomer Sep 23 '22 at 05:14
  • If I install IIS - this can be done via 'localhost\'? – gtomer Sep 23 '22 at 05:15
  • 1
    Yes as long as it is an http (or https) url. And the browser will load *a local file*. What it will *not* do is load a local file *into another existing page*. – bigreddot Sep 23 '22 at 05:18
  • So basically if I put the images in the 'C:\inetpub\wwwroot' folder and access it e.g. http://localhost/photos/cat.jpg it should load? – gtomer Sep 23 '22 at 07:33
  • 1
    You also need to confiigure the web server for cross-domain sharing https://stackoverflow.com/questions/68013097/unable-to-load-image-from-localhost-url-because-of-cross-origin – bigreddot Sep 23 '22 at 19:01

0 Answers0