5

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

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

No, it does not. I tried out the above code. I can see the button but OnClick function is not getting called in this case.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.