Skip to content

Commit

Permalink
Add support for passing objects reference to FlexBox (#6387)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Feb 25, 2024
1 parent 6ee12f6 commit d6c6814
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
15 changes: 14 additions & 1 deletion panel/layout/flex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import param

from param.parameterized import iscoroutinefunction, resolve_ref

from ..reactive import ReactiveHTML
from .base import ListLike

Expand Down Expand Up @@ -60,13 +62,24 @@ class FlexBox(ListLike, ReactiveHTML):
_template = (Path(__file__).parent / 'flexbox.html').read_text('utf-8')

def __init__(self, *objects, **params):
from ..pane.base import panel
if 'sizing_mode' not in params:
direction = params.get('flex_direction', self.flex_direction)
if direction.startswith('row'):
params['sizing_mode'] = 'stretch_width'
else:
params['sizing_mode'] = 'stretch_height'
super().__init__(objects=list(objects), **params)
if objects:
if 'objects' in params:
raise ValueError("A %s's objects should be supplied either "
"as positional arguments or as a keyword, "
"not both." % type(self).__name__)
params['objects'] = [panel(pane) for pane in objects]
elif 'objects' in params:
objects = params['objects']
if not resolve_ref(objects) or iscoroutinefunction(objects):
params['objects'] = [panel(pane) for pane in objects]
super().__init__(**params)

def select(self, selector=None):
"""
Expand Down
8 changes: 4 additions & 4 deletions panel/tests/layout/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from panel.chat import ChatInterface
from panel.layout import (
Accordion, Card, Column, Row, Spacer, Tabs, WidgetBox,
Accordion, Card, Column, FlexBox, Row, Spacer, Tabs, WidgetBox,
)
from panel.layout.base import ListPanel, NamedListPanel
from panel.pane import Bokeh, Markdown
Expand Down Expand Up @@ -569,7 +569,6 @@ def test_no_expand_fixed(panel, document, comm):

assert model.sizing_mode == 'fixed'


@pytest.mark.parametrize('scroll_param', ["auto_scroll_limit", "scroll", "scroll_button_threshold", "view_latest"])
def test_column_scroll_params_sets_scroll(scroll_param, document, comm):
if scroll_param not in ["auto_scroll_limit", "scroll_button_threshold"]:
Expand All @@ -580,9 +579,10 @@ def test_column_scroll_params_sets_scroll(scroll_param, document, comm):
assert getattr(col, scroll_param)
assert col.scroll

def test_pass_objects_ref(document, comm):
@pytest.mark.parametrize('layout', [Row, Column, FlexBox])
def test_pass_objects_ref(document, comm, layout):
multi_select = MultiSelect(options=['foo', 'bar', 'baz'], value=['bar', 'baz'])
col = Column(objects=multi_select)
col = layout(objects=multi_select)
col.get_root(document, comm=comm)

assert len(col.objects) == 2
Expand Down

0 comments on commit d6c6814

Please sign in to comment.