Creating Visualizers
Visualizers turn experiment history into interactive charts that can be displayed by ARES. A PyAres visualizer is a gRPC service that advertises one or more named visualizations and renders a chart whenever ARES sends the corresponding data. Visualizations in PyAres are powered by the Plotly library, which you can install via pip.
The Core Concept
A visualizer has two parts:
- Visualization metadata: A name, chart type, and optional input or settings schemas that tell ARES what the visualization supports.
- Rendering logic: A Python callback that receives a
VisualizationRequestand returns chart data.
Each request contains the selected visualization name and an ordered list of VisualizationDataPoint objects. Every data point has an integer index and a dictionary of values defined by the visualization input schema.
Key Classes
AresVisualizerService
Initialization (__init__)
Arguments required to create an instance of the service:
name(str): The name of the visualizer service.version(str): The version associated with the visualizer service.description(str): A brief description of the service. Defaults to an empty string.timeout(int): The amount of time, in seconds, ARES will wait for a visualization response. Defaults to 30.use_localhost(bool): IfTrue, binds to localhost. Otherwise, binds to all network interfaces. Defaults toTrue.port(int): The port that the service listens on. Defaults to7084.max_message_size(int): The maximum gRPC message size in megabytes. Defaults to-1, which uses the library default.
Methods
add_visualization(visualization_name, custom_visualization_logic, visualization_input_schema=None, visualization_settings_schema=None, chart_type=""): Advertises a visualization to ARES and registers the callback used to render it.visualization_name(str): A unique, non-empty name for the visualization.custom_visualization_logic(callable): A function that accepts aVisualizationRequestand returns chart data. Synchronous and asynchronous callbacks are supported.visualization_input_schema(dict[str, AresSchemaEntry], optional): Describes the fields expected in each data point'svaluesdictionary.visualization_settings_schema(dict[str, AresSchemaEntry], optional): Describes settings associated with the visualization so ARES can advertise them to the client.chart_type(str, optional): A client-facing label describing the chart, such as"Line"or"Scatter".
add_setting(setting_name, setting_type, default_value=None, optional=True, constraints=None, struct_schema=None, list_element_schema=None, limits=None, description=None): Adds a service setting using the common PyAres service API.set_timeout(new_timeout): Sets the response timeout in seconds.start(wait_for_termination): Starts the visualizer service and begins listening for requests.- If
True(Default): The call blocks the main thread. This is appropriate for standalone scripts. - If
False: The call returns immediately, and your program must keep itself alive while the service is running.
- If
stop(): Stops the service and terminates its connection.
Visualization Objects
VisualizationRequest
The request passed to a visualization callback:
visualization_name(str): The name registered withadd_visualization.visualization_data(list[VisualizationDataPoint]): The data points supplied for rendering, in request order.
VisualizationDataPoint
Represents one indexed row of visualization data:
index(int): The source index or sequence number for the data point.values(dict[str, Any]): The values for the point. Keys should match the advertised input schema.
VisualizationOption
The metadata object used internally when a visualization is registered. Most applications should use AresVisualizerService.add_visualization(...) rather than constructing this object directly.
Supported Callback Results
The callback can return any of the following:
- A JSON string containing the chart definition.
- A mapping, such as a dictionary. PyAres serializes mappings with
json.dumps. - A Plotly
Figure, or another object that exposes ato_json()method returning a string. - An iterable of any of the supported chart results when ARES requests a streamed visualization. Each item is sent as a separate chart response.
If the callback raises an exception or returns an unsupported value, PyAres reports an internal gRPC error to ARES. Keep callbacks focused on transforming the supplied request into serializable chart data.
Example Implementation
The following example registers a Plotly line chart. The callback uses the index field for the x-axis and reads the setpoint and temperature fields from each data point.
from typing import Any
import plotly.graph_objects as go
from PyAres import AresDataType, AresSchemaEntry, AresVisualizerService
from PyAres import VisualizationRequest
def render_temperature_history(request: VisualizationRequest) -> go.Figure:
steps = [point.index for point in request.visualization_data]
setpoints = [point.values.get("setpoint") for point in request.visualization_data]
temperatures = [point.values.get("temperature") for point in request.visualization_data]
figure = go.Figure()
figure.add_trace(go.Scatter(x=steps, y=setpoints, mode="lines+markers", name="Setpoint"))
figure.add_trace(go.Scatter(x=steps, y=temperatures, mode="lines+markers", name="Temperature"))
figure.update_layout(
title="Temperature History",
xaxis_title="Step",
yaxis_title="Value",
)
return figure
if __name__ == "__main__":
service = AresVisualizerService(
name="Temperature Visualizer",
version="1.0.0",
description="Plots temperature and setpoint history.",
)
service.add_visualization(
visualization_name="Temperature History",
custom_visualization_logic=render_temperature_history,
visualization_input_schema={
"setpoint": AresSchemaEntry(AresDataType.NUMBER),
"temperature": AresSchemaEntry(AresDataType.NUMBER),
},
chart_type="Line",
)
service.start()
The callback can also be declared with async def; PyAres resolves the awaitable before serializing the result. For a streamed response, return an iterable of figures or chart JSON values so ARES can receive multiple chart updates from one request.
Choosing a Service Port
The default visualizer port is 7084. If several PyAres services run on the same machine, give each service a distinct port and register the service address in ARES using the selected port.