
    1VhC(                         d Z ddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ d	Zd
Z ej$                  d      ZdZddZddZd Zd Zd Z G d dej4                        Zy)zThe TensorBoard Text plugin.    N)wrappers)plugin_util)	http_util)provider)base_plugin)metadataz/tagsz/textz  **Warning:** This text summary contained data of dimensionality %d, but only   2d tables are supported. Showing a 2d slice of the data instead.d   c                 H    fd| D        }ddj                  |      z   dz   S )a  Given an iterable of string contents, make a table row.

    Args:
      contents: An iterable yielding strings.
      tag: The tag to place contents in. Defaults to 'td', you might want 'th'.

    Returns:
      A string containing the content strings, organized into a table row.

    Example: make_table_row(['one', 'two', 'three']) == '''
    <tr>
    <td>one</td>
    <td>two</td>
    <td>three</td>
    </tr>'''
    c              3   6   K   | ]  }d d|dd  yw)<>z</z>
N ).0stags     T/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/plugins/text/text_plugin.py	<genexpr>z!make_table_row.<locals>.<genexpr>A   s     A1#q#.As   z<tr>
 z</tr>
)join)contentsr   columnss    ` r   make_table_rowr   0   s)    " BAGbggg&&22    c                     t         t        j                        st        d       j                  dvrt        d j                  z        |rt        |t
        t        f      rt        j                  |      }t        |t        j                        st        d|z        |j                  dk7  rt        d|j                  z         j                  dk(  r j                  d   nd}|j                  d   |k7  rt        d	|j                  d   |fz        d
t        |d      z  }nd} j                  d   } j                  dk(  r fdt        |      D        }n fdt        |      D        }d|ddj                  |      dS )a  Given a numpy ndarray of strings, concatenate them into a html table.

    Args:
      contents: A np.ndarray of strings. May be 1d or 2d. In the 1d case, the
        table is laid out vertically (i.e. row-major).
      headers: A np.ndarray or list of string header names for the table.

    Returns:
      A string containing all of the content strings, organized into a table.

    Raises:
      ValueError: If contents is not a np.ndarray.
      ValueError: If contents is not 1d or 2d.
      ValueError: If contents is empty.
      ValueError: If headers is present and not a list, tuple, or ndarray.
      ValueError: If headers is not 1d.
      ValueError: If number of elements in headers does not correspond to number
        of columns in contents.
    z+make_table contents must be a numpy ndarray)      z3make_table requires a 1d or 2d numpy array, was %ddz,Could not convert headers %s into np.ndarrayr   zHeaders must be 1d, is %ddr   r   z4Number of headers %d must match number of columns %dz<thead>
%s</thead>
th)r   r   c              3   <   K   | ]  }t        |   g        y wNr   r   ir   s     r   r   zmake_table.<locals>.<genexpr>y   s     E!}-Es   c              3   B   K   | ]  }t        |d d f           y wr   r    r!   s     r   r   zmake_table.<locals>.<genexpr>{   s     F1x1~.Fs   z<table>
z<tbody>
z</tbody>
</table>)
isinstancenpndarray
ValueErrorndimlisttuplearrayshaper   ranger   )r   headersexpected_n_columnsheadern_rowsrowss   `     r   
make_tabler3   E   sh   ( h

+FGG}}F"Amm
 	

 ge}-hhw'G'2::.>H  <<19GLLHII2:--12DX^^A.!==11F==#%789  )>'t+LL^^AF}} FuV}EFfF9?OOr   c                     t        | t        j                        st        d      t	        | j
                        }|dk  rt        d      dg|dz
  z  t        d      t        d      gz   }| t        |         S )a  Given a np.npdarray with nDims > 2, reduce it to 2d.

    It does this by selecting the zeroth coordinate for every dimension greater
    than two.

    Args:
      arr: a numpy ndarray of dimension at least 2.

    Returns:
      A two-dimensional subarray from the input array.

    Raises:
      ValueError: If the argument is not a numpy ndarray, or the dimensionality
        is too low.
    z%reduce_to_2d requires a numpy.ndarrayr   z4reduce_to_2d requires an array of dimensionality >=2r   N)r$   r%   r&   r'   lenr,   slicer*   )arrndimsslicess      r   reduce_to_2dr:      so      c2::&@AA		NEqyOPPcUQYE$Kt#==FuV}r   c                      j                   sH|r#t        j                   j                               S t        j                   j                               S d}t         j                         dkD  r:t        j                  t        t         j                         z        }t                |r-t        j                   j                  d       fd      }||z   S d }t        j                   j                  d      D cg c]
  } ||       c}      j                   j                         }t        j                  t        |            }||z   S c c}w )a  Take a numpy.ndarray containing strings, and convert it into html.

    If the ndarray contains a single scalar string, that string is converted to
    html via our sanitized markdown parser. If it contains an array of strings,
    the strings are individually converted to html and then composed into a table
    using make_table. If the array contains dimensionality greater than 2,
    all but two of the dimensions are removed, and a warning message is prefixed
    to the table.

    Args:
      text_arr: A numpy.ndarray containing strings.
      enable_markdown: boolean, whether to enable Markdown

    Returns:
      The array converted to html.
    r   r   c                 r    t        t        j                  |       j                  j                              S r   )r3   r%   r+   reshaper,   )xstext_arrs    r   <lambda>z$text_array_to_html.<locals>.<lambda>   s#    z"((2,"6"6x~~"FG r   c                 H    t        | t              r| j                  d      S | S )Nzutf-8)r$   bytesdecode)bss    r   rA   z$text_array_to_html.<locals>.<lambda>   s    *R2GBIIg. R r   )r,   r   markdown_to_safe_htmlitem	safe_htmlr5   WARNING_TEMPLATEr:   markdowns_to_safe_htmlr>   r%   r+   r3   )r@   enable_markdownwarningtablerD   rE   text_arr_strs   `      r   text_array_to_htmlrO      s   " >>44X]]_EE((99G
8>>Q33s8>>22
  )22R G
 U? Pxx"*"2"22"67BVBZ7

'(..
! 	 %%j&>?U? 8s   >Ec                 &    t        ||      }| ||dS )z5Convert a text event into a JSON-compatible response.)	wall_timesteptext)rO   )rQ   rR   string_ndarrayrK   htmls        r   process_eventrV      s!    no>D r   c                       e Zd ZdZej
                  Zd Zd Zd Z	d Z
ej                  j                  d        Zd Zej                  j                  d        Zd	 Zy
)
TextPluginzText Plugin for TensorBoard.c                     |j                   xs i j                  | j                  t              | _        |j
                  | _        t        j                  dd      | _	        y)zzInstantiates TextPlugin via TensorBoard core.

        Args:
          context: A base_plugin.TBContext instance.
        rS   r   )	data_kindlatest_known_versionN)
sampling_hintsgetplugin_name_DEFAULT_DOWNSAMPLING_downsample_todata_provider_data_providerr   _MetadataVersionChecker_version_checker)selfcontexts     r   __init__zTextPlugin.__init__   sW      '55;@@3
 &33 + C C!"!
r   c                      y)NFr   re   s    r   	is_activezTextPlugin.is_active   s    r   c                 .    t        j                  d      S )Nztf-text-dashboard)element_name)r   FrontendMetadatari   s    r   frontend_metadatazTextPlugin.frontend_metadata   s    ++9LMMr   c                    | j                   j                  ||t        j                        }|D ci c]  }|g  }}|j	                         D ]x  \  }}|j	                         D ]`  \  }}t        j
                  |j                        }	| j                  j                  |	j                  ||      sM||   j                  |       b z |S c c}w )N)experiment_idr^   )rb   list_tensorsr   PLUGIN_NAMEitemsparse_plugin_metadataplugin_contentrd   okversionappend)
re   ctx
experimentmappingrunresulttag_to_contentr   	metadatummds
             r   
index_implzTextPlugin.index_impl   s    %%22$ ,, 3 

 &--c#r'--#*==? 	(C"0"6"6"8 (Y33I4L4LM,,//

CEs""3'	(	(  .s   
C
c                     t        j                  |j                        }t        j                  |j                        }| j	                  ||      }t        j                  ||d      S )Napplication/json)r   rf   environrp   r   r   Respond)re   requestry   rz   indexs        r   
tags_routezTextPlugin.tags_route   sP    !!'//2 ..w?
Z0  %1CDDr   c           
      j   | j                   j                  ||t        j                  | j                  t        j                  |g|g            }|j                  |i       j                  |d       }|g S |D cg c].  }t        |j                  |j                  |j                  |      0 c}S c c}w )N)runstags)rp   r^   
downsamplerun_tag_filter)rb   read_tensorsr   rr   r`   r   RunTagFilterr]   rV   rQ   rR   numpy)	re   ry   r|   r   rz   rK   all_textrS   ds	            r   	text_implzTextPlugin.text_impl  s    &&33$ ,,**#00se3%H 4 
 ||C$((d3<I 
 !++qvvqwwH
 	
 
s   :3B0c                    t        j                  |j                        }t        j                  |j                        }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|dk7  }| j                  |||||      }t        j                  ||d      S )Nr|   r   markdownfalser   )	r   rf   r   rp   argsr]   r   r   r   )	re   r   ry   rz   r|   r   markdown_argrK   responses	            r   
text_routezTextPlugin.text_route  s    !!'//2 ..w?
llu%llu%||''
3&'1>>#sC_M  (4FGGr   c                 F    t         | j                  t        | j                  iS r   )
TAGS_ROUTEr   
TEXT_ROUTEr   ri   s    r   get_plugin_appszTextPlugin.get_plugin_apps  s    
 	
r   N)__name__
__module____qualname____doc__r   rr   r^   rg   rj   rn   r   r   Requestapplicationr   r   r   r   r   r   r   rX   rX      ss    &&&K
N !!E "E
  !!H "H
r   rX   )tdr   )r   textwrapr   r%   werkzeugr   tensorboardr   tensorboard.backendr   tensorboard.datar   tensorboard.pluginsr   tensorboard.plugins.textr   r   r   dedentrI   r_   r   r3   r:   rO   rV   TBPluginrX   r   r   r   <module>r      s    #    # ) % + - 

 #8??F   3*8Pv6*ZO
%% O
r   