U
    Vc                     @   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   N/tmp/pip-unpacked-wheel-g8kmtpbc/tensorboard/plugins/hparams/hparams_plugin.py__init__7   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_activeI   s    zHParamsPlugin.is_activec                 C   s   t jddS )Nztf-hparams-dashboard)Zelement_name)r   ZFrontendMetadatar   r   r   r   frontend_metadataL   s    zHParamsPlugin.frontend_metadatac              
   C   s   t |j}t |j}z|jd}t|jd}t|t	j
}t|| j|| }t|| j| }t| j|||| \}	}
t||	|
W S  tjk
r } z$td|  tjjt|dW 5 d }~X Y nX d S )NformatZ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%   Zresponse_formatZcolumns_visibilityrequest_protoZsession_groupsZ
experimentbodyZ	mime_typeer   r   r   r   P   sB    
      z!HParamsPlugin.download_data_routec              
   C   s   t |j}t |j}z6t|tj}t|t	j
t|| j| dddW S  tjk
r } z$td|  tjjt|dW 5 d }~X Y nX d S NT)Zincluding_default_value_fieldsapplication/jsonr!   r"   )r   r   r$   r%   r*   r   ZGetExperimentRequestr   r.   r   MessageToJsonr   r,   r   r-   r   r/   r0   r1   r2   r3   r4   )r   r5   r6   r%   _r9   r   r   r   r   o   s&      
z"HParamsPlugin.get_experiment_routec              
   C   s   t |j}t |j}z8t|tj}t|t	j
t|| j|| dddW S  tjk
r } z$td|  tjjt|dW 5 d }~X Y nX d S r:   )r   r   r$   r%   r*   r   r+   r   r.   r   r<   r
   r,   r   r-   r   r/   r0   r1   r2   r3   r4   )r   r5   r6   r%   r7   r9   r   r   r   r      s.        
z'HParamsPlugin.list_session_groups_routec                 C   s   t |j}t |j}zJt|tj}|  }|s>tj	
dt|tt|||| dW S  tjk
r } z$td|  tj	jt|dW 5 d }~X Y nX d S )NzScalars plugin not loadedr;   r!   r"   )r   r   r$   r%   r*   r   ZListMetricEvalsRequest_get_scalars_pluginr1   r2   ZNotFoundr   r.   r(   dumpsr	   r,   r-   r   r/   r0   r3   r4   )r   r5   r6   r%   r7   Zscalars_pluginr9   r   r   r   r      s2        	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   Z
tb_contextZplugin_name_to_instancer'   scalars_metadataPLUGIN_NAMEr   r   r   r   r>      s    
z!HParamsPlugin._get_scalars_pluginN)__name__
__module____qualname____doc__r   rA   Zplugin_namer   r   r   r   r   RequestZapplicationr   r   r   r   r>   r   r   r   r   r   /   s   




r   c              
   C   sn   | j dkr| jn
| jd}zt|| W S  ttjfk
rh } zt	d
|||W 5 d }~X Y nX d S )NPOSTr5   z?Expected a JSON-formatted request data of type: {}, but got {} )methoddatar&   r'   r   ParseAttributeError
ParseErrorr   r/   r    )r5   Zproto_classZrequest_jsonr9   r   r   r   r*      s    
 r*   )rE   r(   r1   r   Ztensorboardr   Ztensorboard.plugins.hparamsr   r   r   r   r   r	   r
   r   Zgoogle.protobufr   Ztensorboard.backendr   Ztensorboard.pluginsr   Ztensorboard.plugins.scalarr@   Ztensorboard.utilr   Z
get_loggerr0   ZTBPluginr   r*   r   r   r   r   <module>   s*    