a
    Df=*                     @   s   d Z ddlZddlZddlZddlZddlZddlmZ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mZ d
dlmZ G dd deZe ZdS )zu
Implements NotebookArchive used to automatically capture notebook data
and export it to disk via the display hooks.
    N)
Javascriptdisplay)HTMLExporterNotebookExporter)ClearOutputPreprocessor)reader   )FileArchivePickler)	HTML_TAGS
MIME_TYPES   )
Substitutec                       s   e Zd ZdZejegdZejdddZ	ej
dddZej
d	d
dZej
dd
dZedZdZejdhZejdhZ fddZdd Zdd Zd#ddZd$ fdd	Zd% fdd	Zdd Zdd Z fdd Zd!d" Z  ZS )&NotebookArchivez
    FileArchive that can automatically capture notebook data via the
    display hooks and automatically adds a notebook HTML snapshot to
    the archive upon export.
    )defaultFz
        Whether to skip JavaScript capture of notebook data which may
        be unreliable. Also disabled automatic capture of notebook
        name.)r   docindexzx
        The basename of the exported notebook snapshot (html). It may
        optionally use the {timestamp} formatter.)r   z{dimensions},{obj}zy
        Similar to FileArchive.filename_formatter except with support
        for the notebook name field as {notebook}.
{notebook}Nnotebookc                    s   t  jf i | d | _d | _d | _d| _i | _d | _d | _dd t	
 D | _dd | j 
 D }dd| d| jj_d S )	NFc                 S   s"   i | ]\}}|t v rt | |qS  )r   .0kvr   r   f/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/holoviews/ipython/archive.py
<dictcomp>B       z,NotebookArchive.__init__.<locals>.<dictcomp>c                 S   s"   g | ]\}}| d |j j qS )=)	__class____name__r   r   r   r   
<listcomp>D   s   z,NotebookArchive.__init__.<locals>.<listcomp>zauto(enabled=Boolean, z, ))super__init__	nbversionnotebook_nameexport_success_auto_replacements_notebook_data
_timestampr   items_tagsparamZobjectsjoinauto__func____doc__)selfparamskeywordsr   r   r   r#   8   s    zNotebookArchive.__init__c                 C   sJ   dt jvrtddd t j D }t|dkr<td|d  dS )zF
        Find the name the user is using to access holoviews.
        	holoviewsz&HoloViews does not seem to be importedc                 S   s,   g | ]$\}}| d s|tjd u r|qS )_r6   )
startswithsysmodulesr   r   r   r   r    O   s   z1NotebookArchive.get_namespace.<locals>.<listcomp>r   z,Could not find holoviews module in namespacez.archive)r9   r:   ImportErrorZget_ipythonZuser_nsr+   len	Exception)r2   matchesr   r   r   get_namespaceI   s    
zNotebookArchive.get_namespacec                 C   sZ   | j du rtd dS | j du r,td dS td | jdu rHtd ntd| j  dS )z@Helper to show the status of the last call to the export method.Tz9The last call to holoviews.archive.export was successful.NztStatus of the last call to holoviews.archive.export is unknown.
(Re-execute this method once kernel status is idle.)z;The last call to holoviews.archive.export was unsuccessful.z
<No traceback captured>
)r&   print	traceback)r2   r   r   r   last_export_statusV   s    



z"NotebookArchive.last_export_statusTc           	      K   s   |   | _d| _tt | _d}d}d| j }|| | d }tt| t	d || _
| jjf i | td| j}|rt|  td|rd	nd
|r|nd dS )z
        Method to enable or disable automatic capture, allowing you to
        simultaneously set the instance parameters.
        r   z&var kernel = IPython.notebook.kernel; z3var nbname = IPython.notebook.get_notebook_name(); z8var name_cmd = '%s.notebook_name = \"' + nbname + '\"'; zkernel.execute(name_cmd); g      ?z [%Y-%m-%d %H:%M:%S]zAutomatic capture is now {}.{}enableddisabled N)r?   	namespacer%   tupletime	localtimer*   r   r   sleepr'   r-   updatestrftimer	   clearrA   format)	r2   rD   rN   kwargskernelZnbnameZnbcmdcmdtstampr   r   r   r/   f   s"    




zNotebookArchive.autoc              	      s   | j du rtd| jr4t j| j d| jid dS d| _|  }d| d }d| d| d	 d
 }t	| j
| j }| | j|| jd}td|tjtj| jd  tt| dS )z;
        Get the current notebook data and export.
        Nz3No timestamp set. Has the archive been initialized?r   	timestampinfoz)var capture = '%s._notebook_data=r\"\"\"'z+json_string+'\"\"\"'; z~var kernel = IPython.notebook.kernel; var json_data = IPython.notebook.toJSON(); var json_string = JSON.stringify(json_data); zvar pycmd = capture + ';z._export_with_html()'; zkernel.execute(pycmd)rU   r   z#Export name: {!r}
Directory    {!r}zK

If no output appears, please check holoviews.archive.last_export_status())r*   r=   skip_notebook_exportr"   exportr%   r&   r?   rI   rM   timestamp_format_formatexport_namerA   rO   ospathr.   abspathrootr   r   )r2   rU   nameZcapture_cmdrR   rS   r\   r5   r   r   rY   }   s6    


zNotebookArchive.exportc           
         s   |du ri }t | r&t| j d nd}| jr| jdd }|D ]^}|g| _t|| jd}t j	||||d t | rt| j d nd}	|	|krB|| j
|	< qB|| _dS )zDSimilar to FileArchive.add but accepts html strings for substitutionN)r   )rV   )r<   list_fileskeysr'   	exportersdictr%   r"   addr(   )
r2   objfilenamedatarV   htmlZinitial_last_keyrf   exporterZnew_last_keyr5   r   r   rh      s    zNotebookArchive.addc                 C   s*   t  }|t| j| ||\}}|S )N)r   register_preprocessorr   r$   from_notebook_node)r2   nodesubstitutionsrm   rl   r7   r   r   r   _generate_html   s    
zNotebookArchive._generate_htmlc                 C   s(   t  }|tdd ||\}}|S )NT)rD   )r   rn   r   ro   )r2   rp   rm   clearedr7   r   r   r   _clear_notebook   s    zNotebookArchive._clear_notebookc                    s  d| _ ztt| j| j}i }| j D ]\\}}}|\}}| j||fd}|du rXq(| 	||| j
d}	|	|r|d| nd }
|
|d d}d|vrq(|d | jvrq(tj|
\}}| ||dd }| 	| j|d  ||d dd	}||f||< q(|  }| ||}| j}d
d| j
d}t j|||d | |}dd| j
d}t j|||d t j| jd| j
id W n ty   t | _Y n0 d| _ dS )z@Computes substitutions before using nbconvert with preprocessorsFNrW   .rF   	mime_type)srcrv   r   )rw   rv   cssrl   z	text/html)zfile-extrv   r   )rj   rk   rV   Zipynbz	text/jsonr   rT   T)r&   rI   rM   rZ   r*   rd   r+   r(   getr[   r%   r,   r]   r^   splitextZ_truncate_name_get_notebook_noderr   snapshot_namer"   rh   rt   rY   r=   rB   
format_exc)r2   rS   rq   basenameextentryr7   rV   Zhtml_keyrj   ZfpathZ	truncatedZ	link_htmlrp   rl   Zexport_filenamers   r5   r   r   _export_with_html   sX    




z!NotebookArchive._export_with_htmlc                 C   s6   t | j}|dkrtdt| j}t|| _|S )zLoad captured notebook noder   z/Captured buffer size for notebook node is zero.)r<   r)   r=   r   Zreadsget_versionr$   )r2   sizerp   r   r   r   r{      s    
z"NotebookArchive._get_notebook_node)TF)N)NNNNN)r   
__module____qualname__r1   r-   Listr
   rf   BooleanrX   Stringr|   Zfilename_formatterr\   r/   rB   r	   ZffieldsunionZefieldsr#   r?   rC   rY   rh   rr   rt   r   r{   __classcell__r   r   r5   r   r      s(   

3r   )r1   r]   r9   rI   rB   r-   ZIPython.displayr   r   Z	nbconvertr   r   Z#nbconvert.preprocessors.clearoutputr   Znbformatr   Zcore.ior	   r
   Zplotting.rendererr   r   Zpreprocessorsr   r   Znotebook_archiver   r   r   r   <module>   s    k