o
    ha                     @  s   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZ ed	d#ddZedd$ddZd%ddZd&ddZd'd!d"ZdS )(    )annotationsN)Any)StreamlitAPIException)
ForwardMsg)gather_metrics)get_script_run_ctx)EMBED_OPTIONS_QUERY_PARAMEMBED_QUERY_PARAMEMBED_QUERY_PARAMS_KEYSexperimental_get_query_paramsreturndict[str, list[str]]c                  C  s2   t  } | du r	i S |   ttj| jddtdS )as  Return the query parameters that is currently showing in the browser's URL bar.

    Returns
    -------
    dict
      The current query parameters as a dict. "Query parameters" are the part of the URL that comes
      after the first "?".

    Example
    -------
    Let's say the user's web browser is at
    `http://localhost:8501/?show_map=True&selected=asia&selected=america`.
    Then, you can get the query parameters using the following:

    >>> import streamlit as st
    >>>
    >>> st.experimental_get_query_params()
    {"show_map": ["True"], "selected": ["asia", "america"]}

    Note that the values in the returned dict are *always* lists. This is
    because we internally use Python's urllib.parse.parse_qs(), which behaves
    this way. And this behavior makes sense when you consider that every item
    in a query string is potentially a 1-element array.

    NTkeep_blank_valueskeys_to_exclude)r   #mark_experimental_query_params_used_exclude_keys_in_dictparseparse_qsquery_stringr
   )ctx r   `/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/commands/experimental_query_params.pyget_query_params   s   r   experimental_set_query_paramsquery_paramsr   Nonec                  K  sH   t  }|du r	dS |  t }t| |j|j_|jj|_|| dS )ad  Set the query parameters that are shown in the browser's URL bar.

    .. warning::
        Query param `embed` cannot be set using this method.

    Parameters
    ----------
    **query_params : dict
        The query parameters to set, as key-value pairs.

    Example
    -------

    To point the user's web browser to something like
    "http://localhost:8501/?show_map=True&selected=asia&selected=america",
    you would do the following:

    >>> import streamlit as st
    >>>
    >>> st.experimental_set_query_params(
    ...     show_map=True,
    ...     selected=["asia", "america"],
    ... )

    N)r   r   r   _ensure_no_embed_paramsr   page_info_changedenqueue)r   r   msgr   r   r   set_query_paramsE   s   
r"   ddict[str, Any]r   	list[str]c                   s    fdd|   D S )z?Returns new object but without keys defined in keys_to_exclude.c                   s"   i | ]\}}|   vr||qS r   lower).0keyvaluer   r   r   
<dictcomp>q   s    z)_exclude_keys_in_dict.<locals>.<dictcomp>)items)r#   r   r   r   r   r   m   s   
r   	param_keystrset[str]c                   s"   dd  fdd  D D S )zWExtracts key (case-insensitive) query params from Dict, and returns them as Set of str.c                 S  s   h | ]}|D ]}|  qqS r   r&   )r(   sublistitemr   r   r   	<setcomp>z   s    z,_extract_key_query_params.<locals>.<setcomp>c                   s4   g | ]}|   kr|rd d | D qS )c                 S  s   g | ]}|  qS r   r&   )r(   r*   r   r   r   
<listcomp>}   s    z8_extract_key_query_params.<locals>.<listcomp>.<listcomp>)r'   get)r(   r)   r-   r   r   r   r3   |   s
    z-_extract_key_query_params.<locals>.<listcomp>)keys)r   r-   r   r5   r   _extract_key_query_paramsv   s
   r7   dict[str, list[str] | str]r   c              	   C  s   t | td}| |krtdtj|dd}tjttt|tdt	tt|t	didd}tj| dd}|rB|r9dnd}|
||gS |S )	zEnsures there are no embed params set (raises StreamlitAPIException) if there is a try,
    also makes sure old param values in query_string are preserved. Returns query_string : str.
    r   zcQuery param embed and embed_options (case-insensitive) cannot be set using set_query_params method.Tr   )r-   )doseq& )r   r
   r   r   r   	urlencoder	   listr7   r   join)r   r   query_params_without_embedall_current_paramscurrent_embed_params	separatorr   r   r   r      s6   r   )r   r   )r   r   r   r   )r#   r$   r   r%   r   r$   )r   r   r-   r.   r   r/   )r   r8   r   r.   r   r.   )
__future__r   urllib.parser   typingr   streamlit.errorsr   streamlit.proto.ForwardMsg_pb2r   streamlit.runtime.metrics_utilr   7streamlit.runtime.scriptrunner_utils.script_run_contextr   $streamlit.runtime.state.query_paramsr   r	   r
   r   r"   r   r7   r   r   r   r   r   <module>   s   %
'
	