bokeh.application.handlers.directory#

Provide a Bokeh Application Handler to build up documents by running the code from main.py or main.ipynb files in specified directories.

The directory may also optionally contain:

  • A server_lifecyle.py module to provide lifecycle callbacks for the application and sessions.

  • A static subdirectory containing app-specific static resources to serve.

  • A theme.yaml file containing a Bokeh theme to automatically apply to all new documents.

  • A templates subdirectory containing templates for app display

A full directory layout might look like:

myapp
   |
   +---main.py
   +---server_lifecycle.py
   +---static
   +---theme.yaml
   +---templates
        +---index.html
class DirectoryHandler(*, filename: PathLike, argv: list[str] = [])[source]#

Load an application directory which modifies a Document.

Public Data Attributes:

error

If the handler fails, may contain a related error message.

error_detail

If the handler fails, may contain a traceback or other details.

failed

True if the handler failed to modify the doc

safe_to_fork

Whether it is still safe for the Bokeh server to fork new workers.

Inherited from : py: class:Handler

error

If the handler fails, may contain a related error message.

error_detail

If the handler fails, may contain a traceback or other details.

failed

True if the handler failed to modify the doc

safe_to_fork

Whether it is still safe for the Bokeh server to fork new workers.

Public Methods:

__init__(*, filename[, argv])

Keywords:

modify_document(doc)

Execute the configured main.py or main.ipynb to modify the document.

on_server_loaded(server_context)

Execute on_server_unloaded` from server_lifecycle.py (if it is defined) when the server is first started.

on_server_unloaded(server_context)

Execute on_server_unloaded from server_lifecycle.py (if it is defined) when the server cleanly exits.

on_session_created(session_context)

Execute on_session_created from server_lifecycle.py (if it is defined) when a new session is created.

on_session_destroyed(session_context)

Execute on_session_destroyed from server_lifecycle.py (if it is defined) when a session is destroyed.

process_request(request)

Processes incoming HTTP request returning a dictionary of additional data to add to the session_context.

url_path()

The last path component for the basename of the path to the configured directory.

Inherited from : py: class:Handler

__init__(*, filename[, argv])

Keywords:

modify_document(doc)

Execute the configured main.py or main.ipynb to modify the document.

on_server_loaded(server_context)

Execute on_server_unloaded` from server_lifecycle.py (if it is defined) when the server is first started.

on_server_unloaded(server_context)

Execute on_server_unloaded from server_lifecycle.py (if it is defined) when the server cleanly exits.

on_session_created(session_context)

Execute on_session_created from server_lifecycle.py (if it is defined) when a new session is created.

on_session_destroyed(session_context)

Execute on_session_destroyed from server_lifecycle.py (if it is defined) when a session is destroyed.

process_request(request)

Processes incoming HTTP request returning a dictionary of additional data to add to the session_context.

static_path()

Return a path to app-specific static resources, if applicable.

url_path()

The last path component for the basename of the path to the configured directory.


__init__(*, filename: PathLike, argv: list[str] = []) None[source]#
Keywords:

filename (str) : a path to an application directory with either “main.py” or “main.ipynb”

argv (list[str], optional) : a list of string arguments to make available as sys.argv to main.py

modify_document(doc: Document) None[source]#

Execute the configured main.py or main.ipynb to modify the document.

This method will also search the app directory for any theme or template files, and automatically configure the document with them if they are found.

on_server_loaded(server_context: ServerContext) None[source]#

Execute on_server_unloaded` from server_lifecycle.py (if it is defined) when the server is first started.

Parameters:

server_context (ServerContext) –

on_server_unloaded(server_context: ServerContext) None[source]#

Execute on_server_unloaded from server_lifecycle.py (if it is defined) when the server cleanly exits. (Before stopping the server’s IOLoop.)

Parameters:

server_context (ServerContext) –

Warning

In practice this code may not run, since servers are often killed by a signal.

on_session_created(session_context: SessionContext) Coroutine[Any, Any, None][source]#

Execute on_session_created from server_lifecycle.py (if it is defined) when a new session is created.

Parameters:

session_context (SessionContext) –

on_session_destroyed(session_context: SessionContext) Coroutine[Any, Any, None][source]#

Execute on_session_destroyed from server_lifecycle.py (if it is defined) when a session is destroyed.

Parameters:

session_context (SessionContext) –

process_request(request: HTTPServerRequest) dict[str, Any][source]#

Processes incoming HTTP request returning a dictionary of additional data to add to the session_context.

Parameters:

request – HTTP request

Returns:

A dictionary of JSON serializable data to be included on the session context.

url_path() str | None[source]#

The last path component for the basename of the path to the configured directory.

property error: str | None#

If the handler fails, may contain a related error message.

property error_detail: str | None#

If the handler fails, may contain a traceback or other details.

property failed: bool#

True if the handler failed to modify the doc

property safe_to_fork: bool#

Whether it is still safe for the Bokeh server to fork new workers.

False if the configured code (script, notebook, etc.) has already been run.