Skip to content

Commit

Permalink
Add disabled to select_slider + tests + snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
mayagbarnes committed Jan 25, 2022
1 parent 2c153aa commit 696d3d7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
14 changes: 11 additions & 3 deletions e2e/scripts/st_select_slider.py
Expand Up @@ -49,17 +49,25 @@
)
st.write("Value 4:", w4)

w5 = st.select_slider(
"Label 5",
value=("orange", "blue"),
options=["red", "orange", "yellow", "green", "blue", "indigo", "violet"],
disabled=True,
)
st.write("Value 5:", w5)


if st._is_running_with_streamlit:

def on_change():
st.session_state.select_slider_changed = True

st.select_slider(
"Label 5",
"Label 6",
options=np.array([1, 2, 3, 4, 5]),
key="select_slider5",
key="select_slider6",
on_change=on_change,
)
st.write("Value 5:", st.session_state.select_slider5)
st.write("Value 6:", st.session_state.select_slider6)
st.write("Select slider changed:", "select_slider_changed" in st.session_state)
25 changes: 19 additions & 6 deletions e2e/specs/st_select_slider.spec.js
Expand Up @@ -21,7 +21,13 @@ describe("st.select_slider", () => {
});

it("displays correct number of elements", () => {
cy.get(".element-container .stSlider").should("have.length", 5);
cy.get(".element-container .stSlider").should("have.length", 6);
});

it("looks right when disabled", () => {
cy.getIndexed(".stSlider", 4).matchThemedSnapshots(
"disabled-select-slider"
);
});

it("shows labels", () => {
Expand All @@ -34,6 +40,8 @@ describe("st.select_slider", () => {
cy.getIndexed(".stSlider label", 3).should("have.text", "Label 4");

cy.getIndexed(".stSlider label", 4).should("have.text", "Label 5");

cy.getIndexed(".stSlider label", 5).should("have.text", "Label 6");
});

it("has correct values", () => {
Expand All @@ -48,9 +56,14 @@ describe("st.select_slider", () => {

cy.getIndexed(".stMarkdown", 3).should("have.text", "Value 4: 5");

cy.getIndexed(".stMarkdown", 4).should("have.text", "Value 5: 1");
cy.getIndexed(".stMarkdown", 4).should(
"have.text",
"Value 5: ('orange', 'blue')"
);

cy.getIndexed(".stMarkdown", 5).should("have.text", "Value 6: 1");

cy.getIndexed(".stMarkdown", 5).should(
cy.getIndexed(".stMarkdown", 6).should(
"have.text",
"Select slider changed: False"
);
Expand Down Expand Up @@ -135,14 +148,14 @@ describe("st.select_slider", () => {

it("calls callback if one is registered", () => {
// This selects the slider ends, so range sliders have two, and this is the
// seventh element in the list.
cy.getIndexed('.stSlider [role="slider"]', 6)
// ninth element in the list.
cy.getIndexed('.stSlider [role="slider"]', 8)
.click()
.type("{rightarrow}", { force: true });

cy.get(".stMarkdown").should(
"contain.text",
"Value 5: 2" + "Select slider changed: True"
"Value 6: 2" + "Select slider changed: True"
);
});
});
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions lib/streamlit/elements/select_slider.py
Expand Up @@ -43,6 +43,8 @@ def select_slider(
on_change: Optional[WidgetCallback] = None,
args: Optional[WidgetArgs] = None,
kwargs: Optional[WidgetKwargs] = None,
*, # keyword-only arguments:
disabled: bool = False,
) -> Any:
"""
Display a slider widget to select items from a list.
Expand Down Expand Up @@ -86,6 +88,9 @@ def select_slider(
An optional tuple of args to pass to the callback.
kwargs : dict
An optional dict of kwargs to pass to the callback.
disabled : bool
An optional boolean, which disables the select slider if set to True.
The default is False. This argument can only be supplied by keyword.
Returns
-------
Expand Down Expand Up @@ -119,6 +124,7 @@ def select_slider(
on_change=on_change,
args=args,
kwargs=kwargs,
disabled=disabled,
ctx=ctx,
)

Expand All @@ -133,6 +139,7 @@ def _select_slider(
on_change: Optional[WidgetCallback] = None,
args: Optional[WidgetArgs] = None,
kwargs: Optional[WidgetKwargs] = None,
disabled: bool = False,
ctx: Optional[ScriptRunContext] = None,
) -> Any:
key = to_key(key)
Expand Down Expand Up @@ -177,6 +184,7 @@ def as_index_list(v):
slider_proto.data_type = SliderProto.INT
slider_proto.options[:] = [str(format_func(option)) for option in opt]
slider_proto.form_id = current_form_id(self.dg)
slider_proto.disabled = disabled
if help is not None:
slider_proto.help = dedent(help)

Expand Down
9 changes: 9 additions & 0 deletions lib/tests/streamlit/select_slider_test.py
Expand Up @@ -40,6 +40,15 @@ def test_no_value(self):
self.assertEqual(c.max, 2)
self.assertEqual(c.step, 1)

def test_just_disabled(self):
"""Test that it can be called with disabled param."""
st.select_slider(
"the label", options=["red", "orange", "yellow"], disabled=True
)

c = self.get_delta_from_queue().new_element.slider
self.assertEqual(c.disabled, True)

@parameterized.expand(
[
(5, [1, 2, 3, 4, 5], [4]), # list
Expand Down

0 comments on commit 696d3d7

Please sign in to comment.