python - 调整两个 Jupyter 小部件之间的间距

标签 python css jupyter-notebook jupyter ipywidgets

我正在尝试创建一个 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()

它产生:

enter image description here

如果我不让小部件具有交互性,即 children=[toggle, check] 它会完美运行。

enter image description here

有什么方法可以调整交互式小部件之间的间距?

最佳答案

在 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/

相关文章:

python - 我如何将 numpy 数组转换为 pandas 数据框

python - Azure Databricks 中的多重处理

css - 在 CSS 中指定相对于父 DIV 的精确百分比宽度

python - 尽管安装了 jupyter 笔记本,但 Mac 上仍无法运行

python - 如何在jupyter中添加conda环境

python - 安装Jupyter Notebook时出现错误 "' jupyter'未被识别为内部或外部命令、可操作程序或批处理文件”

python - 从Python中的文件读取,并进行分割

python - 提取 2 个特定标签之间的行

html - 在网页上列出带有文字的图像

javascript - 如何在单击按钮上向此 JavaScript 或 HTML 添加 CSS?