
    e=*                         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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                       e Zd ZdZ ej        eg          Z ej        dd          Z	 ej
        dd          Z ej
        d	d
          Z ej
        dd
          Z ej        d          ZdZej                            dh          Zej                            dh          Z fdZd Zd ZddZd fd	Zd fd	Zd Zd Z fdZd Z x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                     t                      j        di | d | _        d | _        d | _        d| _        i | _        d | _        d | _        d t          j
                    D             | _        d | j                                        
                                D             }dd                    |           d| j        j        _        d S )NFc                 B    i | ]\  }}|t           v t           |         |S  )r   .0kvs      9lib/python3.11/site-packages/holoviews/ipython/archive.py
<dictcomp>z,NotebookArchive.__init__.<locals>.<dictcomp>B   s'    VVV#!Aa:oojmAooo    c                 6    g | ]\  }}| d |j         j         S )=)	__class____name__r   s      r   
<listcomp>z,NotebookArchive.__init__.<locals>.<listcomp>D   s?     > > >1 111;/11 > > >r    zauto(enabled=Boolean, z, )r   )super__init__	nbversionnotebook_nameexport_success_auto_replacements_notebook_data
_timestampr   items_tagsparamobjectsjoinauto__func____doc__)selfparamskeywordsr#   s      r   r(   zNotebookArchive.__init__8   s    ""6"""!"
"VV1B1BVVV
> > $
 2 2 4 4 : : < <> > >%Tdii>Q>Q%T%T%T	"""r    c                     dt           j        vrt          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                 j    g | ]0\  }}|                     d           s|t          j        d         u .|1S )_r<   )
startswithsysmodulesr   s      r   r%   z1NotebookArchive.get_namespace.<locals>.<listcomp>O   sO     G G G1,,s##G()S[-E(E(E (E(E(Er    r   z,Could not find holoviews module in namespacez.archive)r@   rA   ImportErrorget_ipythonuser_nsr0   len	Exception)r8   matchess     r   get_namespacezNotebookArchive.get_namespaceI   s     ck))FGGGG G 5 ; ; = = G G Gw<<1JKKK!*&&&&r    c                     | j         du rt          d           dS | j         t          d           dS t          d           | j        t          d           dS t          d| j        z              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)r8   s    r   last_export_statusz"NotebookArchive.last_export_statusV   s    $&&MNNNF ( K L L LFKLLL>!-.....$t~%&&&&&r    Tc                 
   |                                  | _        d| _        t          t	          j                              | _        d}d}d| j        z  }||z   |z   dz   }t          t          |                     t	          j	        d           || _
         | j        j        di | t	          j        d| j                  }|rt          j        |            t!          d                    |rd	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 Nr   )rH   	namespacer*   tupletime	localtimer/   r   r   sleepr,   r2   updatestrftimer
   clearrK   format)	r8   rO   rY   kwargskernelnbnamenbcmdcmdtstamps	            r   r5   zNotebookArchive.autof   s   
 ++--) 0 011:GLt~]&)EE
3   
3

##F###5tGG 	$d###.5576ZiiPZ"*, , 	- 	- 	- 	- 	-r    c           
         | j         t          d          | j        r1t                                          | j         d| j        i           dS d| _        |                                 }d|z  dz   }d|z   d| d	z   d
z   }t          j	        | j
        | j                   }|                     | j        || j        d          }t          d                    |t          j                            t          j                            | j                                      dz              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)rc   r   z#Export name: {!r}
Directory    {!r}zK

If no output appears, please check holoviews.archive.last_export_status())r/   rF   skip_notebook_exportr'   exportr*   r+   rH   rT   rX   timestamp_format_formatexport_namerK   rZ   ospathr4   abspathrootr   r   )r8   rc   namecapture_cmdr_   r`   rj   r#   s          r   rg   zNotebookArchive.export}   si    ?"QRRR$ 	GGNNT_!+D,> ?  A A AF"!!##DtK34B  JDIII	J
 ** t4doFFll4#3&UYUg5h5hii5<<[57W\\"'//RVR[B\B\5]5]_ _`a 	b 	b 	b 	
3     r    c                    |i }t          |           r,t          | j                                                  d         nd}| j        r| j        dd         }|D ]}|g| _        t          || j                  }t                      	                    ||||           t          |           r,t          | j                                                  d         nd}	|	|k    r
|| j
        |	<   || _        dS dS )zDSimilar to FileArchive.add but accepts html strings for substitutionN)r   )rd   )rE   list_fileskeysr,   	exportersdictr*   r'   addr-   )r8   objfilenamedatard   htmlinitial_last_keyrv   exporternew_last_keyr#   s             r   rx   zNotebookArchive.add   s   <D;>t99N4 0 0 2 233B77$: 	'qqq)I% < <"*D4+=>>>C4d;;;?B4yyRtDK$4$4$6$677;;d#3337;D&|4 'DNNN	' 	'r    c                     t                      }|                    t          | j        |                     |                    |          \  }}|S N)r   register_preprocessorr   r)   from_notebook_node)r8   nodesubstitutionsr~   r|   r>   s         r   _generate_htmlzNotebookArchive._generate_html   sW    >>&&z$.2?(A (A 	B 	B 	B,,T22Qr    c                     t                      }|                    t          d                     |                    |          \  }}|S )NT)rO   )r   r   r   r   )r8   r   r~   clearedr>   s        r   _clear_notebookzNotebookArchive._clear_notebook   sI    #%%&&'>t'L'L'LMMM0066
r    c                 r   d| _         	 t          j        | j        | j                  }i }| j                                        D ]\  \  }}}|\  }}| j                            ||fd          }|-| 	                    ||| j
        d          }	|	|rd| ndz   }
|
|d         d}d|vrg|d         | j        vrwt          j                            |
          \  }}|                     ||dd                   }| 	                    | j        |d                  ||d         dd	          }||f||<   |                                 }|                     ||          }| j        }d
d| j
        d}t'                                          |||           |                     |          }dd| j
        d}t'                                          |||           t'                                          | j        d| j
        i           d| _         dS # t.          $ r t1          j                    | _        Y dS w xY w)z@Computes substitutions before using nbconvert with preprocessorsFNre   .rQ   	mime_type)srcr   r   )r   r   cssr|   z	text/html)zfile-extr   r   )rz   r{   rd   ipynbz	text/jsonr   rb   T)r+   rT   rX   rh   r/   rt   r0   r-   getri   r*   r1   rk   rl   splitext_truncate_name_get_notebook_noder   snapshot_namer'   rx   r   rg   rF   rL   
format_exc)r8   r`   r   basenameextentryr>   rd   html_keyrz   fpath	truncated	link_htmlr   r|   export_filenamer   r#   s                    r   _export_with_htmlz!NotebookArchive._export_with_html   s   #.	']4#8$/JJFM*.+*;*;*=*= E E&3!	D-118S/4HH#X<<v>B>P3R 3R S S s":)c)));#k1BCCd**D+&dj88$$&G$4$4U$;$;MHc $ 3 3Hc!""g F FI $TZ[8I-J4=:>{:K46.8 .8!9 !9I 09).DM(++**,,D&&t];;D"0O !' +#13 3D GGKKt$KGGG**400G& +#13 3D GGKKwTKJJJGGNNT_!+D,> ?  A A A
 #'D  	4 	4 	4&133DNNNN	4s   G=H "H65H6c                     t          | j                  }|dk    rt          d          t          j        | j                  }t          j        |          | _        |S )zLoad captured notebook noder   z/Captured buffer size for notebook node is zero.)rE   r.   rF   r   readsget_versionr)   )r8   sizer   s      r   r   z"NotebookArchive._get_notebook_node   sS    4&''199MNNN|D/00+D11r    )TFr   )NNNNN)r$   
__module____qualname__r7   r2   Listr   rv   Booleanrf   Stringr   filename_formatterrj   r5   rL   r
   ffieldsunionefieldsr(   rH   rM   rg   rx   r   r   r   r   __classcell__)r#   s   @r   r   r      s        
 
G9---I(5= =   
 !EL /5 6 6 6M &.B I6 7 7 7 %,| :6 7 7 7K
 5=D I!''55G!''55GU U U U U"
' 
' 
'' ' ' - - - -.! ! ! ! ! !>' ' ' ' ' '0    1' 1' 1' 1' 1'f      r    r   )r7   rk   r@   rT   rL   r2   IPython.displayr   r   	nbconvertr   r   #nbconvert.preprocessors.clearoutputr   nbformatr   core.ior
   r   plotting.rendererr   r   preprocessorsr   r   notebook_archiver   r    r   <module>r      s    
 
			 



       / / / / / / / / 4 4 4 4 4 4 4 4 G G G G G G       * * * * * * * * 5 5 5 5 5 5 5 5 % % % % % %g g g g gk g g gT #?$$   r    