U
    /ew                  
   @  sJ  d Z ddlmZ ddlZeeZddlZddlZddl	m
Z
 ddlmZ ddlmZmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZmZ erd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&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/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= erddl>m?Z? ddl@mAZA ddlBmCZC dd l-mDZD dd!l5mEZEmFZF d"ZGd#ZHdZId$ZJd$ZKd%ZLd&ZMd'ZNe(d(ZOe(d)ZPd*ZQeR ZSePr ePTeSZUG d+d, d,eZVd-d-d.d/d0d1d2ZWe*eVj eGeHeIeJeKeLeMd3eV_ e,  dS )4z1 Provides the Bokeh Server Tornado application.

    )annotationsN)pformat)
ModuleType)TYPE_CHECKINGAnyDictListMappingSequenceSetTuple)urljoin)PeriodicCallback)ApplicationStaticFileHandler)IOLoop   )r   )Document)Model)	Resources)settings)import_optional)format_docstring)fixup_windows_event_loop_policy   )NullAuth)ServerConnection)ApplicationContext)ServerSession)per_app_patternstoplevel_patterns)RootHandler)StaticHandler)	WSHandler)	ModifyDoc)ID)Protocol)AuthProvider)RouteContext	URLRoutesihB  i  i:  i  @i,  )BokehTornadoZpandaspsutili   c                      s|  e Zd ZU dZded< ded< ded< ded	< d
ed< ded< ddde e dee	e
eeddeddde dddddefddddddddddddddddddd
ddddddd fddZddddd Zed!d" Zedd#d$d%Zedd#d&d'Zedd#d(d)Zedd#d*d+Zedd#d,d-Zedd#d.d/Zedd#d0d1Zedd#d2d3Zedd#d4d5Zedd#d6d7Zedd#d8d9Zedd#d:d;Zedd#d<d=Zdbdd>d?d@dAZ dd#dBdCZ!dcdddDdEdFZ"dGdHdIdJdKdLdMdNZ#dKddOdPdQZ$ddRdJdSdTdUZ%ddVdWdXdYZ&dd#dZd[Z'dd#d\d]Z(dd#d^d_Z)dd#d`daZ*  Z+S )dr*   a   A Tornado Application used to implement the Bokeh Server.

    Args:
        applications (dict[str,Application] or Application) :
            A map from paths to ``Application`` instances.

            If the value is a single Application, then the following mapping
            is generated:

            .. code-block:: python

                applications = {{ '/' : applications }}

            When a connection comes in to a given path, the associate
            Application is used to generate a new document for the session.

        prefix (str, optional) :
            A URL prefix to use for all Bokeh server paths. (default: None)

        extra_websocket_origins (list[str], optional) :
            A list of hosts that can connect to the websocket.

            This is typically required when embedding a Bokeh server app in an
            external web site using :func:`~bokeh.embed.server_document` or
            similar.

            If None, ``["localhost"]`` will be assumed (default: None)

        extra_patterns (seq[tuple], optional) :
            A list of tuples of (str, http or websocket handler)

            Use this argument to add additional endpoints to custom deployments
            of the Bokeh Server.

            If None, then ``[]`` will be used. (default: None)

        secret_key (str, optional) :
            A secret key for signing session IDs.

            Defaults to the current value of the environment variable
            ``BOKEH_SECRET_KEY``

        sign_sessions (bool, optional) :
            Whether to cryptographically sign session IDs

            Defaults to the current value of the environment variable
            ``BOKEH_SIGN_SESSIONS``. If ``True``, then ``secret_key`` must
            also be provided (either via environment setting or passed as
            a parameter value)

        generate_session_ids (bool, optional) :
            Whether to generate a session ID if one is not provided
            (default: True)

        keep_alive_milliseconds (int, optional) :
            Number of milliseconds between keep-alive pings
            (default: {DEFAULT_KEEP_ALIVE_MS})

            Pings normally required to keep the websocket open. Set to 0 to
            disable pings.

        check_unused_sessions_milliseconds (int, optional) :
            Number of milliseconds between checking for unused sessions
            (default: {DEFAULT_CHECK_UNUSED_MS})

        unused_session_lifetime_milliseconds (int, optional) :
            Number of milliseconds for unused session lifetime
            (default: {DEFAULT_UNUSED_LIFETIME_MS})

        stats_log_frequency_milliseconds (int, optional) :
            Number of milliseconds between logging stats
            (default: {DEFAULT_STATS_LOG_FREQ_MS})

        mem_log_frequency_milliseconds (int, optional) :
            Number of milliseconds between logging memory information
            (default: {DEFAULT_MEM_LOG_FREQ_MS})

            Enabling this feature requires the optional dependency ``psutil`` to be
            installed.

        use_index (bool, optional) :
            Whether to generate an index of running apps in the ``RootHandler``
            (default: True)

        index (str, optional) :
            Path to a Jinja2 template to serve as the index for "/" if use_index
            is True. If None, the basic built in app index template is used.
            (default: None)

        redirect_root (bool, optional) :
            When there is only a single running application, whether to
            redirect requests to ``"/"`` to that application automatically
            (default: True)

            If there are multiple Bokeh applications configured, this option
            has no effect.

        websocket_max_message_size_bytes (int, optional):
            Set the Tornado ``websocket_max_message_size`` value.
            (default: {DEFAULT_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES})

        websocket_compression_level (int, optional):
            Set the Tornado WebSocket ``compression_level`` documented in
            https://docs.python.org/3.7/library/zlib.html#zlib.compressobj.

        websocket_compression_mem_level (int, optional):
            Set the Tornado WebSocket compression ``mem_level`` documented in
            https://docs.python.org/3.7/library/zlib.html#zlib.compressobj.

        index (str, optional):
            Path to a Jinja2 template to use for the root URL

        auth_provider (AuthProvider, optional):
            An AuthProvider instance

        include_headers (list, optional) :
            List of request headers to include in session context
            (by default all headers are included)

        exclude_headers (list, optional) :
            List of request headers to exclude in session context
            (by default all headers are included)

        include_cookies (list, optional) :
            List of cookies to include in session context
            (by default all cookies are included)

        exclude_cookies (list, optional) :
            List of cookies to exclude in session context
            (by default all cookies are included)

        session_token_expiration (int, optional) :
            Duration in seconds that a new session token is valid
            for session creation. After the expiry time has elapsed,
            the token will not be able create a new session
            (default: {DEFAULT_SESSION_TOKEN_EXPIRATION})

    Any additional keyword arguments are passed to ``tornado.web.Application``.

    .. autoclasstoc::

    r   _loopz Mapping[str, ApplicationContext]_applicationsstr_prefixzSet[str]_websocket_originsr'   auth_providerzSet[ServerConnection]_clientsNTFz?Mapping[str, Application | ModifyDoc] | Application | ModifyDocz
str | NonezSequence[str] | NonezURLRoutes | Nonezbytes | Noneboolintz
int | NonezList[str] | None)applicationsprefixextra_websocket_originsextra_patterns
secret_keysign_sessionsgenerate_session_idskeep_alive_milliseconds"check_unused_sessions_milliseconds$unused_session_lifetime_milliseconds stats_log_frequency_millisecondsmem_log_frequency_milliseconds	use_indexredirect_root websocket_max_message_size_byteswebsocket_compression_levelwebsocket_compression_mem_levelindexr1   xsrf_cookiesinclude_headersinclude_cookiesexclude_headersexclude_cookiessession_token_expirationc           )        s&  d | _ ddlm  ddlm} t|r2t||}t|trDd|i}t|	 D ]H\}}t|rtt|| ||< }t
 fdd|jD rP|   qP|d krd}|d}|rd| }|| _|| _|dk rtdn*|dkrtd	 n|tkrtd
| || _|	dkrtdn|	tkr4td|	 |	| _|
dkrNtdn|
tkrdtd|
 |
| _|dkr~tdn|tkrtd| || _|dk rtdn:|dkrtd krtd d}n|tkrtd| || _|dkrtdn|tkrtd||d  || _ | j j!s8| j j"rDtd n
td ||d< |rftd |dkrztdn|| _#|r|rtd|| _$|| _%|r|rtd|| _&|| _'|d krt( | _)n
t(|| _)|| _*|| _+|| _,t-dt| j) i | _.|	 D ]"\}}t/||| j j0d| j.|< q|p<g }|1| j j2 g }|	 D ]\}}g } t3D ]r}!|dkr|!d }"n||!d  }"d | j.| i}#t4|!d! t5r||#d"< ||#d#< | j|" }"| 6|"|!d! |#f qhd }$| D ]}%|%d 7d$r|%d }$q|$st8d%| D ]}%|$|%d& d'< q|1|  |6t9| j|| qX|t: D ]}!|!d! t;kr|r| j.| j| j|d(}&| j|!d  f|!d!d   |&f }'|6|' n&| j|!d  f|!d!d   }'|6|' qTt-d) t<|d*d+=d,D ]}(t-d-|(  qt> j?|fd.|i| d S )/Nr   DocumentLifecycleHandler)FunctionHandler/c                 3  s   | ]}t |  V  qd S N
isinstance).0handlerrM    8/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/server/tornado.py	<genexpr>'  s     z(BokehTornado.__init__.<locals>.<genexpr> z$keep_alive_milliseconds must be >= 0zKeep-alive ping disabledz0Keep-alive ping configured every %d millisecondsz.check_unused_sessions_milliseconds must be > 0z/Check for unused sessions every %d millisecondsz0unused_session_lifetime_milliseconds must be > 0z(Unused sessions last for %d millisecondsz,stats_log_frequency_milliseconds must be > 0z$Log statistics every %d millisecondsz+mem_log_frequency_milliseconds must be >= 0zMemory logging requested, but is disabled. Optional dependency 'psutil' is missing. Try 'pip install psutil' or 'conda install psutil'z&Log memory usage every %d millisecondsz1websocket_max_message_size_bytes must be positivez>Torndado websocket_max_message_size set to %d bytes (%0.2f MB)g      0Az4User authentication hooks provided (no default user)z=User authentication hooks NOT provided (default user enabled)rG   zXSRF cookie protection enabledz$session_token_expiration must be > 0zGDeclare either an include or an exclude list for the cookies, not both.zGDeclare either an include or an exclude list for the headers, not both.z3These host origins can connect to the websocket: %r)url
logout_urlapplication_contextr   Zcompression_levelZ	mem_levelz/wszCouldn't find websocket pathr   Zbokeh_websocket_path)r5   r6   rF   Zuse_redirectzPatterns are:<   )width
z  Zwebsocket_max_message_size)@r,   Z-bokeh.application.handlers.document_lifecyclerN   Z#bokeh.application.handlers.functionrO   callabler   rS   listitemsall	_handlersaddstripr/   _index
ValueErrorloginfoDEFAULT_KEEP_ALIVE_MS_keep_alive_millisecondsDEFAULT_CHECK_UNUSED_MS#_check_unused_sessions_millisecondsDEFAULT_UNUSED_LIFETIME_MS%_unused_session_lifetime_millisecondsDEFAULT_STATS_LOG_FREQ_MS!_stats_log_frequency_millisecondsr+   warningDEFAULT_MEM_LOG_FREQ_MS_mem_log_frequency_milliseconds(DEFAULT_WEBSOCKET_MAX_MESSAGE_SIZE_BYTESr1   Zget_userZget_user_async_session_token_expiration_exclude_cookies_include_cookies_exclude_headers_include_headerssetr0   _secret_key_sign_sessions_generate_session_idsdebugr-   r   r[   extendZ	endpointsr   
issubclassr#   appendendswithRuntimeErrorcreate_static_handlerr    r!   r   splitsuper__init__))selfr5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   r1   rG   rH   rI   rJ   rK   rL   kwargsrO   rZ   appZall_patternskeyZapp_patternsproutecontextZwebsocket_pathrdataZprefixed_patline	__class__rM   rW   r      s   

































"
zBokehTornado.__init__None)io_loopreturnc                 C  s   || _ | j D ]}| j |_ qt | _t| j| j| _| j	dkrRt| j
| j	| _nd| _t| j| j| _| jdkrt| j| j| _nd| _dS )zN Start a Bokeh Server Tornado Application on a given Tornado IOLoop.

        r   N)r,   r-   valuesr|   r2   r   
_log_statsrr   
_stats_jobru   _log_mem_mem_job_cleanup_sessionsrn   _cleanup_jobrl   _keep_alive	_ping_job)r   r   Zapp_contextrV   rV   rW   
initialize  s$    


zBokehTornado.initializec                 C  s   | j S )z& The configured applications

        )r-   r   rV   rV   rW   r5     s    zBokehTornado.applications)r   c                 C  s
   t | jS )zw A list of all application paths for all Bokeh applications
        configured on this Bokeh server instance.

        )r|   r-   r   rV   rV   rW   	app_paths  s    zBokehTornado.app_pathsc                 C  s   | j S )z> Path to a Jinja2 template to serve as the index "/"

        )rg   r   rV   rV   rW   rF     s    zBokehTornado.indexc                 C  s   | j S )z` The Tornado IOLoop that this  Bokeh Server Tornado Application
        is running on.

        )r,   r   rV   rV   rW   r     s    zBokehTornado.io_loopc                 C  s   | j S )z^ A URL prefix for this Bokeh Server Tornado Application to use
        for all paths

        )r/   r   rV   rV   rW   r6     s    zBokehTornado.prefixc                 C  s   | j S )zK A list of websocket origins permitted to connect to this server.

        )r0   r   rV   rV   rW   websocket_origins  s    zBokehTornado.websocket_originsc                 C  s   | j S )zy A secret key for this Bokeh Server Tornado Application to use when
        signing session IDs, if configured.

        )r}   r   rV   rV   rW   r9     s    zBokehTornado.secret_keyc                 C  s   | j S )zV A list of request cookies to make available in the session
        context.

        )ry   r   rV   rV   rW   rI     s    zBokehTornado.include_cookiesc                 C  s   | j S )zV A list of request headers to make available in the session
        context.

        )r{   r   rV   rV   rW   rH     s    zBokehTornado.include_headersc                 C  s   | j S )zG A list of request cookies to exclude in the session context.

        )rx   r   rV   rV   rW   rK   $  s    zBokehTornado.exclude_cookiesc                 C  s   | j S )zG A list of request headers to exclude in the session context.

        )rz   r   rV   rV   rW   rJ   +  s    zBokehTornado.exclude_headersc                 C  s   | j S )z Whether this Bokeh Server Tornado Application has been configured
        to cryptographically sign session IDs

        If ``True``, then ``secret_key`` must also have been configured.
        )r~   r   rV   rV   rW   r:   2  s    zBokehTornado.sign_sessionsc                 C  s   | j S )z{ Whether this Bokeh Server Tornado Application has been configured
        to automatically generate session IDs.

        )r   r   rV   rV   rW   r;   ;  s    z!BokehTornado.generate_session_idsc                 C  s   | j S )z Duration in seconds that a new session token is valid for
        session creation.

        After the expiry time has elapsed, the token will not be able
        create a new session.
        )rw   r   rV   rV   rW   rL   C  s    z%BokehTornado.session_token_expirationr   )absolute_urlr   c                 C  sD   t jdd}|dkr:|r$t|| jn| j}td|tjdS t|dS )aJ   Provide a :class:`~bokeh.resources.Resources` that specifies where
        Bokeh application sessions should load BokehJS resources from.

        Args:
            absolute_url (bool):
                An absolute URL prefix to use for locating resources. If None,
                relative URLs are used (default: None)

        server)default)moderoot_urlZpath_versioner)r   )r   	resourcesr   r/   r   r"   Zappend_version)r   r   r   r   rV   rV   rW   r   M  s
    
zBokehTornado.resourcesc                 C  s^   | j   | jdk	r| j  | j  | jdk	r<| j  | j D ]}| j|j	 qFdS )a   Start the Bokeh Server application.

        Starting the Bokeh Server Tornado application will run periodic
        callbacks for stats logging, cleanup, pinging, etc. Additionally, any
        startup hooks defined by the configured Bokeh applications will be run.

        N)
r   startr   r   r   r-   r   r,   Zadd_callbackZrun_load_hook)r   r   rV   rV   rW   r   ]  s    





zBokehTornado.start)waitr   c                 C  sb   | j  D ]}|  q
| j  | jdk	r6| j  | j  | jdk	rT| j  | j	  dS )z Stop the Bokeh Server application.

        Args:
            wait (bool): whether to wait for orderly cleanup (default: True)

        Returns:
            None

        N)
r-   r   Zrun_unload_hookr   stopr   r   r   r2   clear)r   r   r   rV   rV   rW   r   o  s    






zBokehTornado.stopr&   r#   r   r   r   )protocolsocketr\   sessionr   c                 C  s   t ||||}| j| |S rQ   )r   r2   re   )r   r   r   r\   r   
connectionrV   rV   rW   new_connection  s    zBokehTornado.new_connection)r   r   c                 C  s   | j | |  d S rQ   )r2   discardZdetach_session)r   r   rV   rV   rW   client_lost  s    zBokehTornado.client_lostr%   )app_path
session_idr   c                 C  s&   || j krtd| | j | |S )ak   Get an active a session by name application path and session ID.

        Args:
            app_path (str) :
                The configured application path for the application to return
                a session for.

            session_id (str) :
                The session ID of the session to retrieve.

        Returns:
            ServerSession

        ,Application %s does not exist on this server)r-   rh   get_session)r   r   r   rV   rV   rW   r     s    
zBokehTornado.get_sessionzList[ServerSession])r   r   c                 C  s&   || j krtd| t| j | jS )a   Gets all currently active sessions for an application.

        Args:
            app_path (str) :
                The configured application path for the application to return
                sessions for.

        Returns:
            list[ServerSession]

        r   )r-   rh   ra   sessions)r   r   rV   rV   rW   get_sessions  s    
zBokehTornado.get_sessionsc                   s0   t d | j D ]}|| jI d H  qd S )NzRunning session cleanup job)ri   tracer-   r   r   rp   )r   r   rV   rV   rW   r     s    
zBokehTornado._cleanup_sessionsc                 C  s   t d t  tjkrd S t dtt| j | j	
 D ]H\}}t|j}d}|D ]}|jdkrT|d7 }qTt dt|t|| q:d S )NzRunning stats log jobz[pid %d] %d clients connectedr   r   z,[pid %d]   %s has %d sessions with %d unused)ri   r   getEffectiveLevelloggingDEBUGr   PIDlenr2   r-   rb   ra   r   Zconnection_count)r   r   r   r   Zunused_countsrV   rV   rW   r     s     



   zBokehTornado._log_statsc                   s   t  }tdt|jt |jt  ~t t	j
kr8d S t }dtfdtfdtffD ]4\}  fdd|D }td| dt|  qVd	d t D }td
t|  trdd |D }tdt| d S )Nz5[pid %d] Memory usage: %0.2f MB (RSS), %0.2f MB (VMS)Z	DocumentsZSessionsZModelsc                   s   g | ]}t | r|qS rV   rR   rT   xtyprV   rW   
<listcomp>  s     
 z)BokehTornado._log_mem.<locals>.<listcomp>z  uncollected z: c                 S  s&   g | ]}t |trd t|kr|qS )Z
bokeh_app_)rS   r   r.   r   rV   rV   rW   r     s     
  z  uncollected modules: c                 S  s   g | ]}t |tjr|qS rV   )rS   pdZ	DataFramer   rV   rV   rW   r     s      z  uncollected DataFrames: %d)PROCZmemory_infori   rj   r   ZrssGBZvmsr   r   r   gcZget_objectsr   r   r   r   r   r   )r   ZmemZall_objsnameobjsrV   r   rW   r     s    zBokehTornado._log_memc                 C  s"   t d | jD ]}|  qd S )NzRunning keep alive job)ri   r   r2   Z	send_ping)r   crV   rV   rW   r     s    

zBokehTornado._keep_alive)N)T),__name__
__module____qualname____doc____annotations__r   Zsecret_key_bytesr:   rk   rm   ro   rq   rt   rv   r    DEFAULT_SESSION_TOKEN_EXPIRATIONr   r   propertyr5   r   rF   r   r6   r   r9   rI   rH   rK   rJ   r;   rL   r   r   r   r   r   r   r   r   r   r   r   __classcell__rV   rV   r   rW   r*   d   s   
 D G
	(r*   r.   r   z=Tuple[str, StaticFileHandler | StaticHandler, Dict[str, Any]])r6   r   r   r   c                 C  s@   | }||dkrdn|d 7 }|j d k	r6|td|j ifS |ti fS )NrP   z/static/(.*)path)Zstatic_pathr   r"   )r6   r   r   r   rV   rV   rW   r     s
    
r   )rm   rk   rt   rq   ro   rv   r   )Xr   
__future__r   r   	getLoggerr   ri   r   ospprintr   typesr   typingr   r   r   r   r	   r
   r   r   urllib.parser   Ztornado.ioloopr   Ztornado.webr   ZTornadoApplicationr   r   Zapplicationdocumentr   modelr   r   r   r   Zutil.dependenciesr   Zutil.stringr   Zutil.tornador   r1   r   r   r   Zcontextsr   r   r   urlsr   r    Zviews.root_handlerr!   Zviews.static_handlerr"   Zviews.wsr#   Zapplication.handlers.functionr$   Z
core.typesr%   r   r&   r'   r(   r)   rm   rk   rt   rq   ro   rv   r   __all__r   r+   r   getpidr   Processr   r*   r   rV   rV   rV   rW   <module>   s   
(

     