Esempio n. 1
0
    def test_session_state_stats(self):
        # TODO: document the values used here. They're somewhat arbitrary -
        #  we don't care about actual byte values, but rather that our
        #  SessionState isn't getting unexpectedly massive.
        state = get_session_state()
        stat = state.get_stats()[0]
        assert stat.category_name == "st_session_state"

        init_size = stat.byte_length
        assert init_size < 1500

        state["foo"] = 2
        new_size = state.get_stats()[0].byte_length
        assert new_size > init_size
        assert new_size < 1500

        state["foo"] = 1
        new_size_2 = state.get_stats()[0].byte_length
        assert new_size_2 == new_size

        st.checkbox("checkbox", key="checkbox")
        new_size_3 = state.get_stats()[0].byte_length
        assert new_size_3 > new_size_2
        assert new_size_3 - new_size_2 < 1500

        state.compact_state()
        new_size_4 = state.get_stats()[0].byte_length
        assert new_size_4 <= new_size_3
Esempio n. 2
0
    def test_callbacks_with_experimental_rerun(self, patched_warning):
        """Calling 'experimental_rerun' from within a widget callback
        is disallowed and results in a warning.
        """

        # A mock on_changed handler for our checkbox. It will call
        # `st.experimental_rerun`, which should result in a warning
        # being printed to the user's app.
        mock_on_checkbox_changed = MagicMock(side_effect=st.experimental_rerun)

        st.checkbox("the checkbox", on_change=mock_on_checkbox_changed)

        session_state = get_session_state()

        # Pretend that the checkbox has a new state value
        checkbox_state = WidgetStateProto()
        checkbox_state.id = list(session_state._new_widget_state.keys())[0]
        checkbox_state.bool_value = True
        widget_states = WidgetStatesProto()
        widget_states.widgets.append(checkbox_state)

        # Tell session_state to call our callbacks.
        session_state.on_script_will_rerun(widget_states)

        mock_on_checkbox_changed.assert_called_once()
        patched_warning.assert_called_once()
Esempio n. 3
0
def check_roundtrip(widget_id: str, value: Any) -> None:
    session_state = get_session_state()
    wid = session_state._get_widget_id(widget_id)
    metadata = session_state._new_widget_state.widget_metadata[wid]
    serializer = metadata.serializer
    deserializer = metadata.deserializer

    assert deserializer(serializer(value), "") == value
Esempio n. 4
0
    def test_callbacks_with_experimental_rerun(self, patched_warning):
        def on_change():
            st.experimental_rerun()

        st.checkbox("the checkbox", on_change=on_change)

        session_state = get_session_state()
        widget_ids = list(session_state._new_widget_state.keys())
        wid = widget_ids[0]
        session_state._new_widget_state.set_from_value(wid, True)

        session_state.call_callbacks()
        patched_warning.assert_called_once()