【问题标题】:Ipywidgets with Google ColaboratoryGoogle Colaboratory 的 Ipywidgets
【发布时间】:2018-04-26 10:06:05
【问题描述】:

我正在尝试将 ipywidgets 与 Google Colaboratory 一起使用,并且(与 plotly 一样)最简单的 example from the docs 不起作用。下面的代码在本地笔记本中显示了一个滑块,但在 Google 笔记本中只返回 10 和 <function __main__.f>

!pip install ipywidgets

from ipywidgets import interact

def f(x):
  return x

interact(f, x=10)

我可以使用其他自定义初始化来启用小部件吗?

【问题讨论】:

  • 您好,您找到解决方法了吗?我有同样的问题
  • 不幸的是还没有解决方案:(
  • 我找到了如何启用 nbextension,但仍然无法正常工作 !pip install widgetsnbextension !jupyter nbextension install --debug /usr/local/lib/python3.6/dist-packages/widgetsnbextension !jupyter nbextension enable --debug /usr/local/lib/python3.6/dist-packages/widgetsnbextension !jupyter nbextension list
  • @AlejandroQH 可能,不知何故,安装/启用模块后必须重新启动 Jupyter 服务器。

标签: jupyter-notebook ipywidgets google-colaboratory


【解决方案1】:

更新 2:核心 ipywidgets 现在可以在 Colab 中使用,许多自定义小部件也是如此!特别是,base、controls、FileUpload、Image 和 output 小部件都在 colab 中工作。详情请见https://github.com/googlecolab/colabtools/issues/498

(经过调整的原始答案):ipywidgets 不要只使用 Colab:我们有不同的安全模型,其中每个输出都在其自己的 iframe 中(与 Colab 主页面的来源不同)。这可以防止 ipywidgets 在 Colab 端没有更改的情况下工作。

【讨论】:

  • 以后有计划添加这个吗?
  • github.com/googlecolab/colabtools/issues/60 是要关注的问题并竖起大拇指。
  • 任何人都使用了图像小部件,我尝试了文档中给出的简单代码,但执行后单元格不显示任何内容。
  • 当前要跟踪的 github 问题是:#498
【解决方案2】:
!pip install ipywidgets

# this will allow the notebook to reload/refresh automatically within the runtime
%reload_ext autoreload
%autoreload 2

from ipywidgets import interact

def f(x):
  return x

interact(f, x=10)

【讨论】:

    【解决方案3】:

    我认为现在 Ipywidgets 正在与 Google Collaboratory 合作。我测试了一些装饰器,运行顺利。

    您的代码导致:

    【讨论】:

    • @dasilvadaniel 它仍然对我不起作用,单元格已运行,但没有显示绘图。当我输入!jupyter --version 时,我得到输出:jupyter core : 4.5.0 jupyter-notebook : 5.2.2 qtconsole : 4.5.2 ipython : 5.5.0 ipykernel : 4.6.1 jupyter client : 5.3.1 jupyter lab : not installed nbconvert : 5.5.0 ipywidgets : 7.5.0 nbformat : 4.4.0 traitlets : 4.3.2 。你的配置是什么?
    • 恐怕我和你的设置一样:jupyter core : 4.5.0 jupyter-notebook : 5.2.2 qtconsole : 4.5.2 ipython : 5.5.0 ipykernel : 4.6。 1 jupyter 客户端:5.3.1 jupyter lab:未安装 nbconvert:5.5.0 ipywidgets:7.5.0 nbformat:4.4.0 traitlets:4.3.2 我的运行时间是 Python3。你能检查一下吗?也许您的 cookie 有问题或与您的浏览器有关的其他问题?
    【解决方案4】:

    ipywidgets 现在在 colab 中有点支持;一个值得注意的例外是ipywidgets.Image。见https://github.com/googlecolab/colabtools/issues/587

    【讨论】:

    • 是的,图像不工作对我来说是一个挫折,因为我正在尝试构建一些简单的图像标记器来帮助手动标记图像以构建新数据集
    【解决方案5】:

    截至 2021-12 年,ipywidgets(小部件)在 Colab 中完美运行。好吧,也许不是 100%,但数量相当可观。

    您只需要导入它们(以您喜欢的任何方式,以下 2 个示例)

    import ipywidgets as widgets
    from ipywidgets import interact, interactive, fixed
    

    并使用(简单示例)

    widgets.Select(
        options=['Linux', 'Windows', 'macOS'],
        value='macOS',
        # rows=10,
        description='OS:',
        disabled=False
    )
    

    这里有很棒的文档https://ipywidgets.readthedocs.io

    【讨论】:

      猜你喜欢
      • 2019-04-01
      • 2019-05-29
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 2018-07-09
      • 1970-01-01
      • 2018-07-29
      相关资源
      最近更新 更多