o
    h                     @   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dlmZ ddl	m
Z dZG dd dZG dd	 d	Zdd
dZdddZ							d	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	   G/var/www/vscode/kcb/lib/python3.10/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   ip_portHandlerr	   r	   r
   __init__$      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)pathsend_responsesend_headerend_headerswfilewriteencode
send_error)r   content_typecontentfileshtmlr	   r
   do_GET3   s   



z*generate_handler.<locals>.MyHandler.do_GETN)r   r   r   r+   r	   r(   r	   r
   	MyHandler2   s    r,   )r   BaseHTTPRequestHandler)r*   r)   r,   r	   r(   r
   generate_handler.   s   r.   2   c              	      s~   t  fddt|D  td| d|  }|D ] ttjtj}||  f}|	  |dkr8   S qd}t
|)z*Find an open port near the specified port.c                 3   s    | ]} | V  qd S r   r	   ).0iportr	   r
   	<genexpr>I   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)ipr3   nportssresultmsgr	   r2   r
   find_open_portF   s   *rH   	127.0.0.1"  Treturnc              	      s   t  |t| |}|du rt f|}	n| f|}	|r3zt W n	 ty.   Y nw tt td  d d tj	
  |rT fdd}
tj|
d  z|	  W n ttfyi   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://:z/    [Ctrl-C to exit]c                      s   t d  d S )Nzhttp://rL   )
webbrowseropenr	   rB   r3   r	   r
   b   r   zserve.<locals>.b)targetz
stopping Server...)rH   r.   r   
HTTPServer__IPYTHON__	NameErrorprintJUPYTER_WARNINGsysstdoutflush	threadingThreadstartr   KeyboardInterrupt
SystemExitr   )r*   rB   r3   	n_retriesr)   jupyter_warningopen_browserhttp_serverr   srvrrP   r	   rO   r
   serveV   s.   !

rd   r   )r/   )rI   rJ   r/   NTTN)rK   N)__doc__r7   r:   r<   rW   rZ   rM   httpr   ior   r   rV   r   r   r.   rH   rd   r	   r	   r	   r
   <module>   s0    


	