
    hMh,                     n   d Z ddlZddlZddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZ dZd	Zd
ZdZdZdZdez  Zdez  Zi Zded<   ded<   d Zd!dZd Zd"dZ ee          d#d            Z ej        dej                  Zd Z ee          	 	 	 	 	 	 d$d            Z  ee          d%d             Z!dS )&zB
Functions for generating embeddable HTML/javascript of a widget.
    N   )Widget	DOMWidgetwidget)Link)	doc_subst)__html_manager_version__zg
{load}
<script type="application/vnd.jupyter.widget-state+json">
{json_data}
</script>
{widget_views}
z-<script src="{embed_url}"{use_cors}></script>a2  
<!-- Load require.js. Delete this if your page already loads require.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" crossorigin="anonymous"></script>
<script src="{embed_url}"{use_cors}></script>
z`
<script type="application/vnd.jupyter.widget-state+json">
{json_data}
</script>
{widget_views}
z<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{title}</title>
</head>
<body>
{snippet}
</body>
</html>
zN<script type="application/vnd.jupyter.widget-view+json">
{view_spec}
</script>zKhttps://cdn.jsdelivr.net/npm/@jupyter-widgets/html-manager@%s/dist/embed.jszOhttps://cdn.jsdelivr.net/npm/@jupyter-widgets/html-manager@%s/dist/embed-amd.jsz
    views: widget or collection of widgets or None
        The widgets to include views for. If None, all DOMWidgets are
        included (not just the displayed ones).
views_attributeay  
    drop_defaults: boolean
        Whether to drop default values from the widget states.
    state: dict or None (default)
        The state to include. When set to None, the state of all widgets
        know to the widget manager is included. Otherwise it uses the
        passed state directly. This allows for end users to include a
        smaller state, under the responsibility that this state is
        sufficient to reconstruct the embedded views.
    indent: integer, string or None
        The indent to use for the JSON state dump. See `json.dumps` for
        full description.
    embed_url: string or None
        Allows for overriding the URL used to fetch the widget manager
        for the embedded code. This defaults (None) to a `jsDelivr` CDN url.
    requirejs: boolean (True)
        Enables the requirejs-based embedding, which allows for custom widgets.
        If True, the embed_url should point to an AMD module.
    cors: boolean (True)
        If True avoids sending user credentials while requesting the scripts.
        When opening an HTML file from disk, some browsers may refuse to load
        the scripts.
embed_kwargsc              #     K   t          |                                          }|D ]}t          | |          }t          |t                    r|V  ,t          |t
          t           f          r|D ]}t          |t                    r|V  gt          |t                    r0|                                D ]}t          |t                    r|V  dS )z4Find references to other widgets in a widget's stateN)tuplekeysgetattr
isinstancer   listdictvalues)r   stater   keyvalueitems         P/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/ipywidgets/embed.py_find_widget_refs_by_stater   _   s       D  $$eV$$ 	KKKK e}-- 	  dF++ JJJ t$$ 	  dF++ JJJ     Fc                     |t                      }|                     |          }||| j        <   t          | |d                   D ]}|j        |vrt	          |||           |S )zLGets the embed state of a widget, and all other widgets it refers to as wellNdrop_defaultsr   )r   _get_embed_statemodel_idr   _get_recursive_state)r   storer   r   refs        r   r    r    t   s{    }##-#@@E"E&/ *&%.AA J J<u$$ e=IIIILr   c                    t           j                                        D ]d\  }}t          |t                    rJ|| vrF|j        d         j        | v r2|j        d         j        | v r|                    |          | |j        <   edS )z=Adds the state of any link models between two models in storer   r   N)	widget_module
_instancesitemsr   r   sourcer   targetr   )r!   r   	widget_idr   s       r   add_resolved_linksr*      s    *5;;== ^ ^	6fd## 	^	(>(>}Q(E11fmA6F6OSX6X6X)/)@)@})@)])]fo&^ ^r   Tc                     | t          j        |d          d         }nL	 | d          n# t          t          f$ r | g} Y nw xY wi }| D ]}t	          |||           t          ||           |S )af  Get the state of all widgets specified, and their dependencies.

    This uses a simple dependency finder, including:
     - any widget directly referenced in the state of an included widget
     - any widget in a list/tuple attribute in the state of an included widget
     - any widget in a dict attribute in the state of an included widget
     - any jslink/jsdlink between two included widgets
    What this alogorithm does not do:
     - Find widget references in nested list/dict structures
     - Find widget references in other types of attributes

    Note that this searches the state of the widgets for references, so if
    a widget reference is not included in the serialized state, it won't
    be considered as a dependency.

    Parameters
    ----------
    widgets: single widget or list of widgets.
       This function will return the state of every widget mentioned
       and of all their dependencies.
    drop_defaults: boolean
        Whether to drop default values from the widget states.

    Returns
    -------
    A dictionary with the state of the widgets and any widget they
    depend on.
    Nr   widgetsr   r   )r   get_manager_state
IndexError	TypeErrorr    r*   )r-   r   r   r   s       r   dependency_stater1      s    < (}dSSST[\	 AJJJI& 	  	  	 iGGG	  	? 	?F >>>>5-000Ls   * A Ac                 J   | )d t           j                                        D             } n$	 | d          n# t          t          f$ r | g} Y nw xY w|t          j        |d          d         }t          j        g           }||d<   d | D             }t          ||          S )	a)  Gets data for embedding.

    Use this to get the raw data for embedding if you have special
    formatting needs.

    Parameters
    ----------
    {views_attribute}
    drop_defaults: boolean
        Whether to drop default values from the widget states.
    state: dict or None (default)
        The state to include. When set to None, the state of all widgets
        know to the widget manager is included. Otherwise it uses the
        passed state directly. This allows for end users to include a
        smaller state, under the responsibility that this state is
        sufficient to reconstruct the embedded views.

    Returns
    -------
    A dictionary with the following entries:
        manager_state: dict of the widget manager state data
        view_specs: a list of widget view specs
    Nc                 <    g | ]}t          |t                    |S  )r   r   .0ws     r   
<listcomp>zembed_data.<locals>.<listcomp>   s'    ZZZqAyAYAYZZZZr   r   r,   r   )r-   c                 6    g | ]}|                                 S r4   )get_view_specr5   s     r   r8   zembed_data.<locals>.<listcomp>   s"    333!//##333r   )manager_state
view_specs)r$   r%   r   r/   r0   r   r.   r   )viewsr   r   	json_datar<   s        r   
embed_datar?      s    2 }ZZM4;;==ZZZ	!HHHI& 	 	 	GEEE	 }(}dSSST[\ (444IIg33U333JiJ????s   6 AAz<(script|/script|!--)c                 8    t                               d|           S )a  Escape a string that will be the content of an HTML script tag.

    We replace the opening bracket of <script, </script, and <!-- with the unicode
    equivalent. This is inspired by the documentation for the script tag at
    https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements

    We only replace these three cases so that most html or other content
    involving `<` is readable.
    z	\\u003c\1)script_escape_resub)ss    r   escape_scriptrD      s     a000r      c                 f   t          | ||          }d                    d |d         D                       }||rt          nt          }|rt          nt
          }	|rdnd}
|	                    ||
          t          t          j	        |d	         |
                    |d}t          j        di |S )zReturn a snippet that can be embedded in an HTML file.

    Parameters
    ----------
    {views_attribute}
    {embed_kwargs}

    Returns
    -------
    A unicode string with an HTML snippet containing several `<script>` tags.
    )r   r   
c              3      K   | ]>}t                               t          t          j        |                               V  ?dS ))	view_specN)widget_view_templateformatrD   jsondumps)r6   rI   s     r   	<genexpr>z embed_snippet.<locals>.<genexpr>
  sY         	##mDJy<Q<Q.R.R#SS     r   r<   Nz crossorigin="anonymous" )	embed_urluse_corsr;   )indent)loadr>   widget_viewsr4   )r?   joinDEFAULT_EMBED_REQUIREJS_URLDEFAULT_EMBED_SCRIPT_URLload_requirejs_templateload_templaterK   rD   rL   rM   snippet_template)r=   r   r   rR   rP   	requirejscorsdatarT   rS   rQ   r   s               r   embed_snippetr^      s    * e=FFFD99  l+    L
 3<Z//BZ	&/B""]D-19))rHi(CC"4:d?.CF#S#S#STT$ F ",,V,,,r   IPyWidget exportc                     t          |fi |}||d}|t          } |j        di |}t          | d          r|                     |           dS t          | d          5 }|                    |           ddd           dS # 1 swxY w Y   dS )a  Write a minimal HTML file with widget views embedded.

    Parameters
    ----------
    fp: filename or file-like object
        The file to write the HTML output to.
    {views_attribute}
    title: title of the html page.
    template: Template in which to embed the widget state.
        This should be a Python string with placeholders
        `{{title}}` and `{{snippet}}`. The `{{snippet}}` placeholder
        will be replaced by all the widgets.
    {embed_kwargs}
    )titlesnippetNwriter7   r4   )r^   html_templaterK   hasattrrc   open)	fpr=   ra   templatekwargsrb   r   	html_codefs	            r   embed_minimal_htmlrl     s      E,,V,,G  F  ))&))I r7 
 "c]] 	aGGI	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s    BB
B)NF)T)TN)TNrE   NTT)r_   N)"__doc__rL   rer-   r   r   r   r$   widgets.widget_linkr   widgets.docutilsr   _versionr	   rZ   rY   rX   requirejs_snippet_templaterd   rJ   rW   rV   _doc_snippetsr   r    r*   r1   r?   compile
IGNORECASErA   rD   r^   rl   r4   r   r   <module>rv      s     				 ? ? ? ? ? ? ? ? ? ? % % % % % % ' ' ' ' ' ' . . . . . .  D  
  i  jB  B o  qI  I $  
!n 2  *   ^ ^ ^+ + + +\ =+@ +@ +@ +@Z 2:6FF 
1 
1 
1 = $  '- '- '- '-T =           r   