我正在尝试创建一个 Jupyter Notebook 来帮助一些同事以交互方式访问一些数据。我想给他们一堆小部件,让他们可以使用不同的标准来过滤数据。
主要是它工作起来很漂亮,但我无法调整两个小部件之间的间距,这会让我抓狂。
我已尝试按照说明进行操作 here , 但是这两个按钮总是紧挨着。
class Dashboard(object):
def __init__(self):
item_layout = Layout(flex='1 1 auto',
width='auto')
toggle = widgets.ToggleButtons(
options=["Foo", "Bar"],
value="Foo",
description="Foobar:",
disable=False,
layout=item_layout,
)
check = widgets.Checkbox(
value=True,
description="Checked",
disabled=False,
layout=item_layout,
)
box_layout = Layout(display='flex',
flex_flow='row',
justify_content='space around',
width='500px',
button_layout='solid',
)
buttons = widgets.Box(children=[
widgets.interactive(self._set_foobar, foobar=toggle, layout=item_layout),
widgets.interactive(self._set_checked, checked=check, layout=item_layout),
],
layout=box_layout)
display(buttons)
def _set_foobar(self, foobar):
self._foo = foobar == 'Foo'
def _set_checked(self, checked):
self._checked = bool(checked)
如果我随后打开一个 Jupyter notebook 并执行以下操作:
import dashboard
y = dashboard.Dashboard()
它产生:
如果我不让小部件具有交互性,即 children=[toggle, check]
它会完美运行。
有什么方法可以调整交互式小部件之间的间距?
最佳答案
在 Jupyter 中,您可以向各种小部件添加类。如果您有一个按钮小部件,请使用 add_class()
函数为其添加一个类名。然后,使用 display()
函数添加一些 CSS。
import ipywidgets as widgets
from IPython.core.display import display, HTML
but_1 = widgets.Button(
description = 'Button'
)
but_1.add_class("left-spacing-class")
display(HTML(
"<style>.left-spacing-class {margin-left: 10px;}</style>"
))
display(but_1)
关于python - 调整两个 Jupyter 小部件之间的间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42101586/