Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger File Download within iPython Notebook

Tags:

Given an iPython notebook running on an external server, is there a way to trigger a file download?

I would like to either be able to have the notebook be able to initiate the download of a file living on the the external server to the where the notebook is being rendered locally, or perform a direct string dump from the notebook workspace into a text file, downloaded locally.

I.E. a powerful and tool would be a Notebook that can query from a database, alter data, and download the query results as a CSV file.

Notebook

A quick experiment showed that a cell containing the following renders a link which downloads a file. I'm hoping for a cleaner solution than rendering data into an html frame.

%%html
<a href="data:application/octet-stream,'string of things'">Download a file</a>
like image 893
zachd1_618 Avatar asked Oct 21 '14 23:10

zachd1_618


People also ask

How do I download files from notebook?

You just need to click the checkbox next to the file you want to download and then click the "download" button at the top of the pane. The only drawback is it only works for individual files; not directories or multiple files. then a link will display below the cell.

How do I download the output file from Jupyter Notebook?

If you are using Jupyter notebook, you can go to the "File" tab on the top left part of the notebook and click "Open". It shows you the content of the current directory. You can select your data file with different format (CSV, text, etc) and then you can download it in your local computer.


1 Answers

I got a working answer from the comments. FileLink did the job, which was to be able to download a file from a notebook.

from IPython.display import display, FileLink

local_file = FileLink('./demo.xlsx', result_html_prefix="Click here to download: ")
display(local_file)

enter image description here

For the sake of completeness, here is a similar example with FileLinks:

from IPython.display import display, FileLinks

local_file = FileLinks('./data/', result_html_prefix="Click here to download: ")
display(local_file)

enter image description here

It's not very pretty, so would love some advice for styling it..

like image 90
Nick Brady Avatar answered Oct 27 '22 04:10

Nick Brady