示例#1
0
    def test_session_state_stats(self):
        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 < 500

        state.compact_state()
        new_size_4 = state.get_stats()[0].byte_length
        assert new_size_4 <= new_size_3
示例#2
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
示例#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
示例#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()
示例#5
0
def check_session_state_rules(default_value: Any,
                              key: Optional[str],
                              writes_allowed: bool = True) -> None:
    global _shown_default_value_warning

    if key is None or not streamlit._is_running_with_streamlit:
        return

    session_state = get_session_state()
    if not session_state.is_new_state_value(key):
        return

    if not writes_allowed:
        raise StreamlitAPIException(
            "Values for st.button, st.download_button, st.file_uploader, and "
            "st.form cannot be set using st.session_state.")

    if default_value is not None and not _shown_default_value_warning:
        streamlit.warning(
            f'The widget with key "{key}" was created with a default value but'
            " also had its value set via the Session State API.")
        _shown_default_value_warning = True
示例#6
0
    def test_file_uploader_serde(self, get_file_recs_patch):
        file_recs = [
            UploadedFileRec(1, "file1", "type", b"123"),
        ]
        get_file_recs_patch.return_value = file_recs

        uploaded_file = st.file_uploader("file_uploader", key="file_uploader")

        # We can't use check_roundtrip here as the return_value of a
        # file_uploader widget isn't a primitive value, so comparing them
        # using == checks for reference equality.
        session_state = get_session_state()
        metadata = session_state.get_metadata_by_key("file_uploader")
        serializer = metadata.serializer
        deserializer = metadata.deserializer

        file_after_serde = deserializer(serializer(uploaded_file), "")

        assert uploaded_file.id == file_after_serde.id
        assert uploaded_file.name == file_after_serde.name
        assert uploaded_file.type == file_after_serde.type
        assert uploaded_file.size == file_after_serde.size
        assert uploaded_file.read() == file_after_serde.read()