o
    e                     @   s   d 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	 ddl
Z
ddlZdZG dd dZG dd	 d	Zdd
dZdddZ							dddZdS )z
A Simple server used to show altair graphics from a prompt or script.

This is adapted from the mpld3 package; see
https://github.com/mpld3/mpld3/blob/master/mpld3/_server.py
    N)server)BytesIOz
Note: if you're in the Jupyter notebook, Chart.serve() is not the best
      way to view plots. Consider using Chart.display().
You must interrupt the kernel to cancel this command.
c                   @   s   e Zd Zdd Zdd ZdS )MockRequestc                 O   s   t dS )Ns   GET /)IO)selfargskwargs r	   CD:\Projects\ConvertPro\env\Lib\site-packages\altair/utils/server.pymakefile   s   zMockRequest.makefilec                 C      d S Nr	   )r   responser	   r	   r
   sendall      zMockRequest.sendallN)__name__
__module____qualname__r   r   r	   r	   r	   r
   r      s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )
MockServerc                 C   s   |t  |d |  d S )Nr   )r   )r   Zip_portHandlerr	   r	   r
   __init__#   s   zMockServer.__init__c                 C   r   r   r	   r   r	   r	   r
   serve_forever&   r   zMockServer.serve_foreverc                 C   r   r   r	   r   r	   r	   r
   server_close)   r   zMockServer.server_closeN)r   r   r   r   r   r   r	   r	   r	   r
   r   "   s    r   c                    s(    d u 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r| d | dd |   | j  dS | j  v rC | j  \}}| d | d| |   | j|  dS | d dS )zRespond to a GET request./   zContent-typez	text/htmli  N)pathZsend_responseZsend_headerZend_headerswfilewriteencodeZ
send_error)r   content_typecontentfileshtmlr	   r
   do_GET2   s   



z*generate_handler.<locals>.MyHandler.do_GETN)r   r   r   r%   r	   r"   r	   r
   	MyHandler1   s    r&   )r   ZBaseHTTPRequestHandler)r$   r#   r&   r	   r"   r
   generate_handler-   s   r'   2   c              	      sz   t  fddt|D  td| d|  }|D ] ttjtj}||  f}|	  |dkr8   S qt
d)z)Find an open port near the specified portc                 3   s    | ]} | V  qd S r   r	   ).0iportr	   r
   	<genexpr>H   s    z!find_open_port.<locals>.<genexpr>   r   zno open ports found)	itertoolschainrangerandomrandintsocketAF_INETSOCK_STREAM
connect_exclose
ValueError)ipr,   nZportssresultr	   r+   r
   find_open_portE   s   *r?   	127.0.0.1"  Tc              	      s   t  |t| |}|du rt f|}	n| f|}	|r3zt W n	 ty.   Y nw tt td  t	j
  |rQ fdd}
tj|
d  z|	  W n ttfyf   td Y nw |	  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
    jupyter_warning : bool (optional)
        if True (default), then print a warning if this is used within Jupyter
    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://{}:{}/    [Ctrl-C to exit]c                      s   t d S )Nzhttp://{}:{})
webbrowseropenformatr	   r;   r,   r	   r
   b   s   zserve.<locals>.b)targetz
stopping Server...)r?   r'   r   Z
HTTPServerZ__IPYTHON__	NameErrorprintJUPYTER_WARNINGrD   sysstdoutflush	threadingThreadstartr   KeyboardInterrupt
SystemExitr   )r$   r;   r,   Z	n_retriesr#   Zjupyter_warningZopen_browserZhttp_serverr   ZsrvrrF   r	   rE   r
   serveT   s.    

rS   r   )r(   )r@   rA   r(   NTTN)__doc__rK   rN   rB   r5   httpr   ior   r   r0   r3   rJ   r   r   r'   r?   rS   r	   r	   r	   r
   <module>   s,    


