Skip to content
138 changes: 0 additions & 138 deletions src/ansys/visor/viewer/cli/apis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""API client for controlling a Visor server."""

import json
import os
import shutil
import time
Expand Down Expand Up @@ -49,17 +48,6 @@ def start(self):
# Note: uvicorn.run() blocks until shutdown.
uvicorn.run(app, host=self.host, port=int(self.port), log_level="info")

def health(self):
"""Print the server health-check response."""
url = f"{self.base}/health"
resp = requests.get(url)
print(resp.json())

def info(self):
"""Print general information about the running server."""
resp = requests.get(f"{self.base}/info")
print(resp.json())

def initialize(self, host, port, rendering_mode, standalone, dark_mode, start=False):
Comment thread
LKasianAnsys marked this conversation as resolved.
Resolved
"""Initialize the server with viewer configuration.

Expand Down Expand Up @@ -98,132 +86,6 @@ def initialize(self, host, port, rendering_mode, standalone, dark_mode, start=Fa
resp.raise_for_status()


def list(self):
"""Print the URLs of all currently available viewer instances."""
resp = requests.get(f"{self.base}")
data = resp.json()
if 'urls' in data and data['urls']:
print("Available instances:")
for url in data['urls']:
print(" " + url)


class InstanceAPI:
"""Client for controlling a single Visor viewer instance.

Wraps the instance-level REST endpoints exposed by the Visor server,
allowing callers to start and stop visualizations, manage datasets,
and save or restore viewer state.

Parameters
----------
host : str
Hostname or IP address of the Visor server.
port : int or str
Port number of the Visor server.
"""
def __init__(self, host, port):
binding_host = settings.binding_host or host
scheme = settings.url_scheme
self.base = f"{scheme}://{binding_host}:{port}"

def start(self, file_path, metadata_path, timeout):
"""Start a visualization for the given file.

Parameters
----------
file_path : str
Path to the data file to visualize.
metadata_path : str
Path to the associated metadata file.
timeout : int or float
Maximum number of seconds to wait for the visualization to start.
"""
data = {"file_path": file_path, "metadata": metadata_path, "timeout": timeout}
resp = requests.post(f"{self.base}/start", json=data)
print(resp)
print(resp.json())

def update(self, file_path, metadata_path):
"""Update the current visualization with new data.

Parameters
----------
file_path : str
Path to the updated data file.
metadata_path : str
Path to the updated metadata file.
"""
data = {"file_path": file_path, "metadata": metadata_path}
resp = requests.post(f"{self.base}/update", json=data)
print(resp.json())

def add_dataset(self, file_path, metadata_path):
"""Add an additional dataset to the current visualization.

Parameters
----------
file_path : str
Path to the data file to add.
metadata_path : str
Path to the associated metadata file.
"""
data = {"file_path": file_path, "metadata": metadata_path}
resp = requests.post(f"{self.base}/add_dataset", json=data)
print(resp.json())

def list_datasets(self):
"""Print all datasets currently loaded in the viewer."""
resp = requests.get(f"{self.base}/list_datasets")
print(json.dumps(resp.json().get("datasets", {}), indent=4))

def remove_dataset(self, dataset_id: int):
"""Remove a dataset from the current visualization.

Parameters
----------
dataset_id : int
Numeric identifier of the dataset to remove.
"""
data = {"dataset_id": dataset_id}
resp = requests.post(f"{self.base}/remove_dataset", json=data)
print(resp.json())

def stop_visualization(self):
"""Stop the active visualization without shutting down the instance."""
resp = requests.post(f"{self.base}/stop_visualization")
print(resp.json())

def stop(self):
"""Stop the viewer instance entirely."""
resp = requests.post(f"{self.base}/stop")
print(resp.json())

def save(self, state_dir):
"""Save the current viewer state to disk.

Parameters
----------
state_dir : str
Directory path where the state files will be written.
"""
data = {"state_dir": state_dir}
resp = requests.post(f"{self.base}/save_state", json=data)
print(resp.json())

def load(self, state_dir):
"""Load a previously saved viewer state from disk.

Parameters
----------
state_dir : str
Directory path containing the state files to load.
"""
data = {"state_dir": state_dir}
resp = requests.post(f"{self.base}/load_state", json=data)
print(resp.json())


class LogsAPI:
"""Utility for browsing and tailing Visor log files.

Expand Down
4 changes: 4 additions & 0 deletions src/ansys/visor/viewer/cli/parse_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def parse_args():
remove_dataset_parser = instance_sub.add_parser("remove", help="Remove dataset from instance")
remove_dataset_parser.add_argument("dataset_id", type=int, help="ID of datasets to remove")

# list variables API
list_variables_parser = instance_sub.add_parser("list_variables", help="List variables loaded in a dataset")
list_variables_parser.add_argument("dataset_id", type=int, help="ID of dataset to list variables for")

# stop_visualization API
instance_sub.add_parser("stop_visualization", help="Stop visualization")

Expand Down
12 changes: 8 additions & 4 deletions src/ansys/visor/viewer/cli/visor_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import requests

from ansys.visor.viewer.cli.apis import (
InstanceAPI,
LogsAPI,
ServerAPI,
)
from ansys.visor.viewer.client.visor_client import VisorCliClient
from ansys.visor.viewer.cli.parse_args import parse_args
from ansys.visor.viewer.config import settings

Expand Down Expand Up @@ -133,10 +133,12 @@ def main():

# Execute the appropriate API action based on the group and action
if args.group == "server":
api = ServerAPI(args.api_host, args.api_port)
if args.action == "start":
api = ServerAPI(args.api_host, args.api_port)
api.start()
elif args.action == "health":
return
api = VisorCliClient(args.api_host, args.api_port)
if args.action == "health":
api.health()
elif args.action == "info":
api.info()
Expand All @@ -154,7 +156,7 @@ def main():
elif args.action == "list":
api.list()
elif args.group == "instance":
api = InstanceAPI(args.api_host, args.api_port)
api = VisorCliClient(args.api_host, args.api_port)
if args.action == "start":
api.start(args.file_path, args.metadata_path, args.timeout)
elif args.action == "update":
Expand All @@ -165,6 +167,8 @@ def main():
api.remove_dataset(args.dataset_id)
elif args.action == "list":
api.list_datasets()
elif args.action == "list_variables":
api.list_variables(args.dataset_id)
elif args.action == "stop_visualization":
api.stop_visualization()
elif args.action == "stop":
Expand Down
Empty file.
Loading
Loading