
    $gYA                     ~   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 d dlm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Zd dlZ ed      Z dZ!ed   Z#de$fdZ%e&d d d fdZ'i Z(i Z)d Z*d Z+ddZ, G d d      Z- e-       Z.y# e"$ r
 dZ!dZ d ZY Dw xY w)    N)Optional)Literal)make_server)get_ipython)IFramedisplay
Javascript)HTML)FormattedTB)retry)Commdash)target_nameTFc                       y N r       J/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/dash/_jupyter.py<lambda>r   "   s    r   )inlineexternal
jupyterlabtab_noneerrorc                     ddl m} | j                  }d}|j                  B|dz  }|j                  }|j                  j
                  |j                  u r|S |j                  B|S )Nr   )_invoke_callback   )dash._callbackr   __traceback__tb_nexttb_framef_code__code__)r   r   tbskips       r   	_get_skipr'   '   sf     
		BD
**
 	ZZ;;!1!:!::K	 **
  Kr   c                     d| z   S )N*r   names    r   r   r   =   s
    sTz r   c                     d| z   S )Nz**r   r*   s    r   r   r   >   s
    TD[ r   c                     dt        |       z   S )N=)repr)values    r   r   r   ?   s    cDK/ r   c                    |||fd}g }	t        t        |             D ]  }
|	j                   || |
                 |r#|	j                   ||       |||         z          |r#|	j                   ||       |||         z          ddj                  |	      z   dz   }t        |      dk  r|S ddj                  |	      z   dz   S )	z_Copied from inspect.formatargvalues, modified to place function
    arguments on separate linesc                 *     ||        |||          z   S r   r   )r+   locals	formatargformatvalues       r   convertz(_custom_formatargvalues.<locals>.convertF   s    VD\!:::r   (z, )(   z(
    z,
    z
))rangelenappendjoin)argsvarargsvarkwr3   r4   formatvarargsformatvarkwr5   r6   specsiresults               r   _custom_formatargvaluesrF   7   s     $yk ; E 3t9 'WT!W%&']7+k&/.JJK['+fUm*DDE499U##c)F
6{Ry~~e,,u44r   c                      t               Nt        j                  =t        j                  j                         t        d<   t        j                  ddi       y y y )Nparenttypebase_url_request)r   
_dash_commkernel
get_parent_callersendr   r   r   !_send_jupyter_config_comm_requestrP   `   sM     } ( * 1 1 < < >GHOOV%789 ) !r   c                       t        t              S r   )bool_jupyter_configr   r   r   _jupyter_comm_response_receivedrT   i   s      r   c                 D  	 t         j                  y t                t               }|j                  }g 		fd}||j                  d<   |xj
                  dz  c_        t        j                         }	 t        j                         |z
  | kD  rt        d      t               rnyt        j                  |j                        rIt        j                         }t        j                  |       |j                  |j                                n|j                          |j                   |j                  d<   t"        j$                  j'                          t"        j(                  j'                          	D ]+  \  }}}|j+                  ||       |j!                  |||       - y )Nc                 .    j                  | ||f       y r   )r<   )streamidentrH   captured_eventss      r   capture_eventz._request_jupyter_config.<locals>.capture_event|   s    v67r   execute_requestr   zvUnable to communicate with the jupyter_dash notebook or JupyterLab 
extension required to infer Jupyter configuration.)rK   rL   rP   r   shell_handlersexecution_counttimeEnvironmentErrorrT   asyncioiscoroutinefunctiondo_one_iterationget_event_loopnest_asyncioapplyrun_until_completer[   sysstdoutflushstderr
set_parent)
timeoutshellrL   rZ   t0looprW   rX   rH   rY   s
            @r   _request_jupyter_configrp   m   sj    %' ME\\F O8 0=F+, 
Q 
B
IIK"'"E  +,&&v'>'>?))+Dt$##F$;$;$=>##% & 06/E/EF+,
 JJJJ!0 6v 	%(vuf5	6r   c                       e Zd ZU dZdZeed<    e ej                               Z
dZeed<   i Zd Zd Z	 	 	 	 	 	 dd	ee   fd
Zed        Zed        Zed        Zd Zed        Zy)JupyterDashz;
    Interact with dash apps inside jupyter notebooks.
    r   default_modeTinline_exceptionsc                 J    | j                   r| j                  ryt                y)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_colabrp   )selfs    r   infer_jupyter_proxy_configz&JupyterDash.infer_jupyter_proxy_config   s    , $--!r   c                     t               d u| _        dt        j                  v | _        t
        r+| j                  rt        rt        j                  d        }y y y y )Nzgoogle.colabc                 j   t         j                  d      }|rO|t        j                  j	                         k7  r.t        j                  j                  |d   d   g|       t         d= | j                  d      j                  d      }|j                  dd       }|dk(  rt        j                  |       y y )NrH   headersessioncontentdatarI   base_url_response)rN   getrK   rL   rM   rk   rS   update)msgprev_parentmsg_datamsg_types       r   _receive_messagez.JupyterDash.__init__.<locals>._receive_message   s    %kk(3;*2C2C2N2N2P#P%%00$X.y9:K  )779-11&9#<<522#**84 3r   )r   rv   rg   modulesrw   _dep_installedrK   on_msg)rx   r   s     r   __init__zJupyterDash.__init__   sL    %-t3&#++5doo*5 5 3=o>r   Nmodec                 l   | j                   rddg}ng d}|| j                  }nXt        |t              s#t	        dt        |       dt        |             |j                         }||vrt	        d| d|       | j                  j                  f      }	|	r|	j                          | j                  f= d	t        v rt        d	   j                  d
      dz   }
n|j                  j                  dd      }
|
|
j                        }
nd
}
t        j!                  |j                  d|
       |Rdt        v rt        d   j                  d
      }nBt"        j$                  j                  dd      }|rd|z   }nd d }n|j                  d
      }| |
 }	 ddl}t+        j,                         t/        |j0                  dd      t3        j4                  d      j7                  t2        j8                         t;        ddd      fd       }t=        j>                  |      }d|_         |jC                          | j                  f<   d d dtD        jF                   fdt;        ddd      fd        }	  |        | j                   rtD        jI                  ||||       ytD        jK                  ||||       y# t(        $ r Y @w xY w# tL        $ r=}t        |      }|jO                  d!      rtQ        tS        |             n|Y d}~yd}~ww xY 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                      	 j                          y # t        $ r Y y t        $ r} j                  |        | d } ~ ww xY wr   )serve_forever
SystemExit	Exceptionput)r   err_qservers    r   runz JupyterDash.run_app.<locals>.runP  s@    $$&  		% s    	??:?)targetz/_alive_c                  b    	 j                         } | r| y # t        j                  $ r Y y w xY wr   )
get_nowaitqueueEmpty)errr   s    r   
_get_errorz'JupyterDash.run_app.<locals>._get_errorg  s9    &&(I ;; s    ..
   c                  (            	 t        j                        } | j                  j                         }| j                  dk7  rt        |      |dk7  rd d }t        d| d      y # t         j                  $ r}         |d }~ww xY w)N   Aliver   r   z	Address 'zA' already in use.
    Try passing a different port to run_server.)requestsr   r~   decodestatus_coder   OSErrorConnectionError)reqresurlr   r   	alive_urlhostr   s       r   wait_for_appz)JupyterDash.run_app.<locals>.wait_for_appp  s     Lll9-kk((*??c)#C.('>#D64&1C!#C5 )J J  " ++ 	s   A%A0 0B	BBz<!)*rw   rs   
isinstancestr
ValueErrorrI   r/   lower_serversr   shutdownrS   rstripconfigformatdict__setitem__osenvironorjsonImportErrorr   Queuer   r   logging	getLoggersetLevelERRORr   	threadingThreaddaemonstartrr   alive_token_display_in_colab_display_in_jupyterr   
startswithr   r
   )rx   appr   widthheightr   r   r   valid_display_values
old_serverr   domain_basedashboard_urlr   r   threadr   final_errorr   r   r   r   r   s        ``            @@@@r   run_appzJupyterDash.run_app   sH   8 ==$,j#9 #W <$$DD#&..24j\DJ<I 
 ::<D// /v 6,,@+AC  ]]&&d|4
!tTl+ _,/66s;>NN % (+zz~~6PRV'W$#/'?'F'FD'F'Q$'*$ 	JJ24L	

 .,\:AA#F
 jjnn-?F!+k!9J#*4&$!8J#**3/J &,'?&@A
	 T4daP*%..w}}=	$&(+!%


	


	 !!-&,tTl# dV1TF(;3J3J2KL		 
$&(*!%


	


	$	"N}}--mT4PVW//!4ufK  		P  	"k"C~~d#S	"!! #	"s0   %K ,K- K- 	K*)K*-	L363L..L3c                     ddl m} |dk(  r|j                  |||       y |dk(  rt        d       |j	                  ||        y y )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   s         r   r   zJupyterDash._display_in_colab  sO     	(8..t5.PZ ()..t.O	  r   c                     |dk(  rt        t        | ||             y |dv r-t        d|         |dk(  rt        t        d|  d             y y |dk(  rt        j                  d|| d	       y y )
Nr   )r   r   zDash app running on r   zwindow.open('z')r   show)rI   r   r   )r   r   r   r	   rK   rO   )r   r   r   r   r   s        r   r   zJupyterDash._display_in_jupyter  s    8F=%89(((89u}
]=/#DEF \! OO" ( "r   c                       y)Nr   r   r   r   r   serve_alivezJupyterDash.serve_alive  s    r   c                 X     |j                   j                  t               fd       }y)z(Install traceback handling for callbacksc                 D   rt        |       nd}t        j                  }t        t        _        	 t	        j
                         }t        |ddd|      } |        |t        _        |j                         }j                  rt        |       |dfS # |t        _        w xY w)Nr   VerboseNoColorT)	tb_offsetr   color_schemeinclude_varsostreami  )
r'   inspectformatargvaluesrF   ioStringIOr   getvaluert   r   )r   r&   original_formatargvaluesr   ipytb
stacktracedev_tools_prune_errorsrx   s         r   _wrap_errorszGJupyterDash.configure_callback_exception_handling.<locals>._wrap_errors  s     (>9U#1D (/'>'>$&=G#C++-#""!*!%#  +C' ))+J%%j!s?" +C's   +B BN)r   errorhandlerr   )rx   r   r   r   s   ` ` r   %configure_callback_exception_handlingz1JupyterDash.configure_callback_exception_handling  s&     
	 	 	+	# 
,	#r   c                 t    dt         j                  v }t        xr | xr | j                  xs | j                  S )NDATABRICKS_RUNTIME_VERSION)r   r   r   rv   rw   )rx   _inside_dbxs     r   activezJupyterDash.active  s/    2bjj@Xk/Xt7W$--Xr   )Nz100%i  z	127.0.0.1ir  N)__name__
__module____qualname____doc__rs   JupyterDisplayMode__annotations__r   uuiduuid4r   rt   rR   r   ry   r   r   r   staticmethodr   r   r   r   propertyr  r   r   r   rr   rr      s     (0L$/jdjjl#K"t"H"850 .2p" )*p"d 
P 
P  &   #D Y Yr   rr   )   )/r`   r   r   r   r   r   r  rg   r   r^   typingr   typing_extensionsr   werkzeug.servingr   IPythonr   IPython.displayr   r   r	   IPython.core.displayr
   IPython.core.ultratbr   retryingr   ipykernel.commr   rd   r   rK   r   r   r	  r   r'   r   rF   rS   rN   rP   rT   rp   rr   jupyter_dashr   r   r   <module>r     s     	   	   
    % (#;;)0#&)JN OP Y * )(/!5H 
:!96xxY xYv	 }K  NJKs   ;B- -B<;B<