
    d                         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          Z G d d	          Zdd
ZddZ	 	 	 	 	 	 	 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                       e Zd Zd Zd ZdS )MockRequestc                      t          d          S )Ns   GET /)IO)selfargskwargss      3lib/python3.11/site-packages/altair/utils/server.pymakefilezMockRequest.makefile   s    (||    c                     d S N )r	   responses     r   sendallzMockRequest.sendall       r   N)__name__
__module____qualname__r   r   r   r   r   r   r      s2              r   r   c                        e Zd Zd Zd Zd ZdS )
MockServerc                 D     |t                      |d         |            d S )Nr   )r   )r	   ip_portHandlers      r   __init__zMockServer.__init__#   s$    wqz400000r   c                     d S r   r   r	   s    r   serve_foreverzMockServer.serve_forever&   r   r   c                     d S r   r   r   s    r   server_closezMockServer.server_close)   r   r   N)r   r   r   r   r    r"   r   r   r   r   r   "   sA        1 1 1      r   r   c                 H     i  G  fddt           j                  }|S )Nc                       e Zd Z fdZdS )#generate_handler.<locals>.MyHandlerc                 .   | j         dk    rm|                     d           |                     dd           |                                  | j                                                                       dS | j         v r}| 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htmls      r   do_GETz*generate_handler.<locals>.MyHandler.do_GET2   s   yC""3'''  ===  """
  /////e##(-di(8%g""3'''  >>>  """
  !1!122222$$$$$r   N)r   r   r   r5   )r3   r4   s   r   	MyHandlerr%   1   s3        	% 	% 	% 	% 	% 	% 	% 	%r   r6   )r   BaseHTTPRequestHandler)r4   r3   r6   s   `` r   generate_handlerr8   -   sR    }% % % % % % % %F1 % % %" r   2   c           	      |   t          j        fdt          |          D             t          j        d|z  d|z            z             }|D ]`t          j        t
          j        t
          j                  }|                    | f          }|	                                 |dk    rc S at          d          )z)Find an open port near the specified portc              3   "   K   | ]	}|z   V  
d S r   r   ).0iports     r   	<genexpr>z!find_open_port.<locals>.<genexpr>H   s'      $$a$$$$$$r      r   zno open ports found)	itertoolschainrangerandomrandintsocketAF_INETSOCK_STREAM
connect_exclose
ValueError)ipr>   nportssresults    `    r   find_open_portrR   E   s    O$$$$588$$$tfnR!VQU.K.K'K E   M&.&*<==r4j))				Q;;KKK 
*
+
++r   	127.0.0.1"  Tc                 z   t          |          t          | |          }|t          j        f|          }	n |f|          }	|r-	 t           t          t                     n# t          $ r Y nw xY wt          d                                         t          j
                                         |r-fd}
t          j        |
                                           	 |	                                 n&# t           t"          f$ r t          d           Y nw xY 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                  T    t          j        d                                         S )Nzhttp://{}:{})
webbrowseropenformat)rM   r>   s   r   bzserve.<locals>.b   s#    ?>#8#8T#B#BCCCr   )targetz
stopping Server...)rR   r8   r   
HTTPServer__IPYTHON__printJUPYTER_WARNING	NameErrorrY   sysstdoutflush	threadingThreadstartr    KeyboardInterrupt
SystemExitr"   )r4   rM   r>   	n_retriesr3   jupyter_warningopen_browserhttp_serverr   srvrrZ   s    ``        r   servern   T   s   @ "dI..DtU++G "dW55{B:w// #	#K /""""  	 	 	D	 

8
?
?D
I
IJJJJ +	D 	D 	D 	D 	D 	D 	"""((***&z* & & &$%%%%%& 	s$   A+ +
A87A8,D  D$#D$r   )r9   )rS   rT   r9   NTTN)__doc__ra   rd   rW   rG   httpr   ior   r   rB   rE   r_   r   r   r8   rR   rn   r   r   r   <module>rr      s.    


                                           0, , , ," 	
@ @ @ @ @ @r   