a
    bb^                     @   sv  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mZ ddlm	Z	 ddl
mZ ddlZddlmZmZ ddlmZ dd	lmZ dd
lmZ eejjedddZedd dD Zej ej!eZ"dd Z#G dd dej$Z%G dd de%Z&e'e&jdre&j( Z)n
e&j* Z)e&f i dd e)+ D Z,G dd deZ-dd Z.dd Z/e-0e. e'e-d rre-1e/ dS )!z
The config module supplies the global config object and the extension
which provides convenient support for  loading and configuring panel
components.
    N)ThreadPoolExecutor)contextmanager)WeakKeyDictionary)JupyterCommManager	extension   )panel_log_handler)load_notebookstatez$Format:%h$Zpanel)ZfpathZarchive_commitZreponamec                 c   s   | ]}|t v V  qd S N)__version__).0v r   +lib/python3.9/site-packages/panel/config.py	<genexpr>       r   )ZpostZdirtyc              
   C   s   | j r
dS d| _ t| |}zPzt| || W n( tyT } z|W Y d}~n
d}~0 0 W t| || d| _ nt| || d| _ 0 dS )zC
    Validates parameter setting on a hidden config parameter.
    NTF)_validatinggetattrsetattr	Exception)configZ	parametervalueZoriger   r   r   validate_config&   s    
r   c                   @   sD   e Zd Zejg ddZeji ddZeji ddZejg ddZ	dS )_base_configz$
        External CSS files to load.defaultdoczo
        External JS files to load. Dictionary should map from exported
        name to the URL of the JS file.zy
        External JS fils to load as modules. Dictionary should map from
        exported name to the URL of the JS file.z0
        List of raw CSS strings to add to load.N)
__name__
__module____qualname__paramList	css_filesDictjs_files
js_modulesraw_cssr   r   r   r   r   7   s   r   c                       s  e Zd ZdZejdddZejdddZejdg dd	d
Z	ej
dddZejdddZejdddZejddddgddZejdddZejddddZejdg ddd
ZejdddZejdddgdd
Zejdd dZejdd!dZejdg d"d#d
Zejd$dg d%d&dZejdd'dZejddd(d)Zejdd*dZejd+d,dZejdd-dZejd.d/dZ ejd0g d1d2d
Z!ejdd3dZ"ejddg d4dZ#ej$d5d6d7dZ%ejdd8dZ&ejdd9dZ'ejdd:dZ(ejdd;dZ)ej*de+d<d=Z,ej-i d>dZ.eje/dd?d)Z0g d@Z1g dAZ2e3 Z4 fdBdCZ5ej6dDdddEdFdG Z7ej6dHddIdJdK Z8e9dLdM Z: fdNdOZ;ej6dPddIdQdR Z< fdSdTZ=dUdV Z>dWdX Z?e@dYdZ ZAe@d[d\ ZBe@d]d^ ZCe@d_d` ZDe@dadb ZEe@dcdd ZFe@dedf ZGe@dgdh ZHe@didj ZIe@dkdl ZJe@dmdn ZKe@dodp ZLe@dqdr ZMe@dsdt ZNe@dudv ZOe@dwdx ZPe@dydz ZQe@d{d| ZRe@d}d~ ZSe@dd ZT  ZUS )_configa  
    Holds global configuration options for Panel. The options can be
    set directly on the global config instance, via keyword arguments
    in the extension or via environment variables. For example to set
    the embed option the following approaches can be used:

        pn.config.embed = True

        pn.extension(embed=True)

        os.environ['PANEL_EMBED'] = 'True'
    Tzk
        Whether to set custom Signature which allows tab-completion
        in some IDEs and environments.r   Fz:
        Whether to autoreload server when script changes.arcs)Zarcr+   ZbarZdotsZpetalzJ
        Loading indicator to use when component loading parameter is set.)r   objectsr   z#c3c3c3z(
        Color of the loading indicator.i  z1
        Maximum height of the loading indicator.z7
        Whether to enable notifications functionality.NZpyinstrumentZsnakevizz'
        The profiler engine to enable.)r   
allow_Noner,   r   z
        Ensure all bokeh property changes trigger events which are
        embedded. Useful when only partial updates are made in an
        app, e.g. when working with HoloViews.r   )Nz
        If set to a non-negative value this determines the maximum length
        of the pn.state.session_info dictionary, which tracks
        information about user sessions. A value of -1 indicates an
        unlimited history.)r   Zboundsr   )ZfixedZstretch_widthZstretch_heightZstretch_bothZscale_widthZscale_heightZ
scale_bothNz<
        Specify the default sizing mode behavior of panels.zA
        The default template to render served applications into.r   Zdarkz<
        The theme to apply to the selected global template.zJ
        If sliders and inputs should be throttled until release of mouse.z$Whether the admin panel was enabled.)r   
ipywidgetsvscodecolabz
        Whether to render output in Jupyter with the default Jupyter
        extension or use the jupyter_bokeh ipywidget model.
accumulate)r2   replacedisableFzl
        How to log errors and stdout output triggered by callbacks
        from Javascript in the notebook.z<
        Configure to enable getting/setting secure cookies.z,
        Whether plot data will be embedded.)r   r-   r   z6
        Whether to save embedded state to json files. z8
        Prefix for randomly generated json directories.z5
        Where to load json files for embedded state.z./z5
        Where to save json files for embedded state.WARNING)DEBUGINFOr6   ERRORZCRITICALzLog level of Panel loggersz
        When set to a non-None value a thread pool will be started.
        Whenever an event arrives from the frontend it will be
        dispatched to the thread pool to be processed.z=
        Select between a list of authentification providers.r   )r   Nz6
        Expiry of the OAuth cookie in number of days.z7
        A client key to provide to the OAuth provider.z:
        A client secret to provide to the OAuth provider.z:
        The key in the ID JWT token to consider the user.z9
        A redirect URI to provide to the OAuth provider.zG
        A random string used to encode OAuth related user information.)r   Zclass_r   z;
        Additional parameters required for OAuth provider.zx
        Whether to inline JS and CSS resources. If disabled, resources
        are loaded from CDN if one is available.)
autoreloadcommscookie_secretnthreadsoauth_provideroauth_expiry	oauth_keyoauth_secretoauth_jwt_useroauth_redirect_urioauth_encryption_keyoauth_extra_params)Truetrue1Tr   c                    sf   t  jf i | d| _| jD ]0}|dr|dd  | jvrt| |d d  q| jrbt	| j d S )NF_r   )
super__init__r   r#   
startswith_globalsr   	log_levelr   setLevel)selfparamsp	__class__r   r   rK      s    
z_config.__init__	_nthreads)watchZon_initc                 C   sf   ddl m} | jd u r8|jd ur.|jjdd d |_d S |jrFtd| jrR| jnd }t|d|_d S )Nr   r
   F)waitzThread pool already running)Zmax_workers)io.stater   r=   Z_thread_poolZshutdownRuntimeErrorr   )rP   r   Zthreadsr   r   r   _set_thread_pool   s    

z_config._set_thread_poolnotifications)rV   c                 C   s*   ddl m} ddlm} |js&| |_d S )Nr   )NotificationArear
   )Zio.notificationsr\   rX   r   curdocZ_notification)rP   r\   r   r   r   r   _enable_notifications   s    z_config._enable_notificationsc                 +   s   dd  j   D } fdd j D }| D ]\}}t || q4z>d V  W  j jf i t| |D ]\}}t |d | qnn6 j jf i t| |D ]\}}t |d | q0 d S )Nc                 S   s    g | ]\}}|d kr||fqS )namer   r   kr   r   r   r   
<listcomp>   r   z_config.set.<locals>.<listcomp>c                    s:   g | ]2}| d r|dd  jvr|t |d  fqS )rI   r   N)rL   rM   r   )r   ra   rP   r   r   rb      s   rI   )r#   valuesitemsr   updatedict)rP   kwargsrd   Z	overridesra   r   r   rc   r   set   s    
z_config.setc                    sn  ddl m} t| ddr4|dr,|ds4|dkrBt ||S t| d| ddd	 |}|| jv rt || jv r||nd| | n|j	d ur|| jv rt
| || n2d| | jv rt
| d| | nt|d
|j	| jvri | j|j	< || j|j	 |< n^d| | jv r\t| d| dr\t
| d| | t d| d| nt || d S )Nr   r
   initializedFrI   r   Z_hookc                 S   s   | S r   r   )xr   r   r   <lambda>  r   z%_config.__setattr__.<locals>.<lambda>z! is not a valid config parameter.)rX   r   r   rL   endswithrJ   __setattr__rM   r#   r]   r   AttributeError_session_confighasattr)rP   attrr   r   rS   r   r   rn     s&    (
$
&z_config.__setattr__
_log_levelc                 C   s   t | j d S r   )r   rO   rs   rc   r   r   r   _update_log_level  s    z_config._update_log_levelc                    s,  ddl m} t d}t d}|r@|ds@t d}ng }t d}|jrj|j|vrji ||j< |dv r|jr|||j vrtt |}t| || ||v rt |S |jr|j|v r|||j v r||j | S d	| |v r t| d	| d	d ur t d	| d	S t |S )
Nr   r
   rj   rM   __r#   rp   )r)   r%   r'   r(   rI   )	rX   r   rJ   __getattribute__rL   r]   copyr   r   )rP   rr   r   initZglobal_paramsrQ   Zsession_configZnew_objrS   r   r   rv     s.    
(z_config.__getattribute__c                 C   s   |r|S dS )Nr4   r   rP   r   r   r   r   _console_output_hook6  s    z_config._console_output_hookc                 C   s   t |tr| jjj| S |S r   )
isinstancestrr#   templatenamesry   r   r   r   _template_hook9  s    
z_config._template_hookc                 C   s   t jdS )NZPANEL_DOC_BUILD)osenvirongetrc   r   r   r   
_doc_build>  s    z_config._doc_buildc                 C   s   | j r
dS tjdtjS d S )Nr4   ZPANEL_CONSOLE_OUTPUT)r   r   r   r   r*   _console_outputrc   r   r   r   console_outputB  s    z_config.console_outputc                 C   s   t jdtj| jv S )NZPANEL_EMBED)r   r   r   r*   _embed_truthyrc   r   r   r   embedI  s    z_config.embedc                 C   s   t jd| jS )NZPANEL_COMMS)r   r   r   _commsrc   r   r   r   r;   M  s    z_config.commsc                 C   s   t jdtj| jv S )NZPANEL_EMBED_JSON)r   r   r   r*   _embed_jsonr   rc   r   r   r   
embed_jsonQ  s    z_config.embed_jsonc                 C   s   t jdtjS )NZPANEL_EMBED_JSON_PREFIX)r   r   r   r*   _embed_json_prefixrc   r   r   r   embed_json_prefixU  s    z_config.embed_json_prefixc                 C   s   t jdtjS )NZPANEL_EMBED_SAVE_PATH)r   r   r   r*   _embed_save_pathrc   r   r   r   embed_save_pathY  s    z_config.embed_save_pathc                 C   s   t jdtjS )NZPANEL_EMBED_LOAD_PATH)r   r   r   r*   _embed_load_pathrc   r   r   r   embed_load_path]  s    z_config.embed_load_pathc                 C   s   t jdtj| jv S )NZPANEL_INLINE)r   r   r   r*   _inliner   rc   r   r   r   inlinea  s    z_config.inlinec                 C   s    t jd| j}|r| S d S )NZPANEL_LOG_LEVEL)r   r   r   rs   upper)rP   rN   r   r   r   rN   e  s    z_config.log_levelc                 C   s$   t jd| j}|d u rd S t|S )NZPANEL_NUM_THREADS)r   r   r   rU   int)rP   r=   r   r   r   r=   j  s    z_config.nthreadsc                 C   s    t jd| j}|r| S d S )NZPANEL_OAUTH_PROVIDER)r   r   r   _oauth_providerlowerrP   Zproviderr   r   r   r>   o  s    z_config.oauth_providerc                 C   s   t jd| j}t|S )NZPANEL_OAUTH_EXPIRY)r   r   r   _oauth_expiryfloatr   r   r   r   r?   t  s    z_config.oauth_expiryc                 C   s   t jd| jS )NZPANEL_OAUTH_KEY)r   r   r   
_oauth_keyrc   r   r   r   r@   y  s    z_config.oauth_keyc                 C   s   t jdt jd| jS )NZPANEL_COOKIE_SECRETZBOKEH_COOKIE_SECRET)r   r   r   _cookie_secretrc   r   r   r   r<   }  s    z_config.cookie_secretc                 C   s   t jd| jS )NZPANEL_OAUTH_SECRET)r   r   r   _oauth_secretrc   r   r   r   rA     s    z_config.oauth_secretc                 C   s   t jd| jS )NZPANEL_OAUTH_REDIRECT_URI)r   r   r   _oauth_redirect_urirc   r   r   r   rC     s    z_config.oauth_redirect_uric                 C   s   t jd| jS )NZPANEL_OAUTH_JWT_USER)r   r   r   _oauth_jwt_userrc   r   r   r   rB     s    z_config.oauth_jwt_userc                 C   s   t jd| jS )NZPANEL_OAUTH_ENCRYPTION)r   r   r   _oauth_encryption_keyrc   r   r   r   rD     s    z_config.oauth_encryption_keyc                 C   s$   dt jv rtt jd S | jS d S )NZPANEL_OAUTH_EXTRA_PARAMS)r   r   astZliteral_eval_oauth_extra_paramsrc   r   r   r   rE     s    
z_config.oauth_extra_params)Vr    r!   r"   __doc__r#   ZBooleanapply_signaturesr:   ZSelectorZloading_spinnerZColorZloading_colorZIntegerZloading_max_heightr[   ZprofilerZ
safe_embedZsession_historyZObjectSelectorZsizing_moder}   ZthemeZ	throttledZ_adminr   r   Stringr   r   r   r   r   r   rs   rU   r   Numberr   r   r   r   r   ZClassSelectorbytesr   r&   r   _LOCAL_DEV_VERSIONr   rM   r   r   rp   rK   ZdependsrZ   r^   r   ri   rn   rt   rv   rz   r   propertyr   r   r   r;   r   r   r   r   r   rN   r=   r>   r?   r@   r<   rA   rC   rB   rD   rE   __classcell__r   r   rS   r   r*   H   s   
	






















r*   r,   c                 C   s.   i | ]&\}}|d kr||j r dntt|qS )r_   N)r-   r   r*   )r   ra   rR   r   r   r   
<dictcomp>  s   r   c                   @   sz   e Zd ZdZdZddddddd	d
dddddddZdgdgdgdgdgdgdgddgdgdgd
Zg Zdd Zd d! Z	d"S )#panel_extensionze
    Initializes the pyviz notebook extension to allow plotting with
    bokeh and enable comms.
    Fzpanel.models.katexzpanel.models.mathjaxzpanel.models.plotlyzpanel.models.deckglzpanel.models.vegazpanel.models.vtkzpanel.models.acezpanel.models.echartszipywidgets_bokeh.widgetzpanel.models.perspectivezpanel.models.terminalzpanel.models.tabulatorzpanel.models.quillzpanel.models.json_editor)katexmathjaxplotlydeckglvegavtkZaceechartsr/   Zperspectiveterminal	tabulatorZ
texteditorZ
jsoneditorZdeckr   r   ZMathJaxZPlotlyr   r   ZTerminalZxtermjsZ	TabulatorZ	GridStack)
r   r   r   r   r   r   r   r   r   Z	gridstackc              	   O   s  ddl m}m} dd t| D }|D ]x}|| jv r~z |dkrXt rXdtj	vrXW q,W n t
yl   Y n0 t| j|  q,||v r|j| q,| jd|  q,| D ]j\}}|dv rt|tstd	|t|jf tt|| q|d
krtt|| qtt|| qtjr*|   | j}	|	r|dkrd| jv rt }
t|
d}t|dkrd|d j v rd S dt!j"v rdd l#}dd l$}|	pt|j%dd}	|j&j'|j&j(v r|j&j'}nd}t)|j&dr|j&*| n||j&_'z|+dd p
t }W n t
y$   Y d S 0 dd |D }|	rR|rR| jd|  nt, j|7  _t)|dr| jstj-st./| j0d t.t1_2dt!j"v rtj3rt| jd  t| dd}dt!j"v r.t|j%ddrd S t4d4 |j5j67tj8 t)|j5j6drd}W d    n1 s$0    Y  |sJt)|drJt9tj8 dt,_d |v r^d S zdd l:}d!t_;W d S  t<y   Y n0 d"tj	v rd#t_;d$t!j"v rdt_;tj=rt>t1j= d S )%Nr   )ReactiveHTMLReactiveHTMLMetaclassc                 S   s   i | ]\}}|j |qS r   )Z_extension_namer`   r   r   r   r     s   z,panel_extension.__call__.<locals>.<dictcomp>r/   ZPANEL_IPYWIDGETz0%s extension not recognized and will be skipped.)r)   r%   z2%s should be supplied as a list, not as a %s type.r'   )r   r         Zpyvista	holoviewsr   _loadedFZbokehset_current_backendipc                 S   s   g | ]}|t jvr|qS r   )r   _loaded_extensions)r   argr   r   r   rb     r   z,panel_extension.__call__.<locals>.<listcomp>a6  A HoloViz extension was loaded previously. This means the extension is already initialized and the following Panel extensions could not be properly loaded: %s. If you are loading custom extensions with pn.extension(...) ensure that this is called before any other HoloViz extension such as hvPlot or HoloViews.Zkernelzhv-extension-commZ_repeat_execution_in_cellr9   Z_render_with_panelTr;   r1   Z
VSCODE_PIDr0   Zpyodide)?Zreactiver   r   r#   concrete_descendentsre   _importsZget_ipythonr   r   r   
__import__r   addZwarningr{   list
ValueErrortyper    r   r   extendrf   r   r   _apply_signaturesr   inspectZcurrentframeZgetouterframeslenfilenamesysmodulesr   Zholoviews.plotting.bokehr   ZStoreZcurrent_backendZ	renderersrq   r   popr   r   _JupyterCommManagerZget_client_commZ_process_comm_msgr   Z_comm_managerr   Zlogging_levelZplottingZRendererZload_nbr   r	   Zgoogle.colabr;   ImportErrorr[   Zdisplay)rP   argsrQ   r   r   Zreactive_extsr   ra   r   ZloadedZcurframeZcalframeZhvr   Zbackendr   Znewly_loadedZ	nb_loadedZgoogler   r   r   __call__  s    



$

zpanel_extension.__call__c                    sr  ddl m m} ddlm} t|}tt|	 D ]4}|j
drh|j
dd }|j
|d|_
t |jtj	 }|r6|d  d jkrq6|d d }t g  }}	t| D ]X}g }
t|j D ]6\}}t|tjr||vr|js|
| || q|	|
 q| fd	d
t|	D 7 }d|v rDdnd}| | j ||jd|j_q6d S )Nr   )	Parameter	Signaturer   )ViewablerQ   
r5   r.   c                    s,   g | ]$}|D ]}|j vr | jqqS r   )
parametersZKEYWORD_ONLY)r   Zkwsr_   r   Zsigr   r   rb   b  s   

z5panel_extension._apply_signatures.<locals>.<listcomp>rh   Z_kwargs)return_annotation)r   r   r   viewabler   r#   r   reversedr   rd   r   rL   splitr3   Z	signaturerK   r   ZVAR_KEYWORDri   mrosorted__dict__re   r{   readonlyappendr   r   Z__signature__)rP   r   r   ZdescendantsclsprefixZ
sig_paramsr   Zprocessed_kwsZkeyword_groupsZkeyword_groupra   r   Z
kwarg_namer   r   r   r   I  s<    

z!panel_extension._apply_signaturesN)
r    r!   r"   r   r   r   rM   r   r   r   r   r   r   r   r     s>   yr   c                 C   s0   | t jvrdS t j| \}}}}|| dS )zQ
    A cleanup action which is called when a plot is deleted in the notebook
    N)r   Z_viewsr   _cleanup)Zmsg_idr   ZmodelrI   r   r   r   _cleanup_panelr  s    
r   c                 C   s\   | t jvrdS t j| \}}}|  |D ]*}|jD ]}|jd |jv r6|| q6q,dS )zS
    A cleanup action which is called when a server is deleted in the notebook
    Nid)r   Z_serversr   stoprootsrefZ_modelsr   )Z	server_idZserverr   Zdocsr   rootr   r   r   _cleanup_server|  s    

r   add_server_delete_action)2r   r   rw   r   r   r   Zconcurrent.futuresr   
contextlibr   weakrefr   r#   Zpyviz_commsr   r   r   Z_pyviz_extensionZ
io.loggingr   Zio.notebookr	   rX   r   r|   versionZVersion__file__r   anyr   pathabspathdirnameZ_PATHr   ZParameterizedr   r*   rq   r,   Z_paramsrQ   re   r   r   r   r   Zadd_delete_actionr   r   r   r   r   <module>   sH     V

 N

