在iPython Notebook中触发文件下载 [英] Trigger File Download within iPython Notebook
问题描述
鉴于在外部服务器上运行的iPython笔记本,是否可以触发文件下载?
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.一个强大而实用的工具将是一个Notebook,它可以从数据库中查询,更改数据并将查询结果下载为CSV文件.
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.
一项快速实验显示,包含以下内容的单元格将呈现一个下载文件的链接.我希望有一个比将数据呈现到html框架中更干净的解决方案.
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>
推荐答案
我从评论中得到了一个有效的答案. FileLink
完成了这项工作,该工作可以从笔记本中下载文件.
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)
出于完整性考虑,下面是FileLinks
的类似示例:
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..
这篇关于在iPython Notebook中触发文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!