{ "cells": [ { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "# hide ssl warnings for this test.\n", "import requests\n", "requests.packages.urllib3.disable_warnings()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Reading data with python-fmrest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a short example on how to login, get a foundset from a database layout, read field values, and eventually logout." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import the module" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "scrolled": false }, "outputs": [], "source": [ "import fmrest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create the server instance" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "fms = fmrest.Server('https://10.211.55.15',\n", " user='admin',\n", " password='admin',\n", " database='Contacts',\n", " layout='Demo',\n", " verify_ssl=False\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Login" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The login method obtains the access token." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d4a1304c20b20390f1dca3dac190a8bb0fe5b5aca5d7c40562d4'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fms.login()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get a foundset from the database/layout" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foundset = fms.get_records(limit=2)\n", "foundset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we have a foundset instance we can iterate over:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n" ] } ], "source": [ "for record in foundset:\n", " print(record)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have two records in our foundset. Let's see what is in them:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Inspect a record instance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's look at the available keys (fields) of the first record in the foundset:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "record = foundset[0]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['id',\n", " 'name',\n", " 'drink',\n", " 'portrait',\n", " 'creation',\n", " 'modification',\n", " 'recordId',\n", " 'modId',\n", " 'portal_notes']" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "record.keys()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, if we want to get the name, we just access it via the attribute:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'David'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "record.name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...or via the key:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Coffee'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "record['drink']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What about portals?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By looking at the keys, we can see that we also have portals on our layout (keys starting with \"portal_\"). Let's look at `portal_notes`.\n", "\n", "It is, again, a foundset instance." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "portal = foundset[0].portal_notes\n", "portal" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could go through the rows like this\n", "```\n", "for row in portal:\n", " print(row)\n", "```\n", "Or access a particular row directly:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "row = portal[0]\n", "row" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We get back a record instance just like the ones before. Note, though, that we access fields in portal rows with the table occurrence prefix (just like in FileMaker Pro):" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'A note for David.'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "row['Notes::note']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Logout" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's logout and destroy our opened session." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fms.logout()" ] } ], "metadata": { "kernelspec": { "display_name": "fmrest venv", "language": "python", "name": "venv" }, "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.2" } }, "nbformat": 4, "nbformat_minor": 2 }