o
    dC                     @   s  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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 z?d dlmZ d dlmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dl Z d dl!Z!eddZ"dZ#W n e$y   dZ#dZ"dd ZY nw ed Z%de&fddZ'e(dd dd dd fddZ)i Z*i Z+dd Z,dd Z-d#dd Z.G d!d" d"Z/e/ Z0dS )$    N)Literal)make_server)get_ipython)IFramedisplay
Javascript)HTML)FormattedTB)retry)Ansi2HTMLConverter)Commdash)target_nameTFc                   C   s   d S N r   r   r   R/var/www/html/visualizacion-main/env/lib/python3.10/site-packages/dash/_jupyter.py<lambda>$   s    r   )inlineexternal
jupyterlabtab_noneerrorc                 C   sD   t t| | | j}d}t|D ]\}}d|v r|d } |S q|S )Nr   z%% callback invoked %%   )	tracebackformat_exceptiontype__traceback__	enumerate)r   tbskipiliner   r   r   	_get_skip)   s   r#   c                 C      d|  S )N*r   namer   r   r   r   9       c                 C   r$   )Nz**r   r&   r   r   r   r   :   r(   c                 C   s   dt |  S )N=)repr)valuer   r   r   r   ;   s    c                 C   s   |||fdd}g }	t t| D ]}
|	|| |
  q|r+|	|||||   |r:|	|||||   dd|	 d }t|dk rK|S dd|	 d	 S )
z_Copied from inspect.formatargvalues, modified to place function
    arguments on separate linesc                 S   s   || |||   S r   r   )r'   locals	formatargformatvaluer   r   r   convertB      z(_custom_formatargvalues.<locals>.convert(z, )(   z(
    z,
    z
))rangelenappendjoin)argsvarargsvarkwr,   r-   formatvarargsformatvarkwr.   r/   specsr!   resultr   r   r   _custom_formatargvalues3   s   r?   c                   C   s<   t  d urtjd urtj td< tddi d S d S d S )Nparentr   base_url_request)r   
_dash_commkernel
get_parent_callersendr   r   r   r   !_send_jupyter_config_comm_request\   s   

rG   c                   C   s   t tS r   )bool_jupyter_configr   r   r   r   _jupyter_comm_response_receivede   s   rJ      c           	         s   t jd u rd S t  t }|j}g   fdd}||jd< | jd7  _t }	 t | | kr5tdt r9nt	
|jrPt	 }t| ||  n|  q)|j|jd< tj  tj   D ]\}}}||| |||| qgd S )Nc                    s     | ||f d S r   )r6   )streamidentr@   captured_eventsr   r   capture_eventx   r0   z._request_jupyter_config.<locals>.capture_eventexecute_requestr   TzvUnable to communicate with the jupyter_dash notebook or JupyterLab 
extension required to infer Jupyter configuration.)rB   rC   rG   r   shell_handlersexecution_counttimeEnvironmentErrorrJ   asyncioiscoroutinefunctiondo_one_iterationget_event_loopnest_asyncioapplyrun_until_completerQ   sysstdoutflushstderr
set_parent)	timeoutshellrC   rP   t0looprL   rM   r@   r   rN   r   _request_jupyter_configi   s<   




rf   c                   @   s   e Zd ZU dZdZeed< ee	 Z
dZeed< i Zdd Zdd	 Z	
					
ddefddZedd Zedd Zedd Zdd Zedd Zd
S )JupyterDashz;
    Interact with dash apps inside jupyter notebooks.
    r   default_modeTinline_exceptionsc                 C   s   | j r| jrdS t  dS )ar  
        Infer the current Jupyter server configuration. This will detect
        the proper request_pathname_prefix and server_url values to use when
        displaying Dash apps.Dash requests will be routed through the proxy.

        Requirements:

        In the classic notebook, this method requires the `dash` nbextension
        which should be installed automatically with the installation of the
        jupyter-dash Python package. You can see what notebook extensions are installed
        by running the following command:
            $ jupyter nbextension list

        In JupyterLab, this method requires the `@plotly/dash-jupyterlab` labextension. This
        extension should be installed automatically with the installation of the
        jupyter-dash Python package, but JupyterLab must be allowed to rebuild before
        the extension is activated (JupyterLab should automatically detect the
        extension and produce a popup dialog asking for permission to rebuild). You can
        see what JupyterLab extensions are installed by running the following command:
            $ jupyter labextension list
        N)
in_ipythonin_colabrf   )selfr   r   r   infer_jupyter_proxy_config   s   
z&JupyterDash.infer_jupyter_proxy_configc                 C   sD   t  d u| _dtjv | _tr| jrtr tjdd }d S d S d S d S )Nzgoogle.colabc                 S   sp   t d}|r|tj krtj|d d g| t d= | dd}|dd }|dkr6t| d S d S )Nr@   headersessioncontentdatar   base_url_response)rE   getrB   rC   rD   ra   rI   update)msgprev_parentmsg_datamsg_typer   r   r   _receive_message   s   
z.JupyterDash.__init__.<locals>._receive_message)r   rj   r]   modulesrk   _dep_installedrB   on_msg)rl   ry   r   r   r   __init__   s   zJupyterDash.__init__N100%  	127.0.0.1r  modec              
      s  | j rddg}ng d}|du r| j}n%t|ts'tdt| dt| | }||vr9td| d| | j	f}	|	rM|	
  | jf= d	tv r[td	 d
d }
n|j	dd}
|
durm|
jd}
nd
}
t|jd|
 |du rdtv rtd d
}ntj	dd}|rd| }nd d }n|d
}| |
 }zddl}W n	 ty   Y nw t t|jdddtdtj tddddfdd}tj|d}d|_ |!  | jf< d d dt"j# fdd  tdd!dd fd"d#}z|  | j r(t"$|||| W dS t"%|||| W dS  t&yZ } zt|}|'d$rMt(t)| n|W Y d}~dS d}~ww )%a  
        :type app: dash.Dash
        :param mode: How to display the app on the notebook. One Of:
            ``"external"``: The URL of the app will be displayed in the notebook
                output cell. Clicking this URL will open the app in the default
                web browser.
            ``"inline"``: The app will be displayed inline in the notebook output cell
                in an iframe.
            ``"jupyterlab"``: The app will be displayed in a dedicate tab in the
                JupyterLab interface. Requires JupyterLab and the `jupyterlab-dash`
                extension.
        :param width: Width of app when displayed using mode="inline"
        :param height: Height of app when displayed using mode="inline"
        :param host: Host of the server
        :param port: Port used by the server
        :param server_url: Use if a custom url is required to display the app.
        r   r   )r   r   r   r   r   Nz>The mode argument must be a string
    Received value of type z: zInvalid display argument z
    Valid arguments: base_subpath/z/proxy/{port}/requests_pathname_prefix)port
server_urlDASH_DOMAIN_BASEzhttps://http://:r   T)threaded	processeswerkzeug   d   i  )stop_max_attempt_numberwait_exponential_multiplierwait_exponential_maxc               
      sH   z   W d S  ty   Y d S  ty# }  z |  | d } ~ ww r   )serve_forever
SystemExit	Exceptionput)r   )err_qserverr   r   runL  s   
z JupyterDash.run_app.<locals>.run)targetz/_alive_c                     s.   z   } | r	| W d S  tjy   Y d S w r   )
get_nowaitqueueEmpty)err)r   r   r   
_get_errorc  s   z'JupyterDash.run_app.<locals>._get_error
   c               
      s      z*t } | j }| jdkrt||dkr+d d }td| dW d S  t jy? } z   |d }~ww )N   Aliver   r   z	Address 'zA' already in use.
    Try passing a different port to run_server.)requestsrs   rp   decodestatus_coder   OSErrorConnectionError)reqresurlr   )r   	alive_urlhostr   r   r   wait_for_appl  s"   



z)JupyterDash.run_app.<locals>.wait_for_appz<!)*rk   rh   
isinstancestr
ValueErrorr   r*   lower_serversrs   shutdownrI   rstripconfigformatdict__setitem__osenvironorjsonImportErrorr   Queuer   r   logging	getLoggersetLevelERRORr
   	threadingThreaddaemonstartrg   alive_token_display_in_colab_display_in_jupyterr   
startswithr   r   )rl   appr   widthheightr   r   r   valid_display_values
old_serverr   domain_basedashboard_urlr   r   threadr   final_errorru   r   )r   r   r   r   r   r   r   run_app   s   



		

zJupyterDash.run_appc                 C   sN   ddl m} |dkr|j|||d d S |dkr%td |j|| d d S d S )Nr   )outputr   )r   r   r   zDash app running on:)anchor_text)google.colabr   serve_kernel_port_as_iframeprintserve_kernel_port_as_window)r   r   r   r   r   r   r   r   r   r     s   zJupyterDash._display_in_colabc                 C   sx   |dkrt t| || d S |dv r+td|   |dkr)t td|  d d S d S |dkr:td|| d	 d S d S )
Nr   )r   r   zDash app running on r   zwindow.open('z')r   show)r   r   r   )r   r   r   r   rB   rF   )r   r   r   r   r   r   r   r   r     s   zJupyterDash._display_in_jupyterc                   C   s   dS )Nr   r   r   r   r   r   serve_alive  s   zJupyterDash.serve_alivec                    s   |j t fdd}dS )z(Install traceback handling for callbacksc                    s    rt | nd}tj}tt_zt }t|ddd|d}|  W |t_n|t_w | }jr3t	| t
ddd}||}|d	d
}tdd|}|dfS )Nr   VerboseLinuxT)	tb_offsetr   color_schemeinclude_varsostream	ansi2htmlF)schemedark_bgz<html>z-<html style="width: 75ch; font-size: 0.86em">zbackground-color:[^;]+; i  )r#   inspectformatargvaluesr?   ioStringIOr	   getvalueri   r   r   r/   replaceresub)r   r    original_formatargvaluesr   ipytbansi_stacktraceconvhtml_strdev_tools_prune_errorsrl   r   r   _wrap_errors  s0   
zGJupyterDash.configure_callback_exception_handling.<locals>._wrap_errorsN)r   errorhandlerr   )rl   r   r   r   r   r   r   %configure_callback_exception_handling  s   
z1JupyterDash.configure_callback_exception_handlingc                 C   s    dt jv }to| o| jp| jS )NDATABRICKS_RUNTIME_VERSION)r   r   r{   rj   rk   )rl   _inside_dbxr   r   r   active  s   
zJupyterDash.active)Nr~   r   r   r   N)__name__
__module____qualname____doc__rh   JupyterDisplayMode__annotations__r   uuiduuid4r   ri   rH   r   rm   r}   r   staticmethodr   r   r   r   propertyr   r   r   r   r   rg      s6   
 
 3


2rg   )rK   )1rV   r   r   r   r   r   r  r   r]   r   rT   r   typing_extensionsr   werkzeug.servingr   IPythonr   IPython.displayr   r   r   IPython.core.displayr   IPython.core.ultratbr	   retryingr
   r   r   ipykernel.commr   rZ   r   rB   r{   r   r   r   r#   r   r?   rI   rE   rG   rJ   rf   rg   jupyter_dashr   r   r   r   <module>   s^    

$	
<  
M