Luk*_*kas 11 html python jupyter-notebook ipywidgets
通过以下最小示例,我可以创建与Jupyter笔记本交互的按钮和HTML表格,该表格显示在笔记本中.
import ipywidgets
from IPython.display import display
from IPython.core.display import HTML
def func(btn):
print('Hi!')
btn1 = ipywidgets.Button(description="Click me!")
btn1.on_click(func)
btn2 = ipywidgets.Button(description="Click me!")
btn2.on_click(func)
display(btn1)
display(btn2)
display(HTML(
'<table>' +
'<tr><td>Something here</td><td>Button 1 here</td></tr>' +
'<tr><td>Something here</td><td>Button 2 here</td></tr>' +
'</table>'
))
Run Code Online (Sandbox Code Playgroud)
我现在想将按钮放在html表中.我尝试调查该方法,Widget._ipython_display_()但这不允许我使用我自己的html表中的按钮.
(请参阅小表作为示例.我想将按钮放在一个大表中,并使用按钮从数据库中删除行.)
在这个问题中,想知道如何相对于彼此放置小部件.在这里,我想将小部件放在其他HTML代码中.
似乎没有一个简单的方法来实现这一目标。您将必须构建一个自定义ipywidget来显示表,或者手动编写一个HTML按钮的代码,对此您将拥有完全控制权。
我能找到的最好的方法是使用HBox内的VBoxes数组模拟表的方法:
import ipywidgets as widgets
from IPython.display import display
def func(btn):
print('Hi!')
btn1 = widgets.Button(description="Click me!")
btn1.on_click(func)
btn2 = widgets.Button(description="Click me!")
btn2.on_click(func)
# This is where you fill your table
cols = [
# Each tuple contains a column header and a list of items/widgets
('Col1', ['hello', 'goodbye']),
('Col2', ['world', 'universe']),
('Buttons', [btn1, btn2]),
]
vboxes = []
for header, data in cols:
vboxes.append(widgets.VBox([widgets.HTML('<b>%s</b>' % header)] + [
d if isinstance(d, widgets.Widget) else widgets.HTML(str(d)) for d in data],
layout=widgets.Layout(border='1px solid')))
hbox = widgets.HBox(vboxes)
display(hbox)
Run Code Online (Sandbox Code Playgroud)
结果:
| 归档时间: |
|
| 查看次数: |
1633 次 |
| 最近记录: |