a
    ~b0                     @   s&  d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ ed dkrddlmZ dd	lmZ dd
lmZ ddlmZ nZddlmZ dd	lmZ ed dkrdd
lmZ ddlmZ nddlmZ d\ZZdd ZddlZddlmZmZ ddlmZ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)version_info)
Javascriptdisplay   )
Substitute   )reader)HTMLExporter)ClearOutputPreprocessor)NotebookExporter   )current)NNc                 C   sT   d| d< d| d< | j dd | jd jD ]$}d|v r>g |d< d|v r*d|d< q*| S )	z(strip the outputs from a notebook objectr   nbformatr   Znbformat_minorZ	signatureNZoutputsZprompt_number)metadatapopZ
worksheetsZcells)nbZcell r   8lib/python3.9/site-packages/holoviews/ipython/archive.pyv3_strip_output"   s    
r      )FileArchivePickler)	HTML_TAGS
MIME_TYPESc                       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ddi df 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 t| jf i | d | _d | _d | _d| _i | _d | _d | _	dd t
 D | _dd | j  D }dd| | jj_d S )NFc                 S   s"   i | ]\}}|t v rt | |qS r   )r   .0kvr   r   r   
<dictcomp>_       z,NotebookArchive.__init__.<locals>.<dictcomp>c                 S   s    g | ]\}}d ||j jf qS )z%s=%s)	__class____name__r    r   r   r   
<listcomp>a   s   z,NotebookArchive.__init__.<locals>.<listcomp>zauto(enabled=Boolean, %s)z, )superr   __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*   U   s    zNotebookArchive.__init__c                 C   sH   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 )_r=   )
startswithsysmodulesr    r   r   r   r(   l   s   z1NotebookArchive.get_namespace.<locals>.<listcomp>r   z,Could not find holoviews module in namespacez
%s.archive)r@   rA   ImportErrorZget_ipythonZuser_nsr2   len	Exception)r9   matchesr   r   r   get_namespacef   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)r9   r   r   r   last_export_statuss   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f  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 %s.%senabledZdisabled N)rF   	namespacer,   tupletime	localtimer1   r   r   sleepr.   r4   Z	set_paramstrftimer   clearrH   )	r9   rK   rS   kwargsZkernelZnbnameZnbcmdcmdtstampr   r   r   r6      s&    





zNotebookArchive.autoc                    s   | j du rtd| jr8tt| j| j d| jid dS d| _|  }d| d }d| d|  d	 }t	
| j| j }| | j|| jd
}td|tjtj| jf 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); z1var pycmd = capture + ';%s._export_with_html()'; zkernel.execute(pycmd)rX   r   zExport name: %r
Directory    %rzK

If no output appears, please check holoviews.archive.last_export_status())r1   rD   skip_notebook_exportr)   r   exportr,   r-   rF   rO   rR   timestamp_format_formatexport_namerH   ospathr5   abspathrootr   r   )r9   rX   nameZcapture_cmdrU   rV   r_   r<   r   r   r\      s6    
zNotebookArchive.exportc           
   	      s   t | rt| j d nd}| jr| jdd }|D ]^}|g| _tt| j|||t	|| j
dd t | r~t| j d nd}	|	|kr6|| j|	< q6|| _dS )zDSimilar to FileArchive.add but accepts html strings for substitutionN)r   )rY   )rC   list_fileskeysr.   	exportersr)   r   adddictr,   r/   )
r9   objfilenamedatarY   htmlZinitial_last_keyri   exporterZnew_last_keyr<   r   r   rj      s    zNotebookArchive.addc                 C   s*   t  }|t| j| ||\}}|S )N)r	   register_preprocessorr   r+   from_notebook_node)r9   nodesubstitutionsrp   ro   r>   r   r   r   _generate_html   s    
zNotebookArchive._generate_htmlc                 C   sF   t d ur.t  }|tdd ||\}}nt|}t|d}|S )NT)rK   ipynb)r   rq   r
   rr   r   r   Zwrites)r9   rs   rp   clearedr>   Zstripped_noder   r   r   _clear_notebook   s    zNotebookArchive._clear_notebookc                    s  d| _ zvt| j| j}i }| j D ]\\}}}|\}}| j||fd}|du rXq(| 	||| j
d}	|	|rzd| nd }
|
|d d}d|vrq(|d | jvrq(tj|
\}}| ||dd }| 	| j|d  ||d dd	}||f||< q(|  }| ||}| j}tt| j||d
d| j
dd | |}tt| j||dd| j
dd tt| j| jd| j
id W n   t | _Y n0 d| _ dS )z@Computes substitutions before using nbconvert with preprocessorsFNrZ   z.%srL   	mime_type)srcry   r   )rz   ry   Zcssro   z	text/html)zfile-extry   r   )rm   rn   rY   rv   z	text/jsonr   rW   T)r-   rO   rR   r]   r1   rg   r2   r/   getr^   r,   r3   r`   ra   splitextZ_truncate_name_get_notebook_noderu   snapshot_namer)   r   rj   rx   r\   rI   
format_exc)r9   rV   rt   basenameextentryr>   rY   Zhtml_keyrm   ZfpathZ	truncatedZ	link_htmlrs   ro   Zexport_filenamerw   r<   r   r   _export_with_html   s\    



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.)rC   r0   rD   r   ZreadsZget_versionr+   )r9   sizers   r   r   r   r}     s    
z"NotebookArchive._get_notebook_node)TF)N)r'   
__module____qualname__r8   r4   Listr   ri   ZBooleanr[   Stringr~   Zfilename_formatterr_   r6   rI   r   ZffieldsunionZefieldsr*   rF   rJ   r\   rj   ru   rx   r   r}   __classcell__r   r   r<   r   r   3   s(   

3r   )!r8   rO   r@   r`   rI   ZIPythonr   ZIPython.displayr   r   Zpreprocessorsr   r   r   Z	nbconvertr	   Z#nbconvert.preprocessors.clearoutputr
   r   ZIPython.nbformatZIPython.nbconvertZ+IPython.nbconvert.preprocessors.clearoutputr   r   r4   Zcore.ior   r   Zplotting.rendererr   r   r   Znotebook_archiver   r   r   r   <module>   s.     o