B
    d                 @   s   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ZdZyddlZ	W n  e
k
rd   ddlm	Z	 Y nX dddZddd	ZdddZdS )z,
A Simple server used to show mpld3 images.
    Na  
Note: if you're in the IPython notebook, mpld3.show() is not the best command
      to use. Consider using mpld3.display(), or mpld3.enable_notebook().
      See more information at http://mpld3.github.io/quickstart.html .

You must interrupt the kernel to end this command
)serverc                s(    d kri  G  fdddt j}|S )Nc                   s   e Zd Z fddZdS )z#generate_handler.<locals>.MyHandlerc                s   | j dkrZ| d | dd |   | jd  | j  | jd  nx| j  krȈ | j  \}}| d | d| |   y| j|  W q tk
r   | j| Y qX n
| d dS )	zRespond to a GET request./   zContent-typez	text/htmlz3<html><head><title>mpld3 plot</title></head><body>
z</body></html>i  N)	pathZsend_responseZsend_headerZend_headerswfilewriteencodeUnicodeDecodeErrorZ
send_error)selfcontent_typecontent)fileshtml Z/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/mpld3/_server.pydo_GET    s"    



z*generate_handler.<locals>.MyHandler.do_GETN)__name__
__module____qualname__r   r   )r   r   r   r   	MyHandler   s   r   )r   ZBaseHTTPRequestHandler)r   r   r   r   )r   r   r   generate_handler   s    r   2   c          	      s~   t  fddt|D  td| d|  }x>|D ]6 ttjtj}||  f}|	  |dkr8 S q8W t
ddS )z)Find an open port near the specified portc             3   s   | ]} | V  qd S )Nr   ).0i)portr   r   	<genexpr><   s    z!find_open_port.<locals>.<genexpr>   r   zno open ports foundN)	itertoolschainrangerandomrandintsocketAF_INETSOCK_STREAM
connect_exclose
ValueError)ipr   nZportssresultr   )r   r   find_open_port:   s    
r-   	127.0.0.1"  Tc          	      s   t  |t| |}|dkr0t f|}	n| f|}	|r`yt W n   Y n
X tt td  tj	
  |r fdd}
tj|
d  y|	  W n  ttfk
r   td Y nX |	  dS )a  Start a server serving the given HTML, and (optionally) open a
    browser

    Parameters
    ----------
    html : string
        HTML to serve
    ip : string (default = '127.0.0.1')
        ip address at which the HTML will be served.
    port : int (default = 8888)
        the port at which to serve the HTML
    n_retries : int (default = 50)
        the number of nearby ports to search if the specified port is in use.
    files : dictionary (optional)
        dictionary of extra content to serve
    ipython_warning : bool (optional)
        if True (default), then print a warning if this is used within IPython
    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.
    Nz.Serving to http://{0}:{1}/    [Ctrl-C to exit]c                  s   t d S )Nzhttp://{0}:{1})
webbrowseropenformatr   )r)   r   r   r   <lambda>w       zserve.<locals>.<lambda>)targetz
stopping Server...)r-   r   r   Z
HTTPServerZ__IPYTHON__printIPYTHON_WARNINGr2   sysstdoutflush	threadingThreadstartserve_foreverKeyboardInterrupt
SystemExitserver_close)r   r)   r   Z	n_retriesr   Zipython_warningZopen_browserZhttp_serverHandlerZsrvrbr   )r)   r   r   serveH   s*    

rD   )N)r   )r.   r/   r   NTTN)__doc__r8   r;   r0   r#   r   r!   r7   BaseHTTPServerr   ImportErrorhttpr   r-   rD   r   r   r   r   <module>   s   

 