{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Get Started \n",
    "Load MI Scripting Toolkit, connect to your MI Session, and select a database and table."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to MI\n",
    "Import the `granta` libraries, then connect to MI via the Service Layer using Windows authentication."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from GRANTA_MIScriptingToolkit import granta as mpy\n",
    "\n",
    "mi = mpy.connect(\"http://localhost/mi_servicelayer\", autologon=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Select a database\n",
    "All the sample scripts use the *MI Training* database."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_db = mi.get_db(db_key=\"MI_Training\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set a unit system, and choose whether to use absolute or relative temperatures (Kelvin/Rankine or Celsius/Fahrenheit)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_db.set_unit_system('UK Imperial', absolute_temperatures=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Select a table\n",
    "Select *MaterialUniverse* and print its number of attributes. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Table MaterialUniverse has 424 attributes defined\n"
     ]
    }
   ],
   "source": [
    "my_table = my_db.get_table(\"MaterialUniverse\")\n",
    "print(\"Table {0} has {1} attributes defined\".format(my_table.name, len(my_table.attributes)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Print the definition of an attribute within your table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The definition of the Density attribute in MaterialUniverse is <Attribute Definition, name:Density, type:RNGE, unit:lb/in^3>\n"
     ]
    }
   ],
   "source": [
    "print(\"The definition of the Density attribute in {0} is {1}\".format(my_table.name, my_table.attributes[\"Density\"]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Find a record\n",
    "Search for a record by name (only exact matches for short or long name will be returned), and print information to help you locate and view it in MI applications."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Finding Aluminum, 7075, wrought, T73...\n"
     ]
    }
   ],
   "source": [
    "print(\"Finding Aluminum, 7075, wrought, T73...\")\n",
    "my_record = my_table.search_for_records_by_name(\"Aluminum, 7075, wrought, T73\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found this record:\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Record long name:Aluminum, 7075, wrought, T73>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"Found this record:\")\n",
    "my_record"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in database/table:\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'MI_Training/MaterialUniverse'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"in database/table:\")\n",
    "my_record.db_key+\"/\"+my_record.table_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "at this point in the tree:\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Metals and alloys=>Non-ferrous=>Aluminum=>Wrought=>7000 series (Zn-alloyed)=>7075'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"at this point in the tree:\")\n",
    "\"=>\".join(my_record.path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "with data like this:\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<RangeValue name: Mg (magnesium), value: {'low': 2.5, 'high': 2.5}, unit: %>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"with data like this:\")\n",
    "my_table.bulk_fetch([my_record], attributes=['Mg (magnesium)'])\n",
    "my_record.attributes['Mg (magnesium)']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "and you can see it in MI Viewer here:\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'http://localhost/mi/datasheet.aspx?dbKey=MI_Training&recordHistoryGuid=7682acfe-46ca-4adf-94f7-6fa678debed1'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"and you can see it in MI Viewer here:\")\n",
    "my_record.viewer_url"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
