o
    2hV                     @   s   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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dlmZ ddlmZ ddlmZ e ZG dd dejZdd ZdS )zwThe TensorBoard HParams plugin.

See `http_api.md` in this directory for specifications of the routes for
this plugin.
    N)wrappers)plugin_util)api_pb2)backend_context)download_data)error)get_experiment)list_metric_evals)list_session_groups)metadata)json_format)	http_util)base_plugin)
tb_loggingc                   @   s~   e Zd ZdZejZdd Zdd Zdd Z	dd	 Z
ejjd
d Zejjdd Zejjdd Zejjdd Zdd ZdS )HParamsPluginznHParams Plugin for TensorBoard.

    It supports both GETs and POSTs. See 'http_api.md' for more details.
    c                 C   s   t || _dS )z~Instantiates HParams plugin via TensorBoard core.

        Args:
          context: A base_plugin.TBContext instance.
        N)r   Context_context)selfcontext r   e/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorboard/plugins/hparams/hparams_plugin.py__init__5   s   zHParamsPlugin.__init__c                 C   s   | j | j| j| jdS )zSee base class.)z/download_dataz/experimentz/session_groupsz/metric_evals)download_data_routeget_experiment_routelist_session_groups_routelist_metric_evals_router   r   r   r   get_plugin_apps=   s
   zHParamsPlugin.get_plugin_appsc                 C   s   dS )NFr   r   r   r   r   	is_activeG   s   zHParamsPlugin.is_activec                 C   s   t jddS )Nztf-hparams-dashboard)element_name)r   FrontendMetadatar   r   r   r   frontend_metadataJ   s   zHParamsPlugin.frontend_metadatac              
   C   s   t |j}t |j}zA|jd}t|jd}t|t	j
}t|| j|| }t|| j|| }t| j|||| \}	}
t||	|
W S  tjyj } ztd|  tjjt|dd }~ww )NformatcolumnsVisibilityHParams error: %sdescription)r   r   environexperiment_idargsgetjsonloads_parse_request_argumentr   ListSessionGroupsRequestr
   Handlerr   runr   r   r   Respondr   HParamsErrorloggerwerkzeug
exceptions
BadRequeststr)r   requestctxr(   response_formatcolumns_visibilityrequest_protosession_groups
experimentbody	mime_typeer   r   r   r   N   sF   


z!HParamsPlugin.download_data_routec              
   C      t |j}t |j}zt|tj}t|| j	||
 }t |}t||dW S  tjyG } ztd|  tjjt|dd }~ww Napplication/jsonr$   r%   )r   r   r'   r(   r-   r   GetExperimentRequestr   r/   r   r0   proto_to_jsonr   r1   r   r2   r3   r4   r5   r6   r7   r   r8   r9   r(   r<   response_protoresponserA   r   r   r   r   m   2   
z"HParamsPlugin.get_experiment_routec              
   C   rB   rC   )r   r   r'   r(   r-   r   r.   r
   r/   r   r0   rF   r   r1   r   r2   r3   r4   r5   r6   r7   rG   r   r   r   r      rJ   z'HParamsPlugin.list_session_groups_routec              
   C   s   t |j}t |j}z"t|tj}|  }|stj	
dt|t|||| dW S  tjyK } ztd|  tj	jt|dd }~ww )NzScalars plugin not loadedrD   r$   r%   )r   r   r'   r(   r-   r   ListMetricEvalsRequest_get_scalars_pluginr4   r5   NotFoundr   r1   r	   r/   r0   r   r2   r3   r6   r7   )r   r8   r9   r(   r<   scalars_pluginrA   r   r   r   r      s,   z%HParamsPlugin.list_metric_evals_routec                 C   s   | j jjtjS )z{Tries to get the scalars plugin.

        Returns:
        The scalars plugin or None if it is not yet registered.
        )r   
tb_contextplugin_name_to_instancer*   scalars_metadataPLUGIN_NAMEr   r   r   r   rL      s   
z!HParamsPlugin._get_scalars_pluginN)__name__
__module____qualname____doc__r   rR   plugin_namer   r   r   r!   r   Requestapplicationr   r   r   r   rL   r   r   r   r   r   -   s     




r   c              
   C   s`   | j dkr| jn| jd}zt|| W S  ttjfy/ } z
t	d
|||d }~ww )NPOSTr8   z?Expected a JSON-formatted request data of type: {}, but got {} )methoddatar)   r*   r   ParseAttributeError
ParseErrorr   r2   r"   )r8   proto_classrequest_jsonrA   r   r   r   r-      s    

r-   )rV   r+   r4   r   tensorboardr   tensorboard.plugins.hparamsr   r   r   r   r   r	   r
   r   google.protobufr   tensorboard.backendr   tensorboard.pluginsr   tensorboard.plugins.scalarrQ   tensorboard.utilr   
get_loggerr3   TBPluginr   r-   r   r   r   r   <module>   s,    