a
    d                     @   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	   \/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/altair/utils/server.pymakefile   s    zMockRequest.makefilec                 C   s   d S Nr	   )r   responser	   r	   r
   sendall   s    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   s   d S r   r	   r   r	   r	   r
   serve_forever&   s    zMockServer.serve_foreverc                 C   s   d S r   r	   r   r	   r	   r
   server_close)   s    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  nR| j  v r | j  \}}| d | d| |   | j|  n
| 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              	      s~   t  fddt|D  td| d|  }|D ]: ttjtj}||  f}|	  |dkr6   S q6t
ddS )z)Find an open port near the specified portc                 3   s   | ]} | V  qd S r   r	   ).0iportr	   r
   	<genexpr>H       z!find_open_port.<locals>.<genexpr>   r   zno open ports foundN)	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 r0t f|}	n| f|}	|rfzt W n ty\   Y n
0 tt td  t	j
  |r fdd}
tj|
d  z|	  W n ttfy   td Y n0 |	  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_WARNINGrC   sysstdoutflush	threadingThreadstartr   KeyboardInterrupt
SystemExitr   )r"   r:   r*   Z	n_retriesr!   Zjupyter_warningZopen_browserZhttp_serverr   ZsrvrrE   r	   rD   r
   serveT   s*     

rR   )N)r&   )r?   r@   r&   NTTN)__doc__rJ   rM   rA   r4   httpr   ior   r   r/   r2   rI   r   r   r%   r>   rR   r	   r	   r	   r
   <module>   s*   


       