
    !jYA                        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!n# e"$ r
 dZ!dZ d ZY nw xY w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.dS )    N)Optional)Literal)make_server)get_ipython)IFramedisplay
Javascript)HTML)FormattedTB)retry)Commdash)target_nameTFc                      d S N r       I/var/www/html/bolsaweb/venv/lib/python3.11/site-packages/dash/_jupyter.py<lambda>r   "   s    $ r   )inlineexternal
jupyterlabtab_noneerrorc                     ddl m} | j        }d}|j        (|dz  }|j        }|j        j        |j        u r|S |j        (|S )Nr   )_invoke_callback   )dash._callbackr   __traceback__tb_nexttb_framef_code__code__)r   r   tbskips       r   	_get_skipr'   '   sq          
	BD
*
 	Z;!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KK/ r   c                    |||fd}g }	t          t          |                     D ]&}
|	                     || |
                              '|r0|	                     ||           |||                   z              |r0|	                     ||           |||                   z              dd                    |	          z   dz   }t          |          dk     r|S dd                    |	          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#    y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 3t99 ' 'WWT!W%%&&&& L]]7++kk&/.J.JJKKK F[[''++fUm*D*DDEEE499U###c)F
6{{Ry~~e,,,u44r   c                      t                      Pt          j        Ft          j                                        t          d<   t                              ddi           d S d S d S )Nparenttypebase_url_request)r   
_dash_commkernel
get_parent_callersendr   r   r   !_send_jupyter_config_comm_requestrP   `   s\     }} ( * 1 < < > >GHOOV%7899999 ! ((r   c                  *    t          t                    S r   )bool_jupyter_configr   r   r   _jupyter_comm_response_receivedrT   i   s       r      c                 @  	 t           j        d S t                       t                      }|j        }g 		fd}||j        d<   |xj        dz  c_        t          j                    }	 t          j                    |z
  | k    rt          d          t                      rn}t          j
        |j                  rOt          j                    }t          j        |           |                    |                                           n|                                 |j        |j        d<   t"          j                                         t"          j                                         	D ]3\  }}}|                    ||           |                    |||           4d S )Nc                 8                         | ||f           d S r   )r<   )streamidentrH   captured_eventss      r   capture_eventz._request_jupyter_config.<locals>.capture_event|   s$    v677777r   execute_requestr   TzvUnable 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   r[   t0looprX   rY   rH   rZ   s
            @r   _request_jupyter_configrq   m   s    %''' MME\F O8 8 8 8 8 0=F+, 
Q 
B&IKK"''"E   +,, 	&v'>?? 	&)++Dt$$$##F$;$;$=$=>>>>##%%%&& 06/EF+,
 JJ!0 6 6v 	%(((vuf5555	6 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dS )JupyterDashz;
    Interact with dash apps inside jupyter notebooks.
    r   default_modeTinline_exceptionsc                 B    | 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_colabrq   )selfs    r   infer_jupyter_proxy_configz&JupyterDash.infer_jupyter_proxy_config   s.    ,  	$- 	F!!!!!r   c                     t                      d u| _        dt          j        v | _        t
          r%| j        r t          rt          j        d             }d S d S d S d S )Nzgoogle.colabc                    t                               d          }|rW|t          j                                        k    r5t          j                            |d         d         g|           t           d= |                     d                              d          }|                    dd           }|dk    rt                              |           d S d S )NrH   headersessioncontentdatarI   base_url_response)rN   getrK   rL   rM   rl   rS   update)msgprev_parentmsg_datamsg_types       r   _receive_messagez.JupyterDash.__init__.<locals>._receive_message   s    %kk(33 *;*2C2N2N2P2P#P#P%00$X.y9:K    )779--11&99#<<55222#**844444 32r   )r   rw   rh   modulesrx   _dep_installedrK   on_msg)ry   r   s     r   __init__zJupyterDash.__init__   s}    %--t3&#+5 	5do 	5* 	55 5 5 5 5	5 	5 	5 	5 	5 	5r   N100%  	127.0.0.1r  modec                    | j         rddg}ng d}|| j        }nqt          |t                    s/t	          dt          |           dt          |                     |                                }||vrt	          d| d|           | j        	                    f          }	|	r|	
                                 | j        f= d	t          v r$t          d	                             d
          dz   }
n|j        	                    dd          }
|
|
                              }
nd
}
t                              |j        d|
           |[dt          v r!t          d                             d
          }nFt"          j        	                    dd          }|rd|z   }nd d }n|                    d
          }| |
 }	 ddl}n# t(          $ r Y nw xY wt+          j                    t/          |j        dd          t3          j        d                              t2          j                   t;          ddd          fd            }t=          j        |          }d|_         |!                                 | j        f<   d d dtD          j#         fdt;          ddd          fd             }	  |             | j         r tD          $                    ||||           dS tD          %                    ||||           dS # tL          $ rN}t          |          }|'                    d!          rtQ          tS          |                     n|Y d}~dS 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                      	                                   d S # t          $ r Y d S t          $ r}                     |            | d } ~ ww xY wr   )serve_forever
SystemExit	Exceptionput)r   err_qservers    r   runz JupyterDash.run_app.<locals>.runP  so    $$&&&&&      		%   s    
A	AAA)targetz/_alive_c                  f    	                                  } | r| d S # t          j        $ r Y d S w xY wr   )
get_nowaitqueueEmpty)errr   s    r   
_get_errorz'JupyterDash.run_app.<locals>._get_errorg  sT    &&(( I ;   s    00
   c                  4                 	 t          j                  } | j                                        }| j        dk    rt          |          |dk    rd d }t          d| d          d S # 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     JLLLl9--k((**?c))#C..('>>1D11411C!JC J J J   "> +   
	s   A(A7 7BBBz<!)*rx   rt   
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startrs   alive_token_display_in_colab_display_in_jupyterr   
startswithr   r
   )ry   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   s   8 = 	X$,j#9  #W#W#W <$DDD#&& 	I.24jjI I<@JJI I  
 ::<<D/// C C C,@C C   ]&&d|44
 	,!!!tTl+ _,,/66s;;>NN %$ (+z~~6PRV'W'W$#/'?'F'FD'F'Q'Q$$'*$ 	J24L	
 	
 	

 ..,\:AA#FF

 jnn-?FF 9!+k!9JJ!84!8!8$!8!8JJ#**3//J &A'?AA
	MMMM 	 	 	D	 T4daPPP*%%..w}===	$&(+!%

 

 


	 	 	 	 	

 


	 !---&,tTl# MdLLTLL;3JLL		 	 	 	 	 
$&(*!%

 

 


	 	 	 	 	 	 	

 


	$	"LNNN} --mT4PVWWWWW//!4uf      	" 	" 	"k""C~~d## "S		""""!! #"""""	"s1   G 
GG/L# L# #
M;-AM66M;c                     ddl m} |dk    r|                    |||           d S |dk    r(t          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   s         r   r   zJupyterDash._display_in_colab  s     	('''''8..t5.PPPPPZ ()))..t.OOOOO	  r   c                    |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)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899999(((888999u}}
#D=#D#D#DEEFFFFF }\!! OO" (      "!r   c                      dS )Nr   r   r   r   r   serve_alivezJupyterDash.serve_alive  s    wr   c                 `     |j                             t                     fd            }dS )z(Install traceback handling for callbacksc                 `   rt          |           nd}t          j        }t          t          _        	 t	          j                    }t          |ddd|          } |             |t          _        n# |t          _        w xY w|                                }j        rt          |           |dfS )Nr   VerboseNoColorT)	tb_offsetr   color_schemeinclude_varsostreami  )
r'   inspectformatargvaluesrF   ioStringIOr   getvalueru   r   )r   r&   original_formatargvaluesr   ipytb
stacktracedev_tools_prune_errorsry   s         r   _wrap_errorszGJupyterDash.configure_callback_exception_handling.<locals>._wrap_errors  s     (>D9U###1D (/'>$&=G#C+--#""!*!%#    +C''*B'BBBB ))++J% "j!!!s?"s   1A1 1A?N)r   errorhandlerr   )ry   r   r  r  s   ` ` r   %configure_callback_exception_handlingz1JupyterDash.configure_callback_exception_handling  sI     
	 	 	+	+	# 	# 	# 	# 	# 
,	+	# 	# 	#r   c                 N    dt           j        v }t          o| o| j        p| j        S )NDATABRICKS_RUNTIME_VERSION)r   r   r   rw   rx   )ry   _inside_dbxs     r   activezJupyterDash.active  s)    2bj@Xk/Xt7W$-Xr   )Nr   r   r   r   N)__name__
__module____qualname____doc__rt   JupyterDisplayMode__annotations__r   uuiduuid4r   ru   rR   r   rz   r   r   r   staticmethodr   r   r   r  propertyr	  r   r   r   rs   rs      sF          (0L$///#jdjll##K"t"""H" " "85 5 50 .2p" p" )*p" p" p" p"d 
P 
P \
P   \&   \ #  #  #D Y Y XY Y Yr   rs   )rU   )/ra   r   r   r   r   r   r  rh   r   r_   typingr   typing_extensionsr   werkzeug.servingr   IPythonr   IPython.displayr   r   r	   IPython.core.displayr
   IPython.core.ultratbr   retryingr   ipykernel.commr   re   r   rK   r   r   r  r   r'   r   rF   rS   rN   rP   rT   rq   rs   jupyter_dashr   r   r   <module>r     sQ    				   				   



            % % % % % % ( ( ( ( ( (######;;;;;;;;;;))))))000000######OOO&)))JNN   NJ,KKK
 OP Y    * ))((//!5 !5 !5 !5H 
: : :! ! !96 96 96 96xxY xY xY xY xY xY xY xYv	 {}}s   >A; ;B
	B
