Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display IPython.display.HTML class objects inside ipywidgets

I want to display IPython.display.HTML object i.e. html_bt and widget i.e. bt inside HBox Layout. How do I achieve that? Is it even possible? Or how do I convert html_bt to widget objects? Following is my code:

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
like image 793
Vaishnavi Dongre Avatar asked Nov 15 '22 12:11

Vaishnavi Dongre


2 Answers

ipywidgets has its own HTML widget, if you use that does it give what you want?

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
like image 115
ac24 Avatar answered Dec 06 '22 17:12

ac24


One slightly long-winded fix for this is to display your IPython.display.HTML in a ipywidgets.widgets.Output panel and provide that to the HBox.

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)
)
html_out = widgets.Output()
with html_out:
    display(html_bt)

h_box = HBox([bt, html_out])
h_box
like image 45
Conor McM Avatar answered Dec 06 '22 18:12

Conor McM