Ben*_*ikt 10 html python jupyter-notebook ipywidgets nbconvert
我创建了一个应该在选项卡小部件中显示绘图的笔记本。据我了解,要在选项卡小部件中包含类似绘图的内容,我需要将其包装在输出小部件中。在笔记本本身中它可以工作,但是当我通过 nbconvert 将其转换为 html 时,它会产生错误的输出。
滑块、按钮或文本等小部件出现在标签小部件中应有的位置,但是当我使用输出小部件捕捉绘图(甚至来自 print() 函数的一些文本)时,它出现在标签环境之前,该环境本身是空的.
这是每个选项卡一个绘图的外观(在笔记本中工作): 在笔记本中的选项卡中绘图
这就是它在 nbconvert 之后的样子(在 html 中)。这些图出现在标签环境之前: Plots before tabs in html
请注意,nbconvert 包含其他小部件以及具有其他内容的选项卡。
这是使用的代码:
# Import libraries
import pandas as pd
import matplotlib.pyplot as plt
import ipywidgets as widgets
import numpy as np
# Generated data for plotting
data = pd.DataFrame()
for i in range(5):
data[i] = np.random.normal(size = 50)
Run Code Online (Sandbox Code Playgroud)
现在这部分在笔记本中有效,但在 html 中无效,但正如您将看到的,它似乎与输出小部件有关,因为我不处理绘图或打印文本。
# This does not work with plots
children = []
for i in range(data.shape[1]):
out = widgets.Output()
with out:
fig, axes = plt.subplots()
data[i].hist(ax = axes)
plt.show()
children.append(out)
tab = widgets.Tab()
tab.children = children
for i in range(len(children)):
tab.set_title(i, "Plot " + str(i))
tab
# And this does not work with printed output
children = []
for i in range(5):
out = widgets.Output()
with out:
print("This is text", i)
children.append(out)
tab = widgets.Tab()
tab.children = children
for i in range(len(children)):
tab.set_title(i, "Text " + str(i))
tab
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用不同的小部件类型(例如文本),它会在笔记本和 nbconvert 的 html 输出中正确显示。
# This works with the Text widget
children = []
for i in range(5):
out = widgets.Text(description="P"+str(i))
children.append(out)
tab = widgets.Tab()
tab.children = children
for i in range(len(children)):
tab.set_title(i, "Text " + str(i))
tab
Run Code Online (Sandbox Code Playgroud)
那么,有什么我可以改变的东西来使它真正起作用吗?我最终需要的是一种在 N 个选项卡中显示 N 个图的方法...
我有同样的问题。更新nbconvert(例如通过pip install --upgrade nbconvert)解决了它。
引用自https://github.com/jupyter/nbconvert/issues/923:
MSeal 于 2020-09-07 发表评论:
是的,这个问题已在jupyter/nbclient#24中解决,并且 nbclient >= 0.4.0 已修复。NBconvert 6.0(应该明天发布)默认使用 nbclient,总体问题应该会消失。如果您愿意,您可以在今天的 6.0.0rc0 版本上尝试一下。
| 归档时间: |
|
| 查看次数: |
880 次 |
| 最近记录: |