Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure embedded slider initializes with correct default #4121

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions panel/tests/io/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ def link(target, event):
assert event2['model'] == model.children[1].ref
assert event2['new'] == '<pre>%s</pre>' % states[k]

def test_embed_float_slider_default_value(document, comm):
slider = FloatSlider(start=0, end=7.2, value=3.6)
string = Str()
def link(target, event):
target.object = event.new
slider.link(string, callbacks={'value': link})
panel = Row(slider, string)
with config.set(embed=True):
model = panel.get_root(document, comm)
embed_state(panel, model, document)
layout, state = document.roots
assert set(state.state) == {0, 1, 2}
assert layout.children[0].children[1].value == 1

def test_embed_select_explicit_values(document, comm):
select = Select(options=['A', 'B', 'C'])
Expand Down
4 changes: 3 additions & 1 deletion panel/widgets/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ def _get_embed_state(self, root, values=None, max_opts=3):
# Replace model
layout_opts = {k: v for k, v in self.param.values().items()
if k in Layoutable.param and k != 'name'}
dw = DiscreteSlider(options=values, name=self.name, **layout_opts)

value = values[np.argmin(np.abs(np.array(values)-self.value))]
dw = DiscreteSlider(options=values, value=value, name=self.name, **layout_opts)
dw.link(self, value='value')
self._models.pop(ref)
index = parent.children.index(w_model)
Expand Down