Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alight and place ipywidgets

Is there a way to control the placement and alignment of ipywidgets (inside jupyter notebook)?

from ipywidgets import widgets
from IPython.display import Javascript, display
event_type_ui = widgets.Text(value='open', description='Test Event')
platform_ui = widgets.Text(value='Android', description='Control Event')
display(event_type_ui)
display(platform_ui)

enter image description here

I would like to specify an offset (in pixels?) to allow the label to fit and to have two controls aligned vertically.

like image 475
volodymyr Avatar asked May 03 '16 20:05

volodymyr


2 Answers

After some fiddling, I was able to get this:

Before:enter image description here

After: enter image description here

Here is a copy-paste snippet, if interested:

from ipywidgets import widgets
from IPython.display import Javascript, display

align_kw = dict(
    _css = (('.widget-label', 'min-width', '20ex'),),
    margin = '0px 0px 5px 12px'
)
platform_ui = widgets.Dropdown(description = 'platform',options=['iPhone','iPad','Android'], **align_kw)
event_type_ui = widgets.Text(value='open', description='Test Event', **align_kw)
os_version_ui = widgets.Text(value='iOS9', description='Operating System', **align_kw)
refresh_ui = widgets.Checkbox(description='Force Refresh', **align_kw)

display(platform_ui,event_type_ui,os_version_ui,refresh_ui)
like image 167
volodymyr Avatar answered Oct 19 '22 02:10

volodymyr


The layout and styling documentation for ipywidgets says:

Every Jupyter interactive widget has a layout attribute exposing a number of css properties that impact how widgets are laid out.

I was able to coax it into aligning the labels:

from ipywidgets import Text, HBox, VBox, Box from IPython.display import display widget1 = Text(value='Cats', description='Test Event', layout='margin-right:5px') widget2 = Text(value='Oranges', description='Another Test Event') widget1.width = '200px' widget2.width = '150px' display(VBox([widget1, widget2]))

to produced this:

ipywidgets output

But in general, I doesn't seem like we can directly target layout properties of the description label, just the entire widget itself.

like image 37
Brian Avatar answered Oct 19 '22 03:10

Brian