B
    @\.                 @   s  d Z ddlmZmZmZmZ ddlZee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ZG dd deZed&ddZG dd deZG dd deZG dd deZdd Zd'ddZdd Z dZ!dd Z"d d! Z#d"d# Z$d$d% Z%dS )(z

    )absolute_importdivisionprint_functionunicode_literalsN)SequenceOrderedDict)contextmanager   )Document)Modelcollect_models)settings)make_globally_unique_id)
FromCurdocOutputDocumentFor
RenderItem
RenderRootRenderRootsstandalone_docs_json%standalone_docs_json_and_render_itemssubmodel_has_python_callbacksc               @   s   e Zd ZdZdS )r   z This class merely provides a non-None default value for ``theme``
    arguments, since ``None`` itself is a meaningful value for users to pass.

    N)__name__
__module____qualname____doc__ r   r   /lib/python3.7/site-packages/bokeh/embed/util.pyr   :   s   r   Fc             #   s&  t  tr(t dks(tdd  D s0tddd }tdd  D }d|kr\|d |rv fd	d}t }n|t|dkrt }xh D ]}|	| qW nPt|d
kr|
 }t t|jkr fdd}t }n fdd}t }t r|  t|| |V  t| |  dS )a'   Find or create a (possibly temporary) Document to use for serializing
    Bokeh content.

    Typical usage is similar to:

    .. code-block:: python

         with OutputDocumentFor(models):
            (docs_json, [render_item]) = standalone_docs_json_and_render_items(models)

    Inside the context manager, the models will be considered to be part of a single
    Document, with any theme specified, which can thus be serialized as a unit. Where
    possible, OutputDocumentFor attempts to use an existing Document. However, this is
    not possible in three cases:

    * If passed a series of models that have no Document at all, a new Document will
      be created, and all the models will be added as roots. After the context mananger
      exits, the new Document will continue to be the models' document.

    * If passed a subset of Document.roots, then OutputDocumentFor temporarily "re-homes"
      the models in a new bare Document that is only available inside the context manager.

    * If passed a list of models that have differnet documents, then OutputDocumentFor
      temporarily "re-homes" the models in a new bare Document that is only available
      inside the context manager.

    OutputDocumentFor will also perfom document validation before yielding, if
    ``settings.perform_document_validation()`` is True.


        objs (seq[Model]) :
            a sequence of Models that will be serialized, and need a common document

        apply_theme (Theme or FromCurdoc or None, optional):
            Sets the theme for the doc while inside sith context manager. (default: None)

            If None, use whatever theme is on the document that is found or created

            If FromCurdoc, use curdoc().theme, restoring any previous theme afterwards

            If a Theme instance, use that theme, restoring any previous theme afterwards

        always_new (bool, optional) :
            Always return a new document, even in cases where it is otherwise possible
            to use an existing document on models.

    Yields:
        Document

    r   c             s   s   | ]}t |tV  qd S )N)
isinstancer   ).0xr   r   r   	<genexpr>x   s    z$OutputDocumentFor.<locals>.<genexpr>z.OutputDocumentFor expects a sequence of Modelsc               S   s   d S )Nr   r   r   r   r   finish{   s    z!OutputDocumentFor.<locals>.finishc             s   s   | ]}|j V  qd S )N)document)r   r   r   r   r   r    }   s    Nc                  s   t   d S )N)_dispose_temp_docr   )objsr   r   r!      s       c                  s   t   d S )N)r#   r   )r$   r   r   r!      s    c                  s   t   d S )N)r#   r   )r$   r   r   r!      s    )r   r   lenall
ValueErrorsetremove_create_temp_docr
   Zadd_rootpoprootsr   Zperform_document_validationZvalidate_set_temp_theme_unset_temp_theme)r$   apply_themeZ
always_newr!   docsdocmodelr   )r$   r   r   A   s6    7( 




r   c               @   s&   e Zd ZdddZdd Zdd ZdS )	r   Nc             C   sz   |d kr|d ks |d k	r(|d k	r(t d|d kr8t }nt|trTtdd |D }|| _|| _|| _t|| _|| _	d S )Nz*either docid or sessionid must be providedc             S   s   g | ]}|t  fqS r   )r   )r   rootr   r   r   
<listcomp>   s    z'RenderItem.__init__.<locals>.<listcomp>)
r(   r   r   listdocid	sessionid	elementidr   r-   use_for_title)selfr7   r8   r9   r-   r:   r   r   r   __init__   s     

zRenderItem.__init__c             C   sd   i }| j d k	r| j |d< n
| j|d< | jd k	r8| j|d< | jrL| j |d< | jd k	r`| j|d< |S )Nr7   r8   r9   r-   r:   )r7   r8   r9   r-   to_jsonr:   )r;   Zjsonr   r   r   r=      s    





zRenderItem.to_jsonc             C   s$   t || jsdS |  | kS d S )NF)r   	__class__r=   )r;   otherr   r   r   __eq__   s    zRenderItem.__eq__)NNNNN)r   r   r   r<   r=   r@   r   r   r   r   r      s   
r   c               @   s   e Zd ZdddZdd ZdS )r   Nc             C   s$   || _ || _|pd| _|pg | _d S )N )r9   idnametags)r;   r9   rB   rC   rD   r   r   r   r<      s    
zRenderRoot.__init__c             C   s    t || jsdS | j|jkS d S )NF)r   r>   r9   )r;   r?   r   r   r   r@      s    zRenderRoot.__eq__)NN)r   r   r   r<   r@   r   r   r   r   r      s   
r   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )r   c             C   s
   || _ d S )N)_roots)r;   r-   r   r   r   r<      s    zRenderRoots.__init__c             C   s   t | j S )N)r&   rE   items)r;   r   r   r   __len__   s    zRenderRoots.__len__c             C   sf   t |tr"t| j | \}}n0x.| j D ]\}}|j|kr.P q.W td| t||j|j|j	S )Nzroot with '%s' name not found)
r   intr6   rE   rF   rC   r(   r   rB   rD   )r;   keyr4   r9   r   r   r   __getitem__   s    

zRenderRoots.__getitem__c             C   s
   |  |S )N)rJ   )r;   rI   r   r   r   __getattr__   s    zRenderRoots.__getattr__c             C   s   t dd | j D S )Nc             S   s   g | ]\}}|j |fqS r   )rB   )r   r4   r9   r   r   r   r5      s    z'RenderRoots.to_json.<locals>.<listcomp>)r   rE   rF   )r;   r   r   r   r=      s    zRenderRoots.to_jsonN)r   r   r   r<   rG   rJ   rK   r=   r   r   r   r   r      s
   r   c             C   s   t | \}}|S )z

    )r   )models	docs_jsonrender_itemsr   r   r   r      s    r   c             C   sD  t | ttfr| g} t | tr0tdd | D s8tdt| rN|sNtt	 i }x| D ]}t |trpd}|}n|}|j
}|dkrtd||krt t f||< || \}}|dk	rt ||< qXx|jD ]}t ||< qW qXW i }x$| D ]\}\}}	| ||< qW g }
x,| D ] \}	\}}|
t||d qW ||
fS )z

    c             s   s   | ]}t |ttfV  qd S )N)r   r   r
   )r   r   r   r   r   r    	  s    z8standalone_docs_json_and_render_items.<locals>.<genexpr>z>Expected a Model, Document, or Sequence of Models or DocumentsNzHA Bokeh Model must be part of a Document to render as standalone content)r-   )r   r   r
   r   r'   r(   r   logZwarning_CALLBACKS_WARNINGr"   r   r   r-   rF   r=   appendr   )rL   Zsuppress_callback_warningr1   Zmodel_or_docr3   r2   r7   r-   rM   _rN   r   r   r   r     s:    


r   c             C   s<   d}x2t | D ]&}t|jdks.t|jdkrd}P qW |S )z@ Traverses submodels to check for Python (event) callbacks

    Fr   T)r   r&   Z
_callbacksZ_event_callbacks)rL   Zhas_python_callbackr3   r   r   r   r   0  s    r   aF  
You are generating standalone HTML/JS output, but trying to use real Python
callbacks (i.e. with on_change or on_event). This combination cannot work.

Only JavaScript callbacks may be used with standalone output. For more
information on JavaScript callbacks with Bokeh, see:

    http://bokeh.pydata.org/en/latest/docs/user_guide/interaction/callbacks.html

Alternatively, to use real Python callbacks, a Bokeh server application may
be used. For more information on building and running Bokeh applications, see:

    http://bokeh.pydata.org/en/latest/docs/user_guide/server.html
c             C   sT   t  }xB| D ]:}||j|j< ||_x"| D ]}||j|j< ||_q,W qW | |_|S )N)r
   Z_all_modelsrB   _temp_document
referencesrE   )rL   r2   mrefr   r   r   r+   O  s    
r+   c             C   s0   x*| D ]"}d |_ x| D ]
}d |_ qW qW d S )N)rS   rT   )rL   rU   rV   r   r   r   r#   Z  s    
r#   c             C   s>   | j | _|tkr,ddlm} | | j | _ n|d k	r:|| _ d S )Nr	   )curdoc)theme
_old_themer   iorW   )r2   r0   rW   r   r   r   r.   `  s     r.   c             C   s   t | dsd S | j| _| `d S )NrY   )hasattrrY   rX   )r2   r   r   r   r/   h  s    
r/   )NF)F)&r   Z
__future__r   r   r   r   ZloggingZ	getLoggerr   rO   collectionsr   r   
contextlibr   Zdocument.documentr
   r3   r   r   r   Zutil.serializationr   __all__objectr   r   r   r   r   r   r   r   rP   r+   r#   r.   r/   r   r   r   r   <module>	   s0   
g*
.