Trame Backend

class streamlit_pyvista.trame_viewers.trame_backend.TrameBackend(plotter=None, server=None, port=8080, host='0.0.0.0')[source]

Bases: ABC

A Trame server class that manage the view of a 3d mesh and its controls

__init__(plotter=None, server=None, port=8080, host='0.0.0.0')[source]

Initialize the trame server backend.

Parameters:
  • plotter (pv.Plotter, optional) – The plotter object for visualization. Defaults to None.

  • server (Server, optional) – The server object for handling client connections. Defaults to None.

  • port (int, optional) – The port number for the server. Defaults to 8080.

  • host (str, optional) – The host address for the server. Defaults to “0.0.0.0”.

async client_number(request)[source]
async kill_server(request)[source]

Stops the server and returns a JSON response indicating success.

Parameters:

request – The request object.

Returns:

A JSON response with a success message and a status code of 200.

property state
property ctrl
async change_mesh(request)[source]

This function is called when a request to ‘/select_mesh’ is made

Parameters:

request – the request received

Returns:

a http status 200 if there was no error, else a http status 400

Return type:

web.Response

Note

This function require the request received to have a json body with the following fields:
  • mesh_list: the paths (or the link) of the mesh to load

  • width: the width of the plotter

  • height: the height of the plotter

  • nbr_frames: the number of frames in the sequence

async upload_mesh(request)[source]

This function is called when a request to ‘/upload_mesh’ is made

Parameters:

request – The request object containing the mesh data.

Returns:

A JSON response indicating the success of the upload.

Return type:

web.Response

on_server_bind(wslink_server)[source]

When the server is bind, add api endpoint to it :type wslink_server: :param wslink_server: the socket manager of the server

on_client_exited()[source]

Handles the event when a client exits.

Decreases the client counter and logs the event. If there are no more clients connected, it prints a message indicating that a client disconnected.

on_client_connected()[source]

This method is called when a client connects to the Trame server. It increments the client counter and logs the connection details.

on_server_exited(**kwargs)[source]

Callback function called when the server has exited.

async init_connection(request)[source]

Base api endpoint on ‘/init_connection’ to inform the client of all the endpoints available and their locations.

Parameters:

request – the request made to this endpoint

Returns:

a json with all information about endpoints required and a success status 200

Return type:

web.Response

async start()[source]

Starts the Trame server and waits for it to finish.

async request_stop(force_stop=False)[source]

Stops the server if there are no active clients, otherwise schedules a delayed call to stop.

If there are no active clients connected to the server, the server is stopped immediately and the shutdown_event is set. Otherwise, a delayed call to request_stop is scheduled using threading.Timer and asyncio.get_running_loop().call_soon(asyncio.create_task, self.request_stop()).

Parameters:

force_stop (bool) – Force the request to immediately stop the server

run_async_function(async_func)[source]
signal_handler(sig, frame)[source]

Handles the specified signal and initiates the shutdown process.

Parameters:

sig (int) – The signal number. frame (FrameType): The current stack frame.

async run()[source]

Runs the Trame server.

This method sets up signal handlers for interrupt and termination signals, and then starts the Trame server