更改IPython笔记本小部件中标签的大小 [英] Change the size of the label in an IPython notebook widget

查看:97
本文介绍了更改IPython笔记本小部件中标签的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真是一个微不足道的问题,但它仍然很烦人。我正在编写一个工具,允许用户在IPython笔记本中为分析设置一组数字参数。我在 ContainerWidget 中将它设置为一堆 FloatTextWidget 。它们有相当长的标签,如姿势点数或背景磁盘半径。他们没有很好地排队。这是因为长度:如此页面,窗口小部件的标签具有固定的最小宽度。标签的文本始终右对齐,窗口小部件左对齐...如果标签长于最小宽度,小部件向右移动。

This is really a trivial problem, but it is still annoying. I'm writing a tool to allow a user to set a bunch of numerical parameters for an analysis in the IPython notebook. I've set it up as a bunch of FloatTextWidgets in a ContainerWidget. They have rather long labels like "Number of Posture Points" or "Background Disk Radius". They don't line up nicely. This is because of the length: as explained on this page, "The label of the widget has a fixed minimum width. The text of the label is always right aligned and the widget is left aligned... If a label is longer than the minimum width, the widget is shifted to the right."

我的标签超过固定的最小宽度。我想知道的是如何改变它。我在Widget源代码中搜索过,我无法在任何地方找到它。

My labels exceed the "fixed minimum width". What I want to know is how to change it. I have poked around in the Widget source code, and I can't find this anywhere.

编辑:为了回应@Jakob,这里有一些代码,这里是截图

In response to @Jakob, here is some code, and here is a screenshot

在此示例中,阈值:足够小以适应标签宽度,但所有其他都太长。

In this example, "Threshold:" is small enough to fit within the label width, but all the others are too long.

推荐答案

好吧,我找到了我的问题的简答:最小字段宽度在网站中定义-packages / IPython / html / static / style / ipython.min.css (位于你的python库所在的地方 - 在我的Max上是 /Library/Python/2.7/ ),其中 widget-hlabel

Well, I found the narrow answer to my question: the minimum field width is defined in site-packages/IPython/html/static/style/ipython.min.css (located wherever your python libraries live -- on my Max that is /Library/Python/2.7/), where widget-hlabel is defined by

.widget-hlabel{min-width:10ex;padding-right:8px;padding-top:3px;text-align:right;vertical-align:text-top}

min-width:10ex 是相关部分。

虽然可以覆盖整个文档的这个,但我没有看到一次只更改一个小部件的简单方法。它必须在JavaScript方面完成,因为 FloatTextWidget 类不能从python端单独访问Widget的标签组件。这需要开发一个从 FloatTextWidget 子类化的自定义窗口小部件,这对于这样一个简单的问题来说似乎太费力了,而且很难引导。至少,这是我看到的唯一方式 - 更正欢迎。

Although one can override this for an entire document, I don't see an easy way to change it one widget at a time. It would have to be done on the JavaScript side, since the FloatTextWidget class doesn't give separate access to the label component of the Widget from the python side. That would require developing a custom widget subclassed from FloatTextWidget, which seems like way too much effort for such a simple problem, and fragile to boot. At least, that's the only way I see to do it -- corrections welcome.

相反,我决定完全避免使用描述自动标记小部件,而是将每个标签构造为 HTMLWidget ,这让我可以完全控制它的外观。这是这样的:

Instead, I have decided to eschew altogether the automatic labeling of widgets with their descriptions, and instead construct each label as an HTMLWidget, which gives me complete control over its appearance. Here's what that looks like:

这篇关于更改IPython笔记本小部件中标签的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆