【问题标题】:Display IPython.display.HTML class objects inside ipywidgets在 ipywidgets 中显示 IPython.display.HTML 类对象
【发布时间】:2020-08-16 22:02:49
【问题描述】:

我想在 HBox 布局中显示 IPython.display.HTML 对象,即 html_bt 和小部件,即 bt。我该如何做到这一点?甚至可能吗?或者如何将 html_bt 转换为小部件对象? 以下是我的代码:

from ipywidgets import widgets, Layout, HBox
from IPython.display import display, HTML

css_str = '<style>.foo{color:#F00;} )} </style>'


out = widgets.Output()

def OnClick():
    with out:
        print('QQQ')

html_bt=HTML(css_str + '<button class="button-style" onclick="IPython.notebook.kernel.execute(\'OnClick()\')"> <img src="https://www.fnordware.com/superpng/pnggrad16rgb.png" alt="Snow"></button>')

bt = widgets.Button(
    description='Click me',
    disabled=False,
    button_style='', # 'success', 'info', 'warning', 'danger' or ''
    tooltip='Click me',
    icon='check' # (FontAwesome names without the `fa-` prefix)
)


h_box = HBox([bt, display(html_bt)])
h_box

【问题讨论】:

    标签: jupyter-notebook ipython ipywidgets


    【解决方案1】:

    ipywidgets 有自己的 HTML 小部件,如果你使用它,它会提供你想要的吗?

    from ipywidgets import widgets, Layout, HBox, HTML
    # from IPython.display import display, HTML
    
    css_str = '<style>.foo{color:#F00;} )} </style>'
    
    
    out = widgets.Output()
    
    def OnClick():
        with out:
            print('QQQ')
    
    html_bt=HTML(css_str + '<button class="button-style" onclick="IPython.notebook.kernel.execute(\'OnClick()\')"> <img src="https://www.fnordware.com/superpng/pnggrad16rgb.png" alt="Snow"></button>')
    
    bt = widgets.Button(
        description='Click me',
        disabled=False,
        button_style='', # 'success', 'info', 'warning', 'danger' or ''
        tooltip='Click me',
        icon='check' # (FontAwesome names without the `fa-` prefix)
    )
    
    h_box = HBox([bt, html_bt])
    h_box
    

    【讨论】:

    • 不,它没有。我尝试了上面的代码。我可以看到按钮,但在这种情况下没有调用 OnClick 函数。
    猜你喜欢
    • 2019-02-08
    • 2021-04-26
    • 2016-07-20
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    相关资源
    最近更新 更多