o
    Zh                     @  s   U d Z ddlmZ ddlmZ ddlmZmZmZm	Z	 ddl
mZ ddlmZmZ er1ddlmZ eeZded	< G d
d deZG dd deZdddZdaded< dddZdS )z"Runtime-related utility functions.    )annotations)	getLogger)TYPE_CHECKINGAnyFinalcast)config)MarkdownFormattedExceptionStreamlitAPIException)
ForwardMsgr   _LOGGERc                      s,   e Zd ZdZd fddZdd	d
Z  ZS )MessageSizeErrorzNException raised when a websocket message is larger than the configured limit.failed_msg_strr   returnNonec                   s   |  |}t | d S )N)_get_messagesuper__init__)selfr   msg	__class__ _/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/runtime/runtime_util.pyr   "   s   
zMessageSizeError.__init__strc                 C  s(   dt |d ddt d  ddS )Nz
**Data of size     .Az.1fz& MB exceeds the message size limit of
a   MB.**

This is often caused by a large chart or dataframe. Please decrease the amount of data sent
to the browser, or increase the limit by setting the config option `server.maxMessageSize`.
[Click here to learn more about config options](https://docs.streamlit.io/develop/api-reference/configuration/config.toml).

_Note that increasing the limit may lead to long loading times and large memory consumption
of the client's browser and the Streamlit server._

)lenget_max_message_size_bytesstrip)r   r   r   r   r   r   &   s   
zMessageSizeError._get_message)r   r   r   r   )r   r   r   r   )__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r      s    r   c                   @  s   e Zd ZdZd	ddZdS )
BadDurationStringErrorz5Raised when a bad duration argument string is passed.durationr   r   r   c                 C  s   t | d|  d S )NzcTTL string doesn't look right. It should be formatted as`'1d2h34m'` or `2 days`, for example. Got: )r	   r   )r   r&   r   r   r   r   :   s   zBadDurationStringError.__init__N)r&   r   r   r   )r    r!   r"   r#   r   r   r   r   r   r%   7   s    r%   r   r   r   bytesc                 C  s\   |   }t|t kr,ddlm} t|}td| || j	j
j| d| j_|   }|S )zSerialize a ForwardMsg to send to a client.

    If the message is too large, it will be converted to an exception message
    instead.
    r   )	exceptionzDWebsocket message size limit exceeded. Showing error to the user: %sF)SerializeToStringr   r   streamlit.elementsr(   r   r   warningmarshalldeltanew_elementmetadata	cacheable)r   msg_strr(   msg_size_errorr   r   r   serialize_forward_msgB   s   r3   Nz
int | None_max_message_size_bytesintc                   C  s$   t du rtdtd a tdt S )zReturns the max websocket message size in bytes.

    This will lazyload the value from the config and store it in the global symbol table.
    Nzserver.maxMessageSizer   r5   )r4   r   
get_optionr5   r   r   r   r   r   r   b   s   
r   )r   r   r   r'   )r   r5   )r#   
__future__r   loggingr   typingr   r   r   r   	streamlitr   streamlit.errorsr	   r
   streamlit.proto.ForwardMsg_pb2r   r    r   __annotations__r   r%   r3   r4   r   r   r   r   r   <module>   s   
