U
    /e=                     @  s  U d Z ddlmZ ddlZeeZddlZddlZddl	m
Z
 ddlmZmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ d	d
l m!Z!m"Z" d	dl#m$Z$ d	dl%m&Z& d	dl'm(Z(m)Z) d	dl*m*Z* d	dl+m,Z, ddl-m.Z.m/Z/ erd	dl'm0Z0 dZ1G dd dZ2G dd de2Z3G dd de2Z4G dd de2Z5G dd de2Z6G dd dZ7d d!dd"d#d$Z8d%d&d'd(d)d*Z9d+Z:e
G d,d- d-Z;G d.d/ d/ed0d1Z<i Z=d2e>d3< d%d4d5d"d6d7Z?d%d8d9d:d;Z@d%d<d'd(d=d>ZAd%d'd9d?d@ZBd%d'd9dAdBZCdCd'dDdEdFZDd%d'd9dGdHZEd%d'd9dIdJZFd%d'd9dKdLZGd%d'd9dMdNZHd%d'd9dOdPZIdS )Qz

    )annotationsN)	dataclass)abspathbasenamedirnameexistsjoinnormpath)	TYPE_CHECKINGCallableDictIteratorListSequenceSetTupleType)warn)	TypedDict   )CSS_RESOURCESJS_RESOURCES)Document)Model)BaseResources	Resources)settings)bundle_models   )contains_tex_stringis_tex_string)Hashes)Bundlebundle_for_objs_and_resourcesc                   @  s   e Zd ZdS )ArtifactN)__name__
__module____qualname__ r(   r(   6/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/embed/bundle.pyr$   P   s   r$   c                   @  s    e Zd ZdddddddZdS )		ScriptReftext/javascriptstrNone)urltypereturnc                 C  s   || _ || _d S N)r.   r/   )selfr.   r/   r(   r(   r)   __init__T   s    zScriptRef.__init__N)r+   r%   r&   r'   r3   r(   r(   r(   r)   r*   S   s   r*   c                   @  s    e Zd ZdddddddZdS )	Scriptr+   r,   r-   )contentr/   r0   c                 C  s   || _ || _d S r1   )r6   r/   )r2   r6   r/   r(   r(   r)   r3   Z   s    zScript.__init__N)r+   r4   r(   r(   r(   r)   r5   Y   s   r5   c                   @  s   e Zd ZdddddZdS )StyleRefr,   r-   )r.   r0   c                 C  s
   || _ d S r1   )r.   )r2   r.   r(   r(   r)   r3   `   s    zStyleRef.__init__Nr4   r(   r(   r(   r)   r7   _   s   r7   c                   @  s   e Zd ZdddddZdS )Styler,   r-   )r6   r0   c                 C  s
   || _ d S r1   )r6   )r2   r6   r(   r(   r)   r3   e   s    zStyle.__init__Nr4   r(   r(   r(   r)   r8   d   s   r8   c                   @  s   e Zd ZU ded< ded< ded< ded< ded< g g g g i fddddddd	d
ZddddZddddZddddZd#dddddZe	ddddZ
e	ddddZdddd d!Zd"S )$r"   z	List[str]js_filesjs_raw	css_filescss_rawr!   hashesr9   r:   r;   r<   r=   c                 C  sD   |d d  | _ |d d  | _|d d  | _|d d  | _|| _d S r1   r>   )r2   r9   r:   r;   r<   r=   r(   r(   r)   r3   q   s
    zBundle.__init__zIterator[str])r0   c                 c  s   |   V  |  V  d S r1   )
_render_js_render_cssr2   r(   r(   r)   __iter__y   s    
zBundle.__iter__r,   c                 C  s   t j| j| j| jdS )N)r9   r:   r=   )r   renderr9   r:   r=   rA   r(   r(   r)   r?   }   s    zBundle._render_jsc                 C  s   t j| j| jdS )N)r;   r<   )r   rC   r;   r<   rA   r(   r(   r)   r@      s    zBundle._render_cssTbool)tagr0   c                 C  s$   |rt j| jg dS d| jS d S )N)r:   r9   
)r   rC   r:   r   )r2   rE   r(   r(   r)   scripts   s    zBundle.scriptsc                 C  s   | j S r1   )r9   rA   r(   r(   r)   js_urls   s    zBundle.js_urlsc                 C  s   | j S r1   )r;   rA   r(   r(   r)   css_urls   s    zBundle.css_urlsr$   r-   )artifactr0   c                 C  sj   t |tr| j|j nLt |tr4| j|j n2t |trN| j	|j nt |t
rf| j|j d S r1   )
isinstancer*   r9   appendr.   r5   r:   r6   r7   r;   r8   r<   )r2   rJ   r(   r(   r)   add   s    



z
Bundle.addN)T)r%   r&   r'   __annotations__r3   rB   r?   r@   rG   propertyrH   rI   rM   r(   r(   r(   r)   r"   i   s&   
  r"   z!Sequence[Model | Document] | NonezHBaseResources | Tuple[BaseResources | None, BaseResources | None] | None)objs	resourcesr0   c                 C  s  t j|d}t|tr t|d}|dks2t|tr<| }}n`t|trt|dkrtdd |D r|\}}|r||s|t	d |r|st	d nt
d	|d
dlm} | rt| nd}| rt| nd}| rt| nd}| rt| nd}g }	g }
g }g }|r||}|s$d|jkr$|jd |sBd|jkrB|jd |s`d|jkr`|jd |s~d|jkr~|jd |	|j |
|j |r||}||j ||j |rxt| |}|dk	r|jnd}|dkr |
dd |D  nx|dkr |	dd |D  nX|dkrd|D ]2}|jdk	rL|	|j n|
t|j q.n|	dd |D  | rdd t| D nd}t|}|dk	r|
| t |	|
|||r|j!ni S )z Generate rendered CSS and JS resources suitable for the given
    collection of Bokeh objects

    Args:
        objs (seq[Model or Document]) :

        resources (BaseResources or tuple[BaseResources])

    Returns:
        Bundle

    )default)modeNr   c                 s  s    | ]}|d kpt |tV  qd S r1   )rK   r   ).0rr(   r(   r)   	<genexpr>   s     z0bundle_for_objs_and_resources.<locals>.<genexpr>z`No Bokeh CSS Resources provided to template. If required you will need to provide them manually.z_No Bokeh JS Resources provided to template. If required you will need to provide them manually.z8expected Resources or a pair of optional Resources, got r   )deepcopyTzbokeh-widgetszbokeh-tableszbokeh-glzbokeh-mathjaxinlinec                 S  s   g | ]}t |jqS r(   )r   _inlineartifact_pathrT   bundler(   r(   r)   
<listcomp>   s     z1bundle_for_objs_and_resources.<locals>.<listcomp>serverc                 S  s   g | ]
}|j qS r(   )
server_urlr[   r(   r(   r)   r]      s     Zcdnc                 S  s   g | ]
}|j qS r(   )rZ   r[   r(   r(   r)   r]      s     c                 S  s   g | ]
}|j qS r(   )	__class__)rT   objr(   r(   r)   r]      s     )"r   rQ   rK   r,   r   r   tuplelenallr   
ValueErrorcopyrW   _use_widgets_use_tables_use_gl_use_mathjaxZjs_componentsremoveextendr9   r:   r;   r<   _bundle_extensionsrS   cdn_urlrL   rY   rZ   	_all_objsr   r"   r=   )rP   rQ   Zjs_resourcesZcss_resourcesrW   Zuse_widgetsZ
use_tablesZuse_glZuse_mathjaxr9   r:   r;   r<   
extensionsrS   r\   modelsextr(   r(   r)   r#      sn    


(






r#   zSequence[Model | Document]zCallable[[Type[Model]], bool]rD   )rP   queryr0   c                 C  s~   t  }t| D ]j}t|drq|jdd }|dkr8q||krBq|| tj D ] }|j	
|rV||rV  dS qVqdS )N__implementation__.r   bokehTF)setro   hasattr__view_module__splitrM   r   model_class_reverse_mapvaluesr&   
startswith)rP   rs   namesra   namemodelr(   r(   r)   _query_extensions   s    

r   zhttps://unpkg.comc                   @  s*   e Zd ZU ded< ded< dZded< dS )ExtensionEmbedr,   rZ   r_   Nz
str | Nonern   )r%   r&   r'   rN   rn   r(   r(   r(   r)   r     s   
r   c                   @  s.   e Zd ZU ded< ded< ded< ded< dS )Pkgr,   r   versionmodulemainN)r%   r&   r'   rN   r(   r(   r(   r)   r     s   
r   F)totalzDict[str, str]extension_dirsr   zList[ExtensionEmbed]c              
   C  s|  t  }g }|jrddgndg}| d k	r.t| ntj D ]<}t|drJq8|jdd }|dkrdq8||krnq8|	| t
|}t|j}t|}	t|	d}
t|	d}t|sq8|j d	}t|	d
}d }t|rt|0}zt|}W n tjjk
r   Y nX W 5 Q R X d }|d k	r|dd }|d kr>td|dd}|d|dd }|d k	rt d| d| d| }nt|
| d}t|	t|}t|}t|}| d| }tjsJt }||   |! }| d| }nT|D ]>}t|
| | }|
}| d| | }t|r qJqtd| d|t"|< | d| }t#|||}|$| q8|S )Nz.min.jsz.jsrt   ru   r   rv   distzbokeh.ext.jsonzstatic/extensionszpackage.jsonr   z*invalid package.json; missing package namer   Zlatestr   r   /@z?v=z!can't resolve artifact path for 'z' extension)%rw   Zminifiedro   r   r{   r|   rx   ry   rz   rM   
__import__r   __file__r   r   r   Zroot_urlopenjsonloaddecoderJSONDecodeErrorgetre   _default_cdn_hostr	   r   r   devhashlibsha256updateencode	hexdigestr   r   rL   )rP   rQ   r~   Zbundlesrp   ra   r   r   Z	this_filebase_dirdist_dirZext_pathZserver_prefixpackage_pathpkgiorn   pkg_nameZpkg_versionZpkg_mainrZ   Zartifacts_dirZartifact_nameZserver_pathshaZvstringrr   r_   embedr(   r(   r)   rm     sv     












rm   z
Set[Model])rP   r0   c                 C  sD   t  }| D ]4}t|tr2|jD ]}|| O }qq
|| O }q
|S r1   )rw   rK   r   roots
references)rP   Zall_objsra   rootr(   r(   r)   ro   e  s    

ro   zCallable[[Model], bool]c                   sL   | D ]B}t |tr&t|j rF dS qt fdd| D r dS qdS )z Whether any of a collection of objects satisfies a given query predicate

    Args:
        objs (seq[Model or Document]) :

        query (callable)

    Returns:
        True, if ``query(obj)`` is True for some object in ``objs``, else False

    Tc                 3  s   | ]} |V  qd S r1   r(   )rT   refrs   r(   r)   rV     s     z_any.<locals>.<genexpr>F)rK   r   _anyr   anyr   )rP   rs   ra   r(   r   r)   r   q  s    
r   c                   s&   ddl m  t|  fddp$t| S )z Whether a collection of Bokeh objects contains a TableWidget

    Args:
        objs (seq[Model or Document]) :

    Returns:
        bool

    r   TableWidgetc                   s
   t |  S r1   rK   ra   r   r(   r)   <lambda>      z_use_tables.<locals>.<lambda>)models.widgetsr   r   _ext_use_tablesrP   r(   r   r)   rh     s    
rh   c                   s&   ddl m  t|  fddp$t| S )z Whether a collection of Bokeh objects contains a any Widget

    Args:
        objs (seq[Model or Document]) :

    Returns:
        bool

    r   Widgetc                   s
   t |  S r1   r   r   r   r(   r)   r     r   z_use_widgets.<locals>.<lambda>)r   r   r   _ext_use_widgetsr   r(   r   r)   rg     s    
rg   r   )r   r0   c                 C  s   ddl m} ddlm}m} t| |rht| jtr@t| jr@dS | j	
 D ]}t|trJt|rJ dS qJt| |r| js| jst| jrdS t| |r| jst| jrdS dS )zWhether a model requires MathJax to be loaded
    Args:
        model (Model): Model to check
    Returns:
        bool: True if MathJax required, False if not
    r   )Axis)Div	ParagraphTF)Zmodels.axesr   Zmodels.widgets.markupsr   r   rK   Z
axis_labelr,   r    Zmajor_label_overridesr|   Zdisable_mathZrender_as_textr   text)r   r   r   r   valr(   r(   r)   _model_requires_mathjax  s    


r   c                   s&   ddl m  t|  fddp$t| S )z Whether a collection of Bokeh objects contains a model requesting MathJax
    Args:
        objs (seq[Model or Document]) :
    Returns:
        bool
    r   MathTextc                   s   t |  pt| S r1   )rK   r   r   r   r(   r)   r     r   z_use_mathjax.<locals>.<lambda>)models.textr   r   _ext_use_mathjaxr   r(   r   r)   rj     s    rj   c                   s   ddl m  t|  fddS )z Whether a collection of Bokeh objects contains a plot requesting WebGL

    Args:
        objs (seq[Model or Document]) :

    Returns:
        bool

    r   Plotc                   s   t |  o| jdkS )NZwebgl)rK   Zoutput_backendr   r   r(   r)   r     r   z_use_gl.<locals>.<lambda>)Zmodels.plotsr   r   r   r(   r   r)   ri     s    
ri   c                   s   ddl m  t|  fddS )Nr   r   c                   s
   t |  S r1   
issubclassclsr   r(   r)   r     r   z!_ext_use_tables.<locals>.<lambda>)r   r   r   r   r(   r   r)   r     s    r   c                   s   ddl m  t|  fddS )Nr   r   c                   s
   t |  S r1   r   r   r   r(   r)   r     r   z"_ext_use_widgets.<locals>.<lambda>)r   r   r   r   r(   r   r)   r     s    r   c                   s   ddl m  t|  fddS )Nr   r   c                   s
   t |  S r1   r   r   r   r(   r)   r     r   z"_ext_use_mathjax.<locals>.<lambda>)r   r   r   r   r(   r   r)   r     s    r   )J__doc__
__future__r   logging	getLoggerr%   logr   r   Zdataclassesr   os.pathr   r   r   r   r   r	   typingr
   r   r   r   r   r   r   r   r   warningsr   Ztyping_extensionsr   Zcore.templatesr   r   Zdocument.documentr   r   r   rQ   r   r   r   Zutil.compilerr   utilr   r    r!   __all__r$   r*   r5   r7   r8   r"   r#   r   r   r   r   r   rN   rm   ro   r   rh   rg   r   rj   ri   r   r   r   r(   r(   r(   r)   <module>   sX   
 ,2[K