Skip to content

Commit

Permalink
Do not sync StaticText.value with the frontend (#7038)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Jul 29, 2024
1 parent a6f1503 commit 310309e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
26 changes: 25 additions & 1 deletion panel/tests/widgets/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ def test_literal_input(document, comm):
with pytest.raises(ValueError):
literal.value = []


def test_static_text(document, comm):

text = StaticText(value='ABC', name='Text:')
Expand All @@ -244,6 +243,31 @@ def test_static_text(document, comm):
text.value = '<b>Text:</b>: ABC'
assert widget.text == '<b>Text:</b>: ABC'

def test_static_text_no_sync(document, comm):
text = StaticText(value='ABC', name='Text:')

widget = text.get_root(document, comm=comm)

widget.text = 'CBA'
assert text.value == 'ABC'

def test_static_text_empty(document, comm):

text = StaticText(name='Text:')

widget = text.get_root(document, comm=comm)

assert widget.text == '<b>Text:</b>: '

def test_static_text_repr(document, comm):

text = StaticText(value=StaticText, name='Text:')

widget = text.get_root(document, comm=comm)

assert widget.text == '<b>Text:</b>: &lt;class &#x27;panel.widgets.input.StaticText&#x27;&gt;'



def test_text_input(document, comm):

Expand Down
20 changes: 17 additions & 3 deletions panel/widgets/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
DatetimePicker as _bkDatetimePicker, TextAreaInput as _bkTextAreaInput,
TextInput as _BkTextInput,
)
from ..util import lazy_load, param_reprs, try_datetime64_to_datetime
from ..util import (
escape, lazy_load, param_reprs, try_datetime64_to_datetime,
)
from .base import CompositeWidget, Widget

if TYPE_CHECKING:
Expand Down Expand Up @@ -429,7 +431,7 @@ class StaticText(Widget):
"""

value = param.Parameter(default=None, doc="""
The current value""")
The current value to be displayed.""")

_format: ClassVar[str] = '<b>{title}</b>: {value}'

Expand All @@ -445,10 +447,22 @@ class StaticText(Widget):

_widget_type: ClassVar[type[Model]] = _BkDiv

@property
def _linked_properties(self) -> tuple[str]:
return ()

def _init_params(self) -> dict[str, Any]:
return {
k: v for k, v in self.param.values().items()
if k in self._synced_params and (v is not None or k == 'value')
}

def _process_param_change(self, msg):
msg = super()._process_param_change(msg)
if 'text' in msg:
text = str(msg.pop('text'))
text = msg.pop('text')
if not isinstance(text, str):
text = escape("" if text is None else str(text))
partial = self._format.replace('{value}', '').format(title=self.name)
if self.name:
text = self._format.format(title=self.name, value=text.replace(partial, ''))
Expand Down

0 comments on commit 310309e

Please sign in to comment.