How to programmatically generate download link?

In a notebook, I want to programmatically generate a download link and pass that URL to a Javascript-based visualization tool for display.

In classic Notebook I was able to do this simply by prepending files/ to the relative URL. For example: files/data/file_to_display.bam. I could then pass that relative URL to the visualization and it would work. In JupyterLab, however, it doesn’t work.

So is there a way to reliably generate a download link to a local file? (from the kernel)

P.S. I noticed that IPython.display.FileLink generates an HTML tag that works when displayed, but I haven’t been able to figure out a way programmatically get the URL in the kernel.

Can you share the code that worked in classic notebook?

The code looks something like what is shown below.

The b object represents a visualization instance (of igv.js). Basically it takes the relative URL shown below and passes it directly to the JS visualization, which is then displayed in another cell.

        "name": "Local BAM",
        "url": "files/data/gstt1_sample.bam",
        "format": "bam"

Here’s a binder link of the full notebook in action. By default it open in classic notebook and works, but if you change the URL to lab, it does not.

1 Like

Here is how we take a relative URL from the kernel and convert it to a url that can be used in the browser to get a data set in the vega project: jupyterlab/index.ts at a6f5b0c67f86334f42d2dd83ed8c636bbe938b93 · jupyterlab/jupyterlab · GitHub

See also the discussion around where we introduced that change: Use the url resolver to its full capabilities for vega. by jasongrout · Pull Request #7098 · jupyterlab/jupyterlab · GitHub, Vega extension not loading in subdirectory · Issue #7047 · jupyterlab/jupyterlab · GitHub


That is very helpful! Thank you.