Script a solution#

Warning

The following assumes that:

  • The solution has been created using SAF CLI.

  • The solution virtual environment has been installed using the SAF CLI.

  • The solution is running on the local machine.

  • The script is created / downloaded in the solution root directory.

This is temporary until an end-to-end workflow that includes the initialization of the solution API is provided.

The examples described here demonstrate how to use GLOW’s Client from ansys.saf.glow.client to interact with a running Solution API server.

Basic interaction with the solution API#

The first section of this guide provides a basic example of how to interact with a SAF based solution using the GLOW API client.

Start the solution#

To start the solution API, the following SAF CLI command can be used:

saf run my_solution --no-ui --solution-api-port 50000

It starts the solution API without the UI, listening on port 50000 for the requests that are made in the example.

For more information on how to run a solution, see Run a solution.

Run the example script#

The example script can be run with SAF CLI, which automatically activates the solution virtual environment.

The full example script can be downloaded here: solution_example_script.py. It contains all the code snippets provided in this guide. To execute it, open a new terminal and run the following command:

saf execute my_solution "python solution_example_script.py"

Perform imports and define the GLOW API server URL#

Perform required imports.

from pathlib import Path

from ansys.solutions.my_solution.solution.definition import (  # type: ignore  # pyright: reportMissingImports=false
    MySolution,
)

from ansys.saf.glow.client import Client  # pyright: ignore[reportMissingImports]

Define the GLOW API server URL.

GLOW_API_SERVER_URL = "http://localhost:50000"

Create a GLOW client#

Create a GLOW client to interact with the solution backend.

client = Client(MySolution, GLOW_API_SERVER_URL)

List existing projects#

all_projects = client.list_projects()

Get an existing project#

project_a = client.get_project("projects/6638ce7177e87f72ac4bbab3")

Upgrade an existing project#

project_a = client.upgrade_project("projects/6638ce7177e87f72ac4bbab3")

Delete an existing project#

client.delete_project("projects/6638ce7177e87f72ac4bbab3")

Create a new project#

project_a = client.create_project("Project A")

Get step fields#

print(f"first_arg: {project_a.steps.first_step.first_arg}")
print(f"second_arg: {project_a.steps.first_step.second_arg}")

Set step fields#

project_a.steps.first_step.first_arg = 1
project_a.steps.first_step.second_arg = 2

Run the calculate method#

project_a.steps.first_step.calculate()

Get the calculation result#

print(f"result: {project_a.steps.first_step.result}")

Handle method execution exceptions#

In case the method fails, an exception with information about the failure is raised. For more information, check the Client API documentation.

Shut down the GLOW API server#

To shut down the GLOW API server, focus on the terminal where the solution API is running and press Ctrl+C. This stops the server and terminate the script. If prompted to confirm the termination of the batch job, type Y and press Enter.

Manage projects#

This section provides an example of how to manage projects using the GLOW API client. For more details on how to manage projects, check Solution projects API.

Export the project#

project_a.export(destination=Path.cwd())

Delete the project#

project_a.delete()

Import the project#

project_b = client.import_project(safx_path=Path.cwd() / "Project A.safx", display_name="Project B")

Access the project properties#

# Get the project URL
print(f"Project URL: {project_b.url}")
# Get the creation date of the project
print(f"Project creation date: {project_b.date_created}")
# Get the last modification date of the project
print(f"Project last modification date: {project_b.date_modified}")

Modify the project display name#

project_b.modify_project_display_name(display_name="Project C")

Delete the imported project#

project_b.delete()

Create and manage multiple projects#

This section provides an example of how to create and manage multiple projects using the GLOW Client.

Create a matrix of projects with different arguments#

PROJECT_MATRIX = {
    "Project A": {"first_arg": 1, "second_arg": 2, "result": None, "name": None},
    "Project B": {"first_arg": 10, "second_arg": 25, "result": None, "name": None},
    "Project C": {"first_arg": 5, "second_arg": 3, "result": None, "name": None},
}

Create multiple projects using a loop#

for project_display_name, args in PROJECT_MATRIX.items():
    # Create the project
    project = client.create_project(project_display_name)
    # Set the step fields
    project.steps.first_step.first_arg = args["first_arg"]
    project.steps.first_step.second_arg = args["second_arg"]

    # Run the calculate method
    project.steps.first_step.calculate()

    # Store the result
    args["result"] = project.steps.first_step.result

    # Store the project name for later use
    args["name"] = "projects/" + project.url.split("/")[-1]

Get the available projects list#

projects = client.list_projects()

Example output of the projects list:

[
    {
        "display_name": "Project A",
        "date_created": "2025-07-07T10:44:10.790164",
        "date_modified": "2025-07-07T10:44:10.790164",
        "name": "projects/6867daaa13ae5d5ca473900d"
    },
    {
        "display_name": "Project B",
        "date_created": "2025-07-07T12:41:01.922904",
        "date_modified": "2025-07-07T12:41:01.922904",
        "name": "projects/686ba43d13ae5d8544081b3b"
    },
    {
        "display_name": "Project C",
        "date_created": "2025-07-07T12:45:10.235752",
        "date_modified": "2025-07-07T12:47:23.124312",
        "name": "projects/686ba43d13ae5d8544081b3b"
    }
]

Delete all created projects#

for args in PROJECT_MATRIX.values():
    project = client.get_project(args["name"])
    project.delete()