
    g                         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      Z G d d	      Zdd
ZddZ	 	 	 	 	 	 	 d	 ddZy)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y)MockRequestc                     t        d      S )Ns   GET /)IO)selfargskwargss      H/var/www/openai/venv/lib/python3.12/site-packages/altair/utils/server.pymakefilezMockRequest.makefile   s    (|    c                      y N )r	   responses     r   sendallzMockRequest.sendall       r   N)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   r   c                       e Zd Zd Zd Zd Zy)
MockServerc                 .     |t               |d   |        y )Nr   )r   )r	   ip_portHandlers      r   __init__zMockServer.__init__$   s    wqz40r   c                      y r   r   r	   s    r   serve_foreverzMockServer.serve_forever'   r   r   c                      y r   r   r   s    r   server_closezMockServer.server_close*   r   r   N)r   r   r   r   r    r"   r   r   r   r   r   #   s    1r   r   c                 J     i  G  fddt         j                        }|S )Nc                       e Zd Z fdZy)#generate_handler.<locals>.MyHandlerc                    | j                   dk(  r]| j                  d       | j                  dd       | j                          | j                  j                  j                                y| j                   v ro| j                      \  }}| j                  d       | j                  d|       | j                          | j                  j                  |j                                y| j                  d       y)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_GET3   s    yyC""3'  =  "

  /e#(-dii(8%g""3'  >  "

  !12$r   N)r   r   r   r5   )r3   r4   s   r   	MyHandlerr%   2   s    	%r   r6   )r   BaseHTTPRequestHandler)r4   r3   r6   s   `` r   generate_handlerr8   .   s'    }%F11 %" r   c           	      r   t        j                  fdt        |      D        t        j                  d|z  d|z        z         }|D ]`  t        j
                  t
        j                  t
        j                        }|j                  | f      }|j                          |dk7  s^c S  d}t        |      )z*Find an open port near the specified port.c              3   (   K   | ]	  }|z     y wr   r   ).0iports     r   	<genexpr>z!find_open_port.<locals>.<genexpr>I   s     $8a8s      r   zno open ports found)	itertoolschainrangerandomrandintsocketAF_INETSOCK_STREAM
connect_exclose
ValueError)ipr=   nportssresultmsgs    `     r   find_open_portrR   F   s    OO$58$tfnnR!VQU.K'KE MM&..&*<*<=r4j)		Q;K   C
S/r   c                    t        |      t        | |      }|t        j                  f|      }	n |f|      }	|r	 t         t        t               t        d d d       t        j                  j                          |r*fd}
t        j                  |
      j                          	 |	j                          |	j#                          y# t        $ r Y w xY w# t        t         f$ r t        d       Y <w xY w)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                  :    t        j                  d  d       S )Nzhttp://rT   )
webbrowseropen)rL   r=   s   r   bzserve.<locals>.b   s    ??WRD$#899r   )targetz
stopping Server...)rR   r8   r   
HTTPServer__IPYTHON__printJUPYTER_WARNING	NameErrorsysstdoutflush	threadingThreadstartr    KeyboardInterrupt
SystemExitr"   )r4   rL   r=   	n_retriesr3   jupyter_warningopen_browserhttp_serverr   srvrrX   s    ``        r   serverl   V   s    B "dI.DtU+G  "dW5B:w/	# /" 
rd!D6)>
?@JJ	: 	"((*& 	+  		$ z* &$%&s$   C 7C' 	C$#C$'DDr   )2   )z	127.0.0.1i"  rm   NTTN)returnN)__doc__rA   rD   rF   r_   rb   rV   httpr   ior   r   r]   r   r   r8   rR   rl   r   r   r   <module>rr      sq       
      0$ 	
A 
Ar   