o
    h6                     @  s8  U d dl mZ d dlmZ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 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 ercd dl m!Z!m"Z" e#Z$de%d< ee#ee	g df ef Z&de%d< d.ddZ'd/ddZ(d0dd Z)ed!d"d#d$d1d+d!Z*d1d,d-Z+dS )2    )annotations)MappingSequence)Path)TYPE_CHECKINGCallableLiteralUnion)	TypeAlias)config)StreamlitAPIException)StreamlitPage)
ForwardMsg)
Navigation)gather_metrics)PagesManager)ScriptRunContextget_script_run_ctx)PageHashPageInfor
   SectionHeaderNPageType
page_inputreturnr   c                 C  sV   t | tr| S t | trt| S t | trt| S t| r!t| S tdt|  d)z5Convert various input types to StreamlitPage objects.zInvalid page type: zY. Must be either a string path, a pathlib.Path, a callable function, or a st.Page object.)
isinstancer   strr   callabler   type)r    r   Q/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/commands/navigation.pyconvert_to_streamlit_page*   s   


r    nav_sections(dict[SectionHeader, list[StreamlitPage]]list[StreamlitPage]c                 C  s*   g }|   D ]}|D ]}|| q
q|S )N)valuesappend)r!   	page_listpagespager   r   r   pages_from_nav_sectionsA   s   r)   ctxr   c                 C  s   t  }d|j_| | d S )N )r   page_not_found	page_nameenqueue)r*   msgr   r   r   send_page_not_foundL   s   r0   
navigationsidebarFpositionexpandedr'   ?Sequence[PageType] | Mapping[SectionHeader, Sequence[PageType]]r4   Literal['sidebar', 'hidden']r5   boolc                C  s   dt _t| ||dS )u  
    Configure the available pages in a multipage app.

    Call ``st.navigation`` in your entrypoint file to define the available
    pages for your app. ``st.navigation`` returns the current page, which can
    be executed using ``.run()`` method.

    When using ``st.navigation``, your entrypoint file (the file passed to
    ``streamlit run``) acts like a router or frame of common elements around
    each of your pages. Streamlit executes the entrypoint file with every app
    rerun. To execute the current page, you must call the ``.run()`` method on
    the ``StreamlitPage`` object returned by ``st.navigation``.

    The set of available pages can be updated with each rerun for dynamic
    navigation. By default, ``st.navigation`` displays the available pages in
    the sidebar if there is more than one page. This behavior can be changed
    using the ``position`` keyword argument.

    As soon as any session of your app executes the ``st.navigation`` command,
    your app will ignore the ``pages/`` directory (across all sessions).

    Parameters
    ----------
    pages : Sequence[page-like], Mapping[str, Sequence[page-like]]
        The available pages for the app.

        To create a navigation menu with no sections or page groupings,
        ``pages`` must be a list of page-like objects. Page-like objects are
        anything that can be passed to ``st.Page`` or a ``StreamlitPage``
        object returned by ``st.Page``.

        To create labeled sections or page groupings within the navigation
        menu, ``pages`` must be a dictionary. Each key is the label of a
        section and each value is the list of page-like objects for
        that section.

        When you use a string or path as a page-like object, they are
        internally passed to ``st.Page`` and converted to ``StreamlitPage``
        objects. In this case, the page will have the default title, icon, and
        path inferred from its path or filename. To customize these attributes
        for your page, initialize your page with ``st.Page``.

    position : "sidebar" or "hidden"
        The position of the navigation menu. If this is ``"sidebar"``
        (default), the navigation widget appears at the top of the sidebar. If
        this is ``"hidden"``, the navigation widget is not displayed.

        If there is only one page in ``pages``, the navigation will be hidden
        for any value of ``position``.

    expanded : bool
        Whether the navigation menu should be expanded. If this is ``False``
        (default), the navigation menu will be collapsed and will include a
        button to view more options when there are too many pages to display.
        If this is ``True``, the navigation menu will always be expanded; no
        button to collapse the menu will be displayed.

        If ``st.navigation`` changes from ``expanded=True`` to
        ``expanded=False`` on a rerun, the menu will stay expanded and a
        collapse button will be displayed.

    Returns
    -------
    StreamlitPage
        The current page selected by the user. To run the page, you must use
        the ``.run()`` method on it.

    Examples
    --------
    The following examples show different possible entrypoint files, each named
    ``streamlit_app.py``. An entrypoint file is passed to ``streamlit run``. It
    manages your app's navigation and serves as a router between pages.

    **Example 1: Use a callable or Python file as a page**

    You can declare pages from callables or file paths. If you pass callables
    or paths to ``st.navigation`` as a page-like objects, they are internally
    converted to ``StreamlitPage`` objects using ``st.Page``. In this case, the
    page titles, icons, and paths are inferred from the file or callable names.

    ``page_1.py`` (in the same directory as your entrypoint file):

    >>> import streamlit as st
    >>>
    >>> st.title("Page 1")

    ``streamlit_app.py``:

    >>> import streamlit as st
    >>>
    >>> def page_2():
    ...     st.title("Page 2")
    >>>
    >>> pg = st.navigation(["page_1.py", page_2])
    >>> pg.run()

    .. output::
        https://doc-navigation-example-1.streamlit.app/
        height: 200px

    **Example 2: Group pages into sections and customize them with ``st.Page``**

    You can use a dictionary to create sections within your navigation menu. In
    the following example, each page is similar to Page 1 in Example 1, and all
    pages are in the same directory. However, you can use Python files from
    anywhere in your repository. ``st.Page`` is used to give each page a custom
    title. For more information, see |st.Page|_.

    Directory structure:

    >>> your_repository/
    >>> ├── create_account.py
    >>> ├── learn.py
    >>> ├── manage_account.py
    >>> ├── streamlit_app.py
    >>> └── trial.py

    ``streamlit_app.py``:

    >>> import streamlit as st
    >>>
    >>> pages = {
    ...     "Your account": [
    ...         st.Page("create_account.py", title="Create your account"),
    ...         st.Page("manage_account.py", title="Manage your account"),
    ...     ],
    ...     "Resources": [
    ...         st.Page("learn.py", title="Learn about us"),
    ...         st.Page("trial.py", title="Try it out"),
    ...     ],
    ... }
    >>>
    >>> pg = st.navigation(pages)
    >>> pg.run()

    .. output::
        https://doc-navigation-example-2.streamlit.app/
        height: 300px

    **Example 3: Stateful widgets across multiple pages**

    Call widget functions in your entrypoint file when you want a widget to be
    stateful across pages. Assign keys to your common widgets and access their
    values through Session State within your pages.

    ``streamlit_app.py``:

    >>> import streamlit as st
    >>>
    >>> def page1():
    >>>     st.write(st.session_state.foo)
    >>>
    >>> def page2():
    >>>     st.write(st.session_state.bar)
    >>>
    >>> # Widgets shared by all the pages
    >>> st.sidebar.selectbox("Foo", ["A", "B", "C"], key="foo")
    >>> st.sidebar.checkbox("Bar", key="bar")
    >>>
    >>> pg = st.navigation([page1, page2])
    >>> pg.run()

    .. output::
        https://doc-navigation-multipage-widgets.streamlit.app/
        height: 350px

    .. |st.Page| replace:: ``st.Page``
    .. _st.Page: https://docs.streamlit.io/develop/api-reference/navigation/st.page

    Fr3   )r   uses_pages_directory_navigation)r'   r4   r5   r   r   r   r1   R   s    3c                  s^  t | trdd | D }d|i}n	dd |  D }t|}|s$tdd }i }|D ]}|| D ]}	|	jr?|d ur=td|	}q0q*|d u rL|d }d	|_t }
|
sVd	|_|S |D ]4}|| D ]-}	t |	jt	rlt
|	j}nd}|	j}||v r~td
|	j d||	j|	j||	jd||< q^qXt }|dkrtjj|j_ntddu rtjj|j_ntjj|j_||j_| |jjd d < |D ]&}|| D ]}	|jj }|	j|_|	j|_|	j|_|	j|_||_ |	j|_!qq|
j"#| |
j"j$|jd}d }|r|d   fdd|D }t%|dkr|d }|st&|
 |}d	|_|j|j_|
'|j |
(| |S )Nc                 S     g | ]}t |qS r   r    .0pr   r   r   
<listcomp>      z_navigation.<locals>.<listcomp>r+   c                 S  s    i | ]\}}|d d |D qS )c                 S  r;   r   r<   r=   r   r   r   r@     rA   z*_navigation.<locals>.<dictcomp>.<listcomp>r   )r>   sectionsection_pagesr   r   r   
<dictcomp>  s    z_navigation.<locals>.<dictcomp>z;`st.navigation` must be called with at least one `st.Page`.zUMultiple Pages specified with `default=True`. At most one Page can be set to default.r   Tz+Multiple Pages specified with URL pathname zl. URL pathnames must be unique. The url pathname may be inferred from the filename, callable name, or title.)page_script_hashr-   iconscript_pathurl_pathnamehiddenzclient.showSidebarNavigationF)fallback_page_hashrE   c                   s   g | ]	}|j  kr|qS r   )_script_hashr=   found_page_script_hashr   r   r@   n  s    ))r   r   itemsr)   r   _defaultr   _can_be_called_pager   r   rK   url_pathtitlerF   r   NavigationProtoPositionHIDDENr1   r4   r   
get_optionSIDEBARr5   keyssections	app_pagesaddrE   r-   
is_defaultsection_headerrH   pages_manager	set_pagesget_page_scriptlenr0   set_mpa_v2_pager.   )r'   r4   r5   converted_pagesr!   r&   default_pagepagehash_to_pageinfor^   r(   r*   rG   script_hashr/   r?   
found_pagepage_to_returnmatching_pagesr   rL   r   r:   	  s   

	




r:   )r   r   r   r   )r!   r"   r   r#   )r*   r   )r'   r6   r4   r7   r5   r8   r   r   ),
__future__r   collections.abcr   r   pathlibr   typingr   r   r   r	   typing_extensionsr
   	streamlitr   streamlit.errorsr   streamlit.navigation.pager   streamlit.proto.ForwardMsg_pb2r   streamlit.proto.Navigation_pb2r   rT   streamlit.runtime.metrics_utilr   streamlit.runtime.pages_managerr   7streamlit.runtime.scriptrunner_utils.script_run_contextr   r   streamlit.source_utilr   r   r   r   __annotations__r   r    r)   r0   r1   r:   r   r   r   r   <module>   s4    


 7