有没有一种方法可以在IPython笔记本中显示HTML代码,然后在同一输出单元中显示小部件?

有没有一种方法可以在IPython笔记本中显示HTML代码,然后在同一输出单元中显示小部件?,ipython,ipython-notebook,Ipython,Ipython Notebook,我正在尝试使用小部件编写IPython笔记本。为此,我需要一些HTML标题,后面是输入小部件。我要做到的是: from ipywidgets import widgets from IPython.display import display from IPython.core.display import HTML class buttons(object): def __init__(self): self.buttons = [] for i i

我正在尝试使用小部件编写IPython笔记本。为此,我需要一些HTML标题,后面是输入小部件。我要做到的是:

from ipywidgets import widgets
from IPython.display import display
from IPython.core.display import HTML

class buttons(object):
    def __init__(self):

        self.buttons = []
        for i in xrange(6):

            self.buttons.append( widgets.Button(description = str(i)))
            self.buttons[-1].on_click(self.handle_submit)
            self.buttons[-1].margin=20

        display(HTML("<h1> Heading </h1>"))

        display( widgets.HBox((self.buttons)) )

        self.text = widgets.Text(value="21")
        display(self.text)

    def handle_submit(self, sender):
        self.show(int(self.text.value))

    def show(self, x=None):
        print(1+1)

b = buttons()
从ipywidgets导入小部件
从IPython.display导入显示
从IPython.core.display导入HTML
类按钮(对象):
定义初始化(自):
self.buttons=[]
对于x范围内的i(6):
self.buttons.append(widgets.Button(description=str(i)))
self.button[-1]。单击(self.handle\u submit)
self.按钮[-1]。边距=20
显示(HTML(“标题”))
显示(widgets.HBox((self.buttons)))
self.text=widgets.text(value=“21”)
显示(self.text)
def句柄_提交(自身、发送方):
self.show(int(self.text.value))
def显示(自身,x=无):
打印(1+1)
b=按钮()
然而,即使我在
显示(widgets(…)
函数之前调用
显示(HTML(…))
函数,我还是会得到以下输出(顺序相反):

有没有办法让它看起来像这样:

from ipywidgets import widgets
from IPython.display import display
from IPython.core.display import HTML

class buttons(object):
    def __init__(self):

        self.buttons = []
        for i in xrange(6):

            self.buttons.append( widgets.Button(description = str(i)))
            self.buttons[-1].on_click(self.handle_submit)
            self.buttons[-1].margin=20

        display(HTML("<h1> Heading </h1>"))

        display( widgets.HBox((self.buttons)) )

        self.text = widgets.Text(value="21")
        display(self.text)

    def handle_submit(self, sender):
        self.show(int(self.text.value))

    def show(self, x=None):
        print(1+1)

b = buttons()


我想不出来。

您可以使用VBox将所有内容构建在一起:

from IPython.display import display
from IPython.core.display import HTML

class buttons(object):
    def __init__(self):

        self.buttons = []
        for i in xrange(6):

            self.buttons.append( widgets.Button(description = str(i)))
            self.buttons[-1].on_click(self.handle_submit)
            self.buttons[-1].margin=20

        self.text = widgets.Text(value="21")

        self.header = widgets.HTML(description='',value='<h1> Heading </h1>')
        self.everything = widgets.VBox([self.header,widgets.HBox((self.buttons)),self.text])
        display(self.everything)

        #display(HTML("<h1> Heading </h1>"))

        #display( widgets.HBox((self.buttons)) )


        #display(self.text)

    def handle_submit(self, sender):
        self.show(int(self.text.value))

    def show(self, x=None):
        print(1+1)

b = buttons()
从IPython.display导入显示
从IPython.core.display导入HTML
类按钮(对象):
定义初始化(自):
self.buttons=[]
对于x范围内的i(6):
self.buttons.append(widgets.Button(description=str(i)))
self.button[-1]。单击(self.handle\u submit)
self.按钮[-1]。边距=20
self.text=widgets.text(value=“21”)
self.header=widgets.HTML(description='',value='Heading')
self.everything=widgets.VBox([self.header,widgets.HBox((self.buttons)),self.text])
显示(self.everything)
#显示(HTML(“标题”))
#显示(widgets.HBox((self.buttons)))
#显示(self.text)
def句柄_提交(自身、发送方):
self.show(int(self.text.value))
def显示(自身,x=无):
打印(1+1)
b=按钮()