Altair: href with file:// urls

Created on 10 May 2019  路  3Comments  路  Source: altair-viz/altair

(Long-time admirer, first-time user. This package is great!)

Description

I'm trying to set up a scatter plot with hrefs to my local file-system, e.g., file:///path/to/my/file.txt. When I click the link, it seems that the file:/// prefix is dropped, and it directs to http://127.0.0.1:8889/path/to/my/file.txt, which obviously does not work. I'm running primarily in jupyter-lab, but it also fails using the stand-alone server.

Opening up the plot in the vega editor shows that the url field is correctly represented, so it's not obvious (to me) if this is a problem with altair, vega, or both. Any help would be much appreciated!

Minimum reproducible example

import altair as alt
from vega_datasets import data
cars = data.cars()
cars['url'] = 'file:///etc/hostname'

alt.Chart(cars).mark_point().encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
    shape='Origin',
    href='url'
)

Why does this matter?

The href's i'm linking to are large media files, and the scatter plot represents some statistics extracted from each file. For various technical and legal reasons, it's not feasible to host the data on a publicly accessible web server, but hyper-linking to a local file URL would be an ideal solution.

Versions

>>> alt.__version__
3.0.1

$ jupyter-lab --version
1.0.0a3

Most helpful comment

That would take a bit of work to customize the web server that way. I think that would be better as a third party extension rather than something in Altair's core, just for the sake of keeping the project maintainable.

All 3 comments

The problem is not Altair or JupyterLab, but sercurity restrictions in your browser. Browsers will generally not allow javascript running at http:// addresses to access data files at file:// addresses, for obvious security reasons.

If you want to load data from disk for use in a chart, you'll need to put the data at a file path location that is visible to the jupyterlab frontend server, and use the appropriate http path.

Obv. Thanks!

Your suggestion makes sense for jupyterlab, but it doesn't generalize to static exports or stand-alone server.

Would it be reasonable / possible to have serve() mode allow this by making a specified directory accessible from the local server? Eg, allow relative urls for hrefs, and allow the user to say something like chart.serve(files='/path/to/directory/i/want/exposed')?

That would take a bit of work to customize the web server that way. I think that would be better as a third party extension rather than something in Altair's core, just for the sake of keeping the project maintainable.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DentonGentry picture DentonGentry  路  3Comments

SuperShinyEyes picture SuperShinyEyes  路  3Comments

pabloinsente picture pabloinsente  路  3Comments

Juan-132 picture Juan-132  路  3Comments

jtbaker picture jtbaker  路  3Comments