{ "cells": [ { "cell_type": "markdown", "id": "ce606ce6", "metadata": {}, "source": [ "# PyGranta RecordLists example" ] }, { "cell_type": "markdown", "id": "df0a7133", "metadata": {}, "source": [ "## Introduction" ] }, { "cell_type": "markdown", "id": "c7ffad20", "metadata": {}, "source": [ "An example that uses the PyGranta Data Flow Extensions package to interact with a Granta MI Record List as part of a\n", "Data Flow step. The code below shows how to add the workflow record to a record list. However, the principles shown\n", "here can be applied to any PyGranta package." ] }, { "cell_type": "markdown", "id": "be39f5db", "metadata": {}, "source": [ "### Useful links\n", "* [Recommended script structure](../user_guide/index.rst#recommended-script-structure)\n", "* [Business logic development best practice](../user_guide/index.rst#business-logic-development-best-practice)" ] }, { "cell_type": "markdown", "id": "bf93cf54", "metadata": {}, "source": [ "## Pre-requisites" ] }, { "cell_type": "markdown", "id": "9aca1058", "metadata": {}, "source": [ "This example requires ``ansys-grantami-recordlists``. Install with ``pip install ansys-grantami-recordlists``, or\n", "consult the [Getting started](https://recordlists.grantami.docs.pyansys.com/version/stable/getting_started/index.html)\n", "guide for more details." ] }, { "cell_type": "markdown", "id": "fe1c9190", "metadata": {}, "source": [ "## Example script" ] }, { "cell_type": "code", "execution_count": null, "id": "9fa94837", "metadata": {}, "outputs": [], "source": [ "import logging\n", "import traceback\n", "\n", "from ansys.grantami.recordlists import Connection as RecordListsConnection, RecordListItem\n", "\n", "from ansys.grantami.dataflow_extensions import MIDataflowIntegration\n", "\n", "# Create an instance of the root logger\n", "logger = logging.getLogger()\n", "logger.setLevel(logging.INFO)\n", "\n", "# Add a StreamHandler to write the output to stderr\n", "ch = logging.StreamHandler()\n", "formatter = logging.Formatter(\"%(asctime)s - %(name)s - %(levelname)s - %(message)s\")\n", "ch.setFormatter(formatter)\n", "logger.addHandler(ch)\n", "\n", "\n", "def main():\n", " \"\"\"\n", " Initializes the MI Data Flow integration module, runs the business logic,\n", " and cleans up once execution has completed.\n", " \"\"\"\n", "\n", " # Ansys strongly recommend using HTTPS in production environments.\n", " # If you are using an internal certificate, you should specify the\n", " # CA certificate with certificate_filename=my_cert_file.crt and add the\n", " # certificate to the workflow as a supporting file, or use an absolute\n", " # pathlib.Path object to the file on disk.\n", " # Refer to the MIDataflowIntegration API reference page for more details.\n", " dataflow_integration = MIDataflowIntegration(use_https=False)\n", "\n", " try:\n", " step_logic(dataflow_integration)\n", " exit_code = 0\n", " except Exception:\n", " traceback.print_exc()\n", " exit_code = 1\n", " dataflow_integration.resume_bookmark(exit_code)\n", "\n", "\n", "def testing():\n", " \"\"\"Contains a static copy of a Data Flow data payload for testing purposes\"\"\"\n", " dataflow_payload = {\n", " \"WorkflowId\": \"67eb55ff-363a-42c7-9793-df363f1ecc83\",\n", " \"WorkflowDefinitionId\": \"Example; Version=1.0.0.0\",\n", " \"TransitionName\": \"Python_83e51914-3752-40d0-8350-c096674873e2\",\n", " \"Record\": {\n", " \"Database\": \"MI_Training\",\n", " \"Table\": \"Metals Pedigree\",\n", " \"RecordHistoryGuid\": \"d2f51a3d-c274-4a1e-b7c9-8ba2976202cc\",\n", " },\n", " \"WorkflowUrl\": \"http://my_server_name/mi_dataflow\",\n", " \"AuthorizationHeader\": \"\",\n", " \"ClientCredentialType\": \"Windows\",\n", " \"Attributes\": {\n", " \"Record\": {\"Value\": [\"d2f51a3d-c274-4a1e-b7c9-8ba2976202cc+MI_Training\"]},\n", " \"TransitionId\": {\"Value\": \"9f1bf6e7-0b05-4cd3-ac61-1d2d11a1d351\"},\n", " },\n", " \"CustomValues\": {},\n", " }\n", "\n", " # Call MIDataflowIntegration constructor with \"dataflow_payload\" argument\n", " # instead of reading data from Data Flow.\n", " dataflow_integration = MIDataflowIntegration.from_dict_payload(\n", " dataflow_payload=dataflow_payload,\n", " use_https=False,\n", " )\n", " step_logic(dataflow_integration)\n", "\n", "\n", "# These GUIDs cannot be determined by a PyGranta package, so they are hardcoded\n", "# Alternatively, they could be accessed via the Granta MI Scripting Toolkit\n", "DATABASE_GUID = \"43a43640-4919-428a-bac9-16efbc4ce6ed\" # MI_Training\n", "TABLE_GUID = \"ad27baf0-42e9-4136-bc96-9dbbf116e265\" # Metals Pedigree\n", "\n", "# The Record List name could alternatively be provided as a \"Custom Script Parameter\"\n", "# in Data Flow Designer\n", "RECORD_LIST_NAME = \"Data Flow List\"\n", "\n", "\n", "def step_logic(dataflow_integration):\n", " \"\"\"Contains the business logic to be executed as part of the workflow.\n", "\n", " In this example, get a record list with the required name and add\n", " the data flow record to the list.\n", "\n", " Replace the code in this module with your custom business logic.\n", " \"\"\"\n", " connection = dataflow_integration.configure_pygranta_connection(RecordListsConnection)\n", " client = connection.connect()\n", "\n", " try:\n", " record_list = next(rl for rl in client.get_all_lists() if rl.name == RECORD_LIST_NAME)\n", " except StopIteration:\n", " ValueError(f'Could not find record list with name \"{RECORD_LIST_NAME}\"')\n", " return\n", "\n", " payload = dataflow_integration.get_payload_as_dict()\n", " new_item = RecordListItem(\n", " database_guid=DATABASE_GUID,\n", " table_guid=TABLE_GUID,\n", " record_history_guid=payload[\"Record\"][\"RecordHistoryGuid\"],\n", " )\n", " client.add_items_to_list(record_list=record_list, items=[new_item])\n", " logger.info(\"Added item to list\") # This output will be visible in the api/logs page\n", "\n", "\n", "if __name__ == \"__main__\":\n", " # main() # Used when running the script as part of a workflow\n", " testing() # Used when testing the script manually" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }