widget_box.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. """Box widgets.
  4. These widgets are containers that can be used to
  5. group other widgets together and control their
  6. relative layouts.
  7. """
  8. from .widget import register, widget_serialization, Widget
  9. from .domwidget import DOMWidget
  10. from .widget_core import CoreWidget
  11. from .docutils import doc_subst
  12. from .trait_types import TypedTuple
  13. from traitlets import Unicode, CaselessStrEnum, Instance
  14. _doc_snippets = {}
  15. _doc_snippets['box_params'] = """
  16. children: iterable of Widget instances
  17. list of widgets to display
  18. box_style: str
  19. one of 'success', 'info', 'warning' or 'danger', or ''.
  20. Applies a predefined style to the box. Defaults to '',
  21. which applies no pre-defined style.
  22. """
  23. @register
  24. @doc_subst(_doc_snippets)
  25. class Box(DOMWidget, CoreWidget):
  26. """ Displays multiple widgets in a group.
  27. The widgets are laid out horizontally.
  28. Parameters
  29. ----------
  30. {box_params}
  31. Examples
  32. --------
  33. >>> import ipywidgets as widgets
  34. >>> title_widget = widgets.HTML('<em>Box Example</em>')
  35. >>> slider = widgets.IntSlider()
  36. >>> widgets.Box([title_widget, slider])
  37. """
  38. _model_name = Unicode('BoxModel').tag(sync=True)
  39. _view_name = Unicode('BoxView').tag(sync=True)
  40. # Child widgets in the container.
  41. # Using a tuple here to force reassignment to update the list.
  42. # When a proper notifying-list trait exists, use that instead.
  43. children = TypedTuple(trait=Instance(Widget), help="List of widget children").tag(
  44. sync=True, **widget_serialization)
  45. box_style = CaselessStrEnum(
  46. values=['success', 'info', 'warning', 'danger', ''], default_value='',
  47. help="""Use a predefined styling for the box.""").tag(sync=True)
  48. def __init__(self, children=(), **kwargs):
  49. kwargs['children'] = children
  50. super(Box, self).__init__(**kwargs)
  51. self.on_displayed(Box._fire_children_displayed)
  52. def _fire_children_displayed(self):
  53. for child in self.children:
  54. child._handle_displayed()
  55. @register
  56. @doc_subst(_doc_snippets)
  57. class VBox(Box):
  58. """ Displays multiple widgets vertically using the flexible box model.
  59. Parameters
  60. ----------
  61. {box_params}
  62. Examples
  63. --------
  64. >>> import ipywidgets as widgets
  65. >>> title_widget = widgets.HTML('<em>Vertical Box Example</em>')
  66. >>> slider = widgets.IntSlider()
  67. >>> widgets.VBox([title_widget, slider])
  68. """
  69. _model_name = Unicode('VBoxModel').tag(sync=True)
  70. _view_name = Unicode('VBoxView').tag(sync=True)
  71. @register
  72. @doc_subst(_doc_snippets)
  73. class HBox(Box):
  74. """ Displays multiple widgets horizontally using the flexible box model.
  75. Parameters
  76. ----------
  77. {box_params}
  78. Examples
  79. --------
  80. >>> import ipywidgets as widgets
  81. >>> title_widget = widgets.HTML('<em>Horizontal Box Example</em>')
  82. >>> slider = widgets.IntSlider()
  83. >>> widgets.HBox([title_widget, slider])
  84. """
  85. _model_name = Unicode('HBoxModel').tag(sync=True)
  86. _view_name = Unicode('HBoxView').tag(sync=True)
  87. @register
  88. class GridBox(Box):
  89. """ Displays multiple widgets in rows and columns using the grid box model.
  90. Parameters
  91. ----------
  92. {box_params}
  93. Examples
  94. --------
  95. >>> import ipywidgets as widgets
  96. >>> title_widget = widgets.HTML('<em>Grid Box Example</em>')
  97. >>> slider = widgets.IntSlider()
  98. >>> button1 = widgets.Button(description='1')
  99. >>> button2 = widgets.Button(description='2')
  100. >>> # Create a grid with two columns, splitting space equally
  101. >>> layout = widgets.Layout(grid_template_columns='1fr 1fr')
  102. >>> widgets.GridBox([title_widget, slider, button1, button2], layout=layout)
  103. """
  104. _model_name = Unicode('GridBoxModel').tag(sync=True)
  105. _view_name = Unicode('GridBoxView').tag(sync=True)