From 03439580a6ab097eeb0090612f65933565562c61 Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Wed, 24 Jan 2024 18:07:39 +0100 Subject: [PATCH] Handle margin=None in layout sizing mode computation (#6267) --- panel/layout/base.py | 2 ++ panel/tests/layout/test_base.py | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/panel/layout/base.py b/panel/layout/base.py index 3a970661ce..6211d33d8d 100644 --- a/panel/layout/base.py +++ b/panel/layout/base.py @@ -210,6 +210,8 @@ def _compute_sizing_mode(self, children, props): ensure sufficient space is available. """ margin = props.get('margin', self.margin) + if margin is None: + margin = 0 sizing_mode = props.get('sizing_mode', self.sizing_mode) if sizing_mode == 'fixed': return {} diff --git a/panel/tests/layout/test_base.py b/panel/tests/layout/test_base.py index 7cc09bbbb6..455ab9c301 100644 --- a/panel/tests/layout/test_base.py +++ b/panel/tests/layout/test_base.py @@ -598,3 +598,50 @@ def test_pass_objects_ref(document, comm): md3 = col.objects[0] assert isinstance(md3, Markdown) assert md3.object == 'foo' + +@pytest.mark.parametrize('dim', ["width", "height"]) +def test_compute_sizing_mode_stretch_margin_none(dim, document, comm): + md = Markdown(**{dim: 100}) + col = Column(md, margin=None, sizing_mode=f'stretch_{dim}') + + root = col.get_root(document, comm=comm) + + new_props = col._compute_sizing_mode(root.children, {'margin': None}) + + assert new_props == {f'min_{dim}': 100, 'sizing_mode': f'stretch_{dim}'} + +@pytest.mark.parametrize('dim', ["width", "height"]) +def test_compute_sizing_mode_stretch_margin_int(dim, document, comm): + margin = 10 + md = Markdown(**{dim: 100}) + col = Column(md, margin=margin, sizing_mode=f'stretch_{dim}') + + root = col.get_root(document, comm=comm) + + new_props = col._compute_sizing_mode(root.children, {'margin': margin}) + + assert new_props == {f'min_{dim}': 120, 'sizing_mode': f'stretch_{dim}'} + +@pytest.mark.parametrize('dim', ["width", "height"]) +def test_compute_sizing_mode_stretch_margin_two_tuple(dim, document, comm): + margin = (0, 10) if dim == 'width' else (10, 0) + md = Markdown(**{dim: 100}) + col = Column(md, margin=margin, sizing_mode=f'stretch_{dim}') + + root = col.get_root(document, comm=comm) + + new_props = col._compute_sizing_mode(root.children, {'margin': margin}) + + assert new_props == {f'min_{dim}': 120, 'sizing_mode': f'stretch_{dim}'} + +@pytest.mark.parametrize('dim', ["width", "height"]) +def test_compute_sizing_mode_stretch_margin_four_tuple(dim, document, comm): + margin = (0, 10, 0, 5) if dim == 'width' else (10, 0, 5, 0) + md = Markdown(**{dim: 100}) + col = Column(md, margin=margin, sizing_mode=f'stretch_{dim}') + + root = col.get_root(document, comm=comm) + + new_props = col._compute_sizing_mode(root.children, {'margin': margin}) + + assert new_props == {f'min_{dim}': 115, 'sizing_mode': f'stretch_{dim}'}