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.
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>
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.
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.
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)
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)
It's not very pretty, so would love some advice for styling it..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With