【问题标题】:How to use FileUpload widget in jupyter lab?如何在 jupyter 实验室中使用 FileUpload 小部件?
【发布时间】:2020-11-22 17:05:43
【问题描述】:

我想在 jupyter 实验室中使用 FileUpload 小部件。 我的笔记本单元格中有以下代码行:

uploader = widgets.FileUpload()
uploader

在 jupyter notebook 中,单元格的输出是一个可点击的按钮,我可以使用它来上传文件。在 jupyter 实验室中,输出如下:

FileUpload(value={}, description='Upload')

这是uploader 对象的信息:

Type:           FileUpload
String form:    FileUpload(value={}, description='Upload')
File:           ~/miniconda3/envs/fastai2/lib/python3.7/site-packages/ipywidgets/widgets/widget_upload.py

是否可以让这个小部件在 jupyter lab 上工作?如果是这样,我应该如何进行?

【问题讨论】:

    标签: python file-upload jupyter-notebook jupyter-lab


    【解决方案1】:

    如果您使用的是开箱即​​用的 jupyterlab,它默认没有启用 ipywidgets,您需要在启用扩展后重新构建它。按照here的步骤:

    1. 安装nodeJS
    2. pip install ipywidgets
    3. jupyter nbextension enable --py widgetsnbextension
    4. jupyter labextension install @jupyter-widgets/jupyterlab-manager
    5. (可能需要重新启动您的实验室)

    它说较新的 Jupyterlab 已启用它,但我仍然遇到问题,具体取决于平台。手动安装通常是要走的路。

    【讨论】:

    • 我认为我缺少的步骤是第 3 步或第 4 步,按照您的指示,一切正常。谢谢!
    • @StatisticDean,是的,这并不明显,因为通常安装一个模块就足够了。不过,JupyterLab 并非如此。每次安装扩展或更新时,都需要重新构建。
    【解决方案2】:

    对我来说,它在之后起作用

    pip install jupyterlab-widgets
    jupyter labextension install @jupyter-widgets/jupyterlab-manager
    

    另见

    https://developer.aliyun.com/mirror/npm/package/@jupyter-widgets/jupyterlab-manager

    用法

    from ipywidgets import FileUpload
    from IPython.display import display
    upload = FileUpload(accept='.txt', multiple=True)
    display(upload)
    

    with open('z_merged_output.txt', 'wb') as output_file: 
        for uploaded_filename in upload.value:
            content = upload.value[uploaded_filename]['content']   
            output_file.write(content) 
    

    【讨论】:

      【解决方案3】:

      如果您已经安装了ipywidgets,您可能需要更新它:

      pip install -U ipywidgets
      

      然后安装新版本的文件:

      jupyter nbextension install --py widgetsnbextension
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-09
        • 1970-01-01
        • 2018-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-03
        • 2018-10-03
        相关资源
        最近更新 更多