首页 > 解决方案 > Jupyter 笔记本下载按钮

问题描述

我正在尝试在 jupyter notebook ( IPython) 中实现下载按钮。我知道按钮小部件确实存在于 jupyter 中,如下所示。

from ipywidgets import Button

...

btn_download = widgets.Button(
    description='Download',
    button_style='', # 'success', 'info', 'warning', 'danger' or ''
    tooltip='Download',
    icon='download',
    layout=button_layout )

# Then implement download in on_click 
def on_button_download_clicked(b):
    # Handle download using urlopen
    filedata = urllib.request.urlopen(r'file://' + filepath)
    datatowrite = filedata.read()
    with open("download.fid", 'wb') as f:  
        f.write(datatowrite)

# Register callback
btn_download.on_click(on_button_download_clicked)

但是,这似乎不起作用。我已经尝试了一些其他方法,例如使用urlretrieve,但仍然无法正常工作。

我也知道ipython.display.FileLink存在诸如使用之类的解决方案,但我想以按钮的形式使用它。

这有什么解决方法吗?

标签: pythonjupyter-notebookipythonipywidgets

解决方案


推荐阅读