Trigger File Download Within Ipython Notebook
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 dow
Solution 1:
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, FileLinklocal_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, FileLinkslocal_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..
Solution 2:
Alternatively, if the file's aren't too large and hence suitable for base64 encoding. This only works in 'recent browsers', but ipynb users will likely have one. This example works with JSON data.
from IPython.display import display, HTML
import json
import base64
encoded = base64.b64encode(json.dumps(data).encode('utf-8')).decode('utf-8')
HTML(f'<a href="data:application/json;base64,{encoded}" download="download.json">download</a>')
Solution 3:
You can use the urllib
library to download files or request URLs.
testfile = urllib.URLopener()
testfile.retrieve("http://exmaple.com/file.txt", "file.txt")
Post a Comment for "Trigger File Download Within Ipython Notebook"