moleculekit.viewer.molstar.server module#

Singleton HTTP server with SSE for the molstar viewer.

class moleculekit.viewer.molstar.server.ServerState(port, session, httpd, server_thread, monitor_thread, registry, subscribers, subscribers_lock, stop_event, handler_threads)#

Bases: object

Runtime state of the singleton molstar viewer server.

port#

The TCP port the HTTP server is bound to on localhost.

Type:

int

session#

Per-server session token; SSE clients must present it to subscribe.

Type:

str

httpd#

The running HTTP server instance.

Type:

ThreadingHTTPServer

server_thread#

Daemon thread running httpd.serve_forever.

Type:

threading.Thread

monitor_thread#

Daemon thread polling the registry for changes and broadcasting events.

Type:

threading.Thread

registry#

The slot registry of molecules being served.

Type:

Registry

subscribers#

One queue per connected SSE client, fed serialized event payloads.

Type:

list of queue.Queue

subscribers_lock#

Guards subscribers and handler_threads.

Type:

threading.Lock

stop_event#

Set on shutdown to stop the monitor loop and SSE handlers.

Type:

threading.Event

handler_threads#

Threads currently serving SSE streams, joined during shutdown.

Type:

set of threading.Thread

handler_threads: set[Thread]#
httpd: ThreadingHTTPServer#
monitor_thread: Thread#
port: int#
registry: Registry#
server_thread: Thread#
session: str#
stop_event: Event#
subscribers: list[Queue]#
subscribers_lock: allocate_lock#
moleculekit.viewer.molstar.server.get_or_start_server(open_browser=True)#

Start the singleton server on first call; subsequent calls are no-ops.

Parameters:

open_browser (bool) – Whether to open a browser tab to the viewer when the server first starts. Defaults to True.

Returns:

state – The running server state (the same instance on every call).

Return type:

ServerState

moleculekit.viewer.molstar.server.get_registry()#

Return the running server’s slot registry, starting the server if needed.

Returns:

registry – The singleton server’s slot registry.

Return type:

Registry

moleculekit.viewer.molstar.server.register(mol)#

Register a Molecule with the running server and broadcast it.

Starts the server if it is not already running, registers mol in the registry and broadcasts a topology event to connected viewers.

Parameters:

mol (Molecule) – The molecule to register and display.

Returns:

uid – The slot uuid assigned to mol.

Return type:

str

moleculekit.viewer.molstar.server.shutdown_for_tests()#
Return type:

None

moleculekit.viewer.molstar.server.start_for_tests(open_browser=False)#
Return type:

ServerState