B
    dC                 @   sB  d dl Z d dl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 ddl	m
Z
mZmZ ddlmZ ddlmZ ddlmZ dd	d
ddddddddgZedZedZedZeeedZG dd dejZdd	 Zd-ddZd.ddZd/d"dZd0d#dZd$d Z d%d Z!d&d Z"e
ed'd(Z#e
ed)d*Z$e
ed+d,Z%dS )1    N   )serve)
deprecatedget_idwrite_ipynb_local_js)Exporter)MPLD3Renderer)urlsfig_to_htmlfig_to_dict	fig_to_d3
display_d3displayshow_d3showenable_notebookdisable_notebook	save_html	save_jsona~  
{% if include_libraries %}
<script type="text/javascript" src="{{ d3_url }}"></script>
<script type="text/javascript" src="{{ mpld3_url }}"></script>
{% endif %}

<style>
{{ extra_css }}
</style>

<div id={{ figid }}></div>
<script type="text/javascript">

  !function(mpld3){
       {{ extra_js }}
       mpld3.draw_figure({{ figid }}, {{ figure_json }});
  }(mpld3);


</script>
a1  
<style>
{{ extra_css }}
</style>

<div id={{ figid }}></div>
<script type="text/javascript">

if(typeof(window.mpld3) !== "undefined" && window.mpld3._mpld3IsLoaded){
    !function (mpld3){
            {{ extra_js }}
            mpld3.draw_figure({{ figid }}, {{ figure_json }});
    }(mpld3);
}else{
  require.config({paths: {d3: "{{ d3_url[:-3] }}"}});
  require(["d3"], function(d3){
    window.d3 = d3;
    $.getScript("{{ mpld3_url }}", function(){
       {{ extra_js }}
       mpld3.draw_figure({{ figid }}, {{ figure_json }});
    });
  });
}
</script>
a  

<style>
{{ extra_css }}
</style>

<div id={{ figid }}></div>
<script>
function mpld3_load_lib(url, callback){
  var s = document.createElement('script');
  s.src = url;
  s.async = true;
  s.onreadystatechange = s.onload = callback;
  s.onerror = function(){console.warn("failed to load library " + url);};
  document.getElementsByTagName("head")[0].appendChild(s);
}

if(typeof(mpld3) !== "undefined" && mpld3._mpld3IsLoaded){
   // already loaded: just create the figure
   !function(mpld3){
       {{ extra_js }}
       mpld3.draw_figure({{ figid }}, {{ figure_json }});
   }(mpld3);
}else if(typeof define === "function" && define.amd){
   // require.js is available: use it to load d3/mpld3
   require.config({paths: {d3: "{{ d3_url[:-3] }}"}});
   require(["d3"], function(d3){
      window.d3 = d3;
      mpld3_load_lib("{{ mpld3_url }}", function(){
         {{ extra_js }}
         mpld3.draw_figure({{ figid }}, {{ figure_json }});
      });
    });
}else{
    // require.js not available: dynamically load d3 & mpld3
    mpld3_load_lib("{{ d3_url }}", function(){
         mpld3_load_lib("{{ mpld3_url }}", function(){
                 {{ extra_js }}
                 mpld3.draw_figure({{ figid }}, {{ figure_json }});
            })
         });
}
</script>
)simplenotebookgeneralc               @   s   e Zd ZdZdd ZdS )NumpyEncoderz& Special json encoder for numpy types c                sl   yt |}W n tk
r    Y nX  fdd|D S t|tjrH| S t|tjfr^| S tj	
 |S )Nc                s   g | ]}  |qS  )default).0item)selfr   [/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/mpld3/_display.py
<listcomp>   s    z(NumpyEncoder.default.<locals>.<listcomp>)iter	TypeError
isinstancenumpyZgenericr   ZndarraytolistjsonJSONEncoderr   )r   objiterabler   )r   r   r      s    zNumpyEncoder.defaultN)__name__
__module____qualname____doc__r   r   r   r   r   r      s   r   c             K   s6   t  }t|fddi||  |jd \} }}}|S )ae  Output json-serializable dictionary representation of the figure

    Parameters
    ----------
    fig : matplotlib figure
        The figure to display
    **kwargs :
        Additional keyword arguments passed to mplexporter.Exporter

    Returns
    -------
    fig_dict : dict
        the Python dictionary representation of the figure, which is
        directly convertible to json using the standard json package.

    See Also
    --------
    :func:`save_json`: save json representation of a figure to file
    :func:`save_html` : save html representation of a figure to file
    :func:`fig_to_html` : output html representation of the figure
    :func:`show` : launch a local server and show a figure in a browser
    :func:`display` : embed figure within the IPython notebook
    :func:`enable_notebook` : automatically embed figures in IPython notebook
    	close_mplFr   )r   r   runfinished_figures)figkwargsrendererZfigure_dict	extra_cssextra_jsr   r   r   r      s    Fr   Tc          	   K   s   |sd}t | }	|ptj}|p"tj}|r@|dd}|dd}|dkrjdt|  ttt d  }nt	
d|r~tdt }
t|
fd	d
i||  |
jd \} }}}|rd}d}|	jt|||tj|td|||dS )a4  Output html representation of the figure

    Parameters
    ----------
    fig : matplotlib figure
        The figure to display
    d3_url : string (optional)
        The URL of the d3 library.  If not specified, a standard web path
        will be used.
    mpld3_url : string (optional)
        The URL of the mpld3 library.  If not specified, a standard web path
        will be used.
    no_extras : boolean
        If true, remove any extra javascript or CSS. The output will be similar
        to that if the representation output by fig_to_json is embedded in
        a web page.
    template_type : string
        string specifying the type of HTML template to use. Options are:

        ``"simple"``
             suitable for a simple html page with one figure.  Will
             fail if require.js is available on the page.
        ``"notebook"``
             assumes require.js and jquery are available.
        ``"general"``
             more complicated, but works both in and out of the
             notebook, whether or not require.js and jquery are available
    figid : string (optional)
        The html/css id of the figure div, which must not contain spaces.
        If not specified, a random id will be generated.
    use_http : boolean (optional)
        If true, use http:// instead of https:// for d3_url and mpld3_url.
    include_libraries: boolean (optional)
        Whether to inject <script> tag to load JS libraries. Defaults to True.

    **kwargs :
        Additional keyword arguments passed to mplexporter.Exporter

    Returns
    -------
    fig_html : string
        the HTML representation of the figure

    See Also
    --------
    :func:`save_json`: save json representation of a figure to file
    :func:`save_html` : save html representation of a figure to file
    :func:`fig_to_dict` : output dictionary representation of the figure
    :func:`show` : launch a local server and show a figure in a browser
    :func:`display` : embed figure within the IPython notebook
    :func:`enable_notebook` : automatically embed figures in IPython notebook
    r   zhttps://zhttp://NZfig_g    _Bz\szfigid must not contain spacesr-   Fr    )cls)figidd3_url	mpld3_urlfigure_jsonr3   r4   include_libraries)TEMPLATE_DICTr	   ZD3_URLZ	MPLD3_URLreplacer   strintrandomresearch
ValueErrorr   r   r.   r/   renderr%   dumpsr   )r0   r8   r9   Z	no_extrasZtemplate_typer7   Zuse_httpr;   r1   templater2   r:   r3   r4   r   r   r   r
      s2    6

"c             K   sv   ddl m} ddlm} |rHd|ks,d|kr6td t \|d< |d< | dkrX| } |rf||  |t	| f|S )a  Display figure in IPython notebook via the HTML display hook

    Parameters
    ----------
    fig : matplotlib figure
        The figure to display (grabs current figure if missing)
    closefig : boolean (default: True)
        If true, close the figure so that the IPython matplotlib mode will not
        display the png version of the figure.
    local : boolean (optional, default=False)
        if True, then copy the d3 & mpld3 libraries to a location visible to
        the notebook server, and source them from there. See Notes below.
    **kwargs :
        additional keyword arguments are passed through to :func:`fig_to_html`.

    Returns
    -------
    fig_d3 : IPython.display.HTML object
        the IPython HTML rich display of the figure.

    Notes
    -----
    Known issues: using ``local=True`` may not work correctly in certain cases:

    - In IPython < 2.0, ``local=True`` may fail if the current working
      directory is changed within the notebook (e.g. with the %cd command).
    - In IPython 2.0+, ``local=True`` may fail if a url prefix is added
      (e.g. by setting NotebookApp.base_url).

    See Also
    --------
    :func:`show` : launch a local server and show a figure in a browser
    :func:`enable_notebook` : automatically embed figures in IPython notebook
    r   )HTMLNr9   r8   z3display: specified urls are ignored when local=True)
IPython.displayrG   matplotlib.pyplotpyplotwarningswarnr   gcfcloser
   )r0   Zclosefiglocalr1   rG   pltr   r   r   r     s    $
	127.0.0.1"  2   c          	   K   s   |r@d|d< d|d< dt tjd gdt tjd gd}nd}| dkr`d	dlm}	 |	 } t| f|}
t	|
||||||d
 dS )a  Open figure in a web browser

    Similar behavior to plt.show().  This opens the D3 visualization of the
    specified figure in the web browser.  On most platforms, the browser
    will open automatically.

    Parameters
    ----------
    fig : matplotlib figure
        The figure to display.  If not specified, the current active figure
        will be used.
    ip : string, default = '127.0.0.1'
        the ip address used for the local server
    port : int, default = 8888
        the port number to use for the local server.  If already in use,
        a nearby open port will be found (see n_retries)
    n_retries : int, default = 50
        the maximum number of ports to try when locating an empty port.
    local : bool, default = True
        if True, use the local d3 & mpld3 javascript versions, within the
        js/ folder.  If False, use the standard urls.
    open_browser : bool (optional)
        if True (default), then open a web browser to the given HTML
    http_server : class (optional)
        optionally specify an HTTPServer class to use for showing the
        figure. The default is Python's basic HTTPServer.
    **kwargs :
        additional keyword arguments are passed through to :func:`fig_to_html`

    See Also
    --------
    :func:`display` : embed figure within the IPython notebook
    :func:`enable_notebook` : automatically embed figures in IPython notebook
    z	/mpld3.jsr9   z/d3.jsr8   ztext/javascriptr)z	/mpld3.jsz/d3.jsNr   )ipport	n_retriesfilesopen_browserhttp_server)
openr	   ZMPLD3_LOCALreadZD3_LOCALrI   rJ   rM   r
   r   )r0   rU   rV   rW   rO   rY   rZ   r1   rX   rP   htmlr   r   r   r   ?  s    $c             K   s   yddl m} ddlm} W n tk
r8   tdY nX | rjd|ksNd|krXtd t \|d< |d< | }|jj	d }|
||fd	d
 dS )a  Enable the automatic display of figures in the IPython Notebook.

    This function should be used with the inline Matplotlib backend
    that ships with IPython that can be enabled with `%pylab inline`
    or `%matplotlib inline`. This works by adding an HTML formatter
    for Figure objects; the existing SVG/PNG formatters will remain
    enabled.

    Parameters
    ----------
    local : boolean (optional, default=False)
        if True, then copy the d3 & mpld3 libraries to a location visible to
        the notebook server, and source them from there. See Notes below.
    **kwargs :
        all keyword parameters are passed through to :func:`fig_to_html`

    Notes
    -----
    Known issues: using ``local=True`` may not work correctly in certain cases:

    - In IPython < 2.0, ``local=True`` may fail if the current working
      directory is changed within the notebook (e.g. with the %cd command).
    - In IPython 2.0+, ``local=True`` may fail if a url prefix is added
      (e.g. by setting NotebookApp.base_url).

    See Also
    --------
    :func:`disable_notebook` : undo the action of enable_notebook
    :func:`display` : embed figure within the IPython notebook
    :func:`show` : launch a local server and show a figure in a browser
    r   )get_ipython)Figurez1This feature requires IPython 1.0+ and Matplotlibr9   r8   z;enable_notebook: specified urls are ignored when local=Truez	text/htmlc             S   s   t | f|S )N)r
   )r0   kwdsr   r   r   <lambda>      z!enable_notebook.<locals>.<lambda>N)IPython.core.getipythonr^   matplotlib.figurer_   ImportErrorrK   rL   r   display_formatter
formattersZfor_type)rO   r1   r^   r_   rU   	formatterr   r   r   r   v  s     c              C   s^   yddl m}  ddlm} W n tk
r8   tdY nX |  }|jjd }|j|d dS )zDisable the automatic display of figures in the IPython Notebook.

    See Also
    --------
    :func:`enable_notebook` : automatically embed figures in IPython notebook
    r   )r^   )r_   z1This feature requires IPython 1.0+ and Matplotlibz	text/htmlN)	rc   r^   rd   r_   re   rf   rg   Ztype_printerspop)r^   r_   rU   rh   r   r   r   r     s    c             K   s<   t |trt|d}t|ds&td|t| f| dS )ak  Save a matplotlib figure to an html file

    Parameters
    ----------
    fig : matplotlib Figure instance
        The figure to write to file.
    fileobj : filename or file object
        The filename or file-like object in which to write the HTML
        representation of the figure.
    **kwargs :
        additional keyword arguments will be passed to :func:`fig_to_html`

    See Also
    --------
    :func:`save_json`: save json representation of a figure to file
    :func:`fig_to_html` : output html representation of the figure
    :func:`fig_to_dict` : output dictionary representation of the figure
    wwritez/fileobj should be a filename or a writable fileN)r"   r>   r[   hasattrrC   rk   r
   )r0   fileobjr1   r   r   r   r     s
    


c             K   sB   t |trt|d}t|ds&tdtjt| f||td dS )a  Save a matplotlib figure to a json file.

    Note that any plugins which depend on generated HTML will not be included
    in the JSON encoding.

    Parameters
    ----------
    fig : matplotlib Figure instance
        The figure to write to file.
    fileobj : filename or file object
        The filename or file-like object in which to write the HTML
        representation of the figure.
    **kwargs :
        additional keyword arguments will be passed to :func:`fig_to_dict`

    See Also
    --------
    :func:`save_html` : save html representation of a figure to file
    :func:`fig_to_html` : output html representation of the figure
    :func:`fig_to_dict` : output dictionary representation of the figure
    rj   rk   z/fileobj should be a filename or a writable file)r6   N)	r"   r>   r[   rl   rC   r%   dumpr   r   )r0   rm   r1   r   r   r   r     s
    


zmpld3.show_d3z
mpld3.showzmpld3.fig_to_d3zmpld3.fig_to_htmlzmpld3.display_d3zmpld3.display)NNFr   NFT)NTF)NrQ   rR   rS   TTN)F)&rK   r@   r%   Zjinja2r#   rA   os_serverr   utilsr   r   r   Zmplexporterr   Zmpld3rendererr   r5   r	   __all__TemplateZSIMPLE_HTMLZREQUIREJS_HTMLZGENERAL_HTMLr<   r&   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sN   + 
Y
4 
6
2