def enqueue(self, msg: ForwardMsg) -> None:
        if msg.HasField(
                "page_config_changed") and not self._set_page_config_allowed:
            raise StreamlitAPIException(
                "`set_page_config()` can only be called once per app, " +
                "and must be called as the first Streamlit command in your script.\n\n"
                + "For more information refer to the [docs]" +
                "(https://docs.streamlit.io/en/stable/api.html#streamlit.set_page_config)."
            )

        if msg.HasField("delta") or msg.HasField("page_config_changed"):
            self._set_page_config_allowed = False

        self._enqueue(msg)
    def enqueue(self, msg: ForwardMsg) -> None:
        """Add message into queue, possibly composing it with another message."""
        with self._lock:
            if not msg.HasField("delta"):
                self._queue.append(msg)
                return

            # If there's a Delta message with the same delta_path already in
            # the queue - meaning that it refers to the same location in
            # the app - we attempt to combine this new Delta into the old
            # one. This is an optimization that prevents redundant Deltas
            # from being sent to the frontend.
            delta_key = tuple(msg.metadata.delta_path)
            if delta_key in self._delta_index_map:
                index = self._delta_index_map[delta_key]
                old_msg = self._queue[index]
                composed_delta = _maybe_compose_deltas(old_msg.delta,
                                                       msg.delta)
                if composed_delta is not None:
                    new_msg = ForwardMsg()
                    new_msg.delta.CopyFrom(composed_delta)
                    new_msg.metadata.CopyFrom(msg.metadata)
                    self._queue[index] = new_msg
                    return

            # No composition occured. Append this message to the queue, and
            # store its index for potential future composition.
            self._delta_index_map[delta_key] = len(self._queue)
            self._queue.append(msg)
Beispiel #3
0
    def enqueue(self, msg: ForwardMsg) -> None:
        if msg.HasField(
                "page_config_changed") and not self._set_page_config_allowed:
            raise StreamlitAPIException(
                "`set_page_config()` can only be called once per app, " +
                "and must be called as the first Streamlit command in your script.\n\n"
                + "For more information refer to the [docs]" +
                "(https://docs.streamlit.io/library/api-reference/utilities/st.set_page_config)."
            )

        # We want to disallow set_page config if one of the following occurs:
        # - set_page_config was called on this message
        # - The script has already started and a different st call occurs (a delta)
        if msg.HasField("page_config_changed") or (msg.HasField("delta") and
                                                   self._has_script_started):
            self._set_page_config_allowed = False

        self._enqueue(msg)
def _is_composable_message(msg: ForwardMsg) -> bool:
    """True if the ForwardMsg is potentially composable with other ForwardMsgs."""
    if not msg.HasField("delta"):
        # Non-delta messages are never composable.
        return False

    # We never compose add_rows messages in Python, because the add_rows
    # operation can raise errors, and we don't have a good way of handling
    # those errors in the message queue.
    delta_type = msg.delta.WhichOneof("type")
    return delta_type != "add_rows" and delta_type != "arrow_add_rows"