Ë
    úmxi  ã                   ó¦   — 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      úJ/home/htdocs/ttos/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   € Ù”“˜w q™z¨4Õ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¿   ø€ à�y‰y˜CÒØ×"Ñ" 3Ô'Ø× Ñ  °Ô=Ø× Ñ Ô"Ø—
‘
× Ñ  §¡£Õ/Ø—‘˜eÑ#Ø(-¨d¯i©iÑ(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   øè ø€ Ò$�aˆ��Ñ$ùs   ƒéþÿÿÿé   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š   ø€ ä�O‰OÛ$œ5 ›8Ô$ t¬f¯n©n¸RÀ!¹VÀQÈÁUÓ.KÑ'Kó€Eð ò ˆÜ�M‰Mœ&Ÿ.™.¬&×*<Ñ*<Ó=ˆØ—‘˜r 4˜jÓ)ˆØ	�‰Œ	Ø�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   ø€ Ü—?‘? W¨R¨D°°$°Ð#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 ˜"˜d IÓ.€DÜ˜t UÓ+€GàÐÜ× Ñ  " d ¨WÓ5‰á˜B ˜: wÓ/ˆáð	#Ýô ”/Ô"ô 
Ð˜r˜d ! D 6Ð)>Ð
?Ô@Ü‡J�J×ÑÔáõ	:ô 	×Ñ Ô"×(Ñ(Ô*ð&Ø×ÑÔð 	×ÑÕøô+ ò 	Ùð	ûô$ œ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   