【问题标题】:IPython display always displays widgets before Text/MarkdownIPython 显示总是在 Text/Markdown 之前显示小部件
【发布时间】:2017-08-24 18:42:38
【问题描述】:
我正在使用 IPython 模块在 Jupyter Notebook 上开展一个项目,我正在尝试同时显示小部件和 Markdown 格式的文本。但是,我无法在显示小部件之前显示文本。以下代码sn -p,例如:
import ipywidgets as widgets
from IPython.display import display, Markdown
display(Markdown("## Enter your name"))
name = widgets.Text(description="Enter your name: ")
display(name)
显示this output,即使我要求在显示小部件之前显示降价。如何强制 Jupyter Notebook 按我想要的顺序显示?
【问题讨论】:
标签:
python
widget
ipython
jupyter-notebook
ipython-notebook
【解决方案1】:
我相信这个错误已经在 ipywidgets 的最新版本 7 中得到修复。更新到 7.0 后尝试相同的代码。您可以使用以下命令进行升级(假设您在 anaconda 上运行)。
conda install -c conda-forge ipywidgets
如果它仍然对您不起作用,请尝试使用 dominate 和 HTML 小部件。首先从命令行安装dominant,pip install dominate然后你可以运行以下命令;
import ipywidgets as widgets
from dominate import tags
from IPython.display import display
header = widgets.HTML(tags.h2("Enter your name").render())
name = widgets.Text(description="Enter your name: ")
display(header, name)
对于改进的布局,上面的代码与 HBox 和 Label 小部件的代码相同;
import ipywidgets as widgets
from dominate import tags
from IPython.display import display
header = widgets.HTML(tags.h3("Enter your name").render())
name = widgets.Text()
namebox = widgets.HBox([widgets.Label("Enter your name: "), name])
display(header, namebox)