moleculekit.viewer.molstar.cdp module#

Minimal Chrome DevTools Protocol client over a raw WebSocket.

Only the standard library is used. This exists so headless rendering needs no browser-automation dependency: the sandbox images moleculekit runs in ship a chromium binary but neither playwright nor node.

class moleculekit.viewer.molstar.cdp.WS(url, timeout=300.0)#

Bases: object

A CDP session over one WebSocket connection.

call(method, params=None)#

Send a CDP command and return its result, ignoring unrelated events.

Parameters:
  • method (str) – The CDP method name (e.g., ‘Page.navigate’).

  • params (dict | None) – Method parameters.

Returns:

result – The result object from the CDP response.

Return type:

dict

Raises:

RuntimeError – If the CDP endpoint returns an error.

close()#

Close the WebSocket connection.

Return type:

None

evaluate(expression, timeout_ms=300000)#

Evaluate JavaScript in the page and return its value.

Promises are awaited. A JavaScript exception is re-raised here rather than returning a value, so a failed render cannot masquerade as a successful one.

set_timeout(timeout)#

Change the socket’s read timeout for subsequent calls.

Parameters:

timeout (float) – New socket timeout in seconds.

Return type:

None

moleculekit.viewer.molstar.cdp.encode_frame(payload)#

Encode a payload as a masked client-to-server WebSocket text frame.

Parameters:

payload (bytes) – The UTF-8 encoded message body.

Returns:

frame – The complete frame, including header and masking key.

Return type:

bytes

moleculekit.viewer.molstar.cdp.page_target_url(port, timeout=30.0)#

Wait for devtools to come up and return the first page target’s ws url.

Parameters:
  • port (int) – The port chromium was told to serve the devtools protocol on.

  • timeout (float) – Seconds to keep polling before giving up.

Returns:

url – The websocket debugger url of the first page target.

Return type:

str

Raises:

TimeoutError – If no page target appears within timeout.