{ "cells": [ { "cell_type": "markdown", "id": "811f637d", "metadata": {}, "source": [ "# Getting started\n", "This example shows how to connect to Granta MI and perform basic operations on record lists." ] }, { "cell_type": "markdown", "id": "f0860624", "metadata": {}, "source": [ "## Connect to Granta MI" ] }, { "cell_type": "markdown", "id": "e70609f4", "metadata": {}, "source": [ "First, use the ``ansys.grantami.recordlists.Connection`` class to connect to the Granta MI\n", "server. The ``Connection`` class uses a fluent interface to build the connection, which is\n", "always invoked in the following sequence:\n", "\n", "1. Specify your Granta MI Service Layer URL as a parameter to the ``Connection`` class.\n", "2. Specify the authentication method using a ``Connection.with_...()`` method.\n", "3. Use the ``Connection.connect()`` method to finalize the connection.\n", "\n", "This returns a client object, called ``client`` in these examples." ] }, { "cell_type": "code", "execution_count": null, "id": "2278535c", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ansys.grantami.recordlists import Connection, RecordListItem\n", "\n", "connection = Connection(\"http://my_grantami_server/mi_servicelayer\").with_autologon()\n", "client = connection.connect()" ] }, { "cell_type": "markdown", "id": "2343716b", "metadata": {}, "source": [ "## Create a new record list\n", "API operations expect a ``RecordList`` instance as input. For the purpose of this example, create\n", "a new record list, using the ``create_list`` method. It returns a ``RecordList`` instance." ] }, { "cell_type": "code", "execution_count": null, "id": "9c408237", "metadata": { "tags": [] }, "outputs": [], "source": [ "example_list = client.create_list(\n", " name=\"Example list\",\n", " description=f\"Created by example 00_Basic_usage\",\n", ")\n", "example_list" ] }, { "cell_type": "markdown", "id": "ad0851c9", "metadata": {}, "source": [ "## Get the details of an existing record list\n", "The ``get_list`` method allows obtaining a ``RecordList`` instance from an existing record list\n", "unique identifier. This can be useful when the identifier is already known, for example if it has\n", "been obtained from the Favorites application or if it is hard-coded in the script configuration." ] }, { "cell_type": "code", "execution_count": null, "id": "f9695113", "metadata": { "tags": [] }, "outputs": [], "source": [ "list_details = client.get_list(example_list.identifier)\n", "list_details" ] }, { "cell_type": "code", "execution_count": null, "id": "dfc76433", "metadata": { "tags": [] }, "outputs": [], "source": [ "print(f\"Name: {list_details.name}\")\n", "print(f\"Identifier: {list_details.identifier}\")\n", "print(f\"Notes: {list_details.notes}\")\n", "print(f\"Description: {list_details.description}\")\n", "print(f\"Created timestamp: {list_details.created_timestamp}\")" ] }, { "cell_type": "markdown", "id": "6590cdc8", "metadata": {}, "source": [ "## Get all record lists" ] }, { "cell_type": "code", "execution_count": null, "id": "a5b0b95b", "metadata": { "tags": [] }, "outputs": [], "source": [ "all_lists = client.get_all_lists()\n", "all_lists" ] }, { "cell_type": "markdown", "id": "356fefe1", "metadata": {}, "source": [ "## Copy a record list" ] }, { "cell_type": "code", "execution_count": null, "id": "027e1fa9", "metadata": { "tags": [] }, "outputs": [], "source": [ "list_copy = client.copy_list(example_list)\n", "list_copy" ] }, { "cell_type": "markdown", "id": "62cf5263", "metadata": {}, "source": [ "## Update a record list" ] }, { "cell_type": "code", "execution_count": null, "id": "c9eff560", "metadata": { "tags": [] }, "outputs": [], "source": [ "updated_list_copy = client.update_list(\n", " list_copy,\n", " name=\"Copied - Example List\",\n", " description=None,\n", " notes=\"Copy of the example list\",\n", ")\n", "print(f\"Name: {updated_list_copy.name}\")\n", "print(f\"Identifier: {updated_list_copy.identifier}\")\n", "print(f\"Notes: {updated_list_copy.notes}\")\n", "print(f\"Description: {updated_list_copy.description}\")\n", "print(f\"Created timestamp: {updated_list_copy.created_timestamp}\")" ] }, { "cell_type": "markdown", "id": "76f34255", "metadata": {}, "source": [ "## Delete a record list" ] }, { "cell_type": "code", "execution_count": null, "id": "a33c25d0", "metadata": { "tags": [] }, "outputs": [], "source": [ "client.delete_list(updated_list_copy)" ] }, { "cell_type": "markdown", "id": "61334459", "metadata": {}, "source": [ "## Read the items in a record list\n", "The list was created at the beginning of this example, so the list is currently empty." ] }, { "cell_type": "code", "execution_count": null, "id": "3d3b3119", "metadata": { "tags": [] }, "outputs": [], "source": [ "items = client.get_list_items(example_list)\n", "items" ] }, { "cell_type": "markdown", "id": "d4e47e7e", "metadata": {}, "source": [ "## Add items to a record list\n", "Add items to a list using ``add_items_to_list``.\n", "Items are described using the database GUID, table GUID, and record history GUID." ] }, { "cell_type": "code", "execution_count": null, "id": "7aa6de58", "metadata": { "tags": [] }, "outputs": [], "source": [ "client.add_items_to_list(\n", " example_list,\n", " items=[\n", " RecordListItem(\n", " database_guid=\"e595fe23-b450-4d18-8c08-4a0f378ef095\",\n", " table_guid=\"81dff531-0254-4fbe-9621-174b10aaee3d\",\n", " record_history_guid=\"3bc2b82f-0199-4f3b-a7af-8d520250b180\",\n", " ),\n", " ],\n", ")" ] }, { "cell_type": "markdown", "id": "4e4ca749", "metadata": {}, "source": [ "Then retrieve the items and confirm that the record list now includes the added items." ] }, { "cell_type": "code", "execution_count": null, "id": "7a94a069", "metadata": { "tags": [] }, "outputs": [], "source": [ "list_items = client.get_list_items(example_list)\n", "list_items" ] }, { "cell_type": "markdown", "id": "a399af43", "metadata": {}, "source": [ "## Remove items from a record list\n", "Remove items from a record list using ``remove_items_from_list``." ] }, { "cell_type": "code", "execution_count": null, "id": "9efb8cfb", "metadata": { "tags": [] }, "outputs": [], "source": [ "client.remove_items_from_list(\n", " example_list,\n", " items=list_items,\n", ")" ] }, { "cell_type": "markdown", "id": "5e9f23e0", "metadata": {}, "source": [ "Then retrieve the items again and confirm that the record list is empty." ] }, { "cell_type": "code", "execution_count": null, "id": "aaaf1bb1", "metadata": { "lines_to_next_cell": 2, "tags": [] }, "outputs": [], "source": [ "items = client.get_list_items(example_list)\n", "items" ] }, { "cell_type": "code", "execution_count": null, "id": "e4b6be35", "metadata": { "nbsphinx": "hidden" }, "outputs": [], "source": [ "client.delete_list(example_list)" ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:light" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }