Created
February 12, 2022 17:50
-
-
Save symbioquine/7641a2ab258726347ec937e8ea02a167 to your computer and use it in GitHub Desktop.
farmOS + JupyterLite: Import a CSV of Animals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"language_info": { | |
"codemirror_mode": { | |
"name": "python", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8" | |
}, | |
"kernelspec": { | |
"name": "python", | |
"display_name": "Pyolite", | |
"language": "python" | |
} | |
}, | |
"nbformat_minor": 4, | |
"nbformat": 4, | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"source": "# From https://gist.github.com/bollwyvl/132aaff5cdb2c35ee1f75aed83e87eeb\nasync def get_contents(path):\n \"\"\"use the IndexedDB API to acess JupyterLite's in-browser (for now) storage\n \n for documentation purposes, the full names of the JS API objects are used.\n \n see https://developer.mozilla.org/en-US/docs/Web/API/IDBRequest\n \"\"\"\n import js, asyncio\n\n DB_NAME = \"JupyterLite Storage\"\n\n # we only ever expect one result, either an error _or_ success\n queue = asyncio.Queue(1)\n \n IDBOpenDBRequest = js.self.indexedDB.open(DB_NAME)\n IDBOpenDBRequest.onsuccess = IDBOpenDBRequest.onerror = queue.put_nowait\n \n await queue.get()\n \n if IDBOpenDBRequest.result is None:\n return None\n \n IDBTransaction = IDBOpenDBRequest.result.transaction(\"files\", \"readonly\")\n IDBObjectStore = IDBTransaction.objectStore(\"files\")\n IDBRequest = IDBObjectStore.get(path, \"key\")\n IDBRequest.onsuccess = IDBRequest.onerror = queue.put_nowait\n \n await queue.get()\n \n return IDBRequest.result.to_py() if IDBRequest.result else None\n", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 8, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": "import io\nimport pandas\n\nanimals = pandas.read_csv(io.StringIO((await get_contents(\"animals.csv\"))[\"content\"]))\n\nanimals", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"execution_count": 15, | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": " animal_name animal_dob animal_sex\n0 alice 2021/01/18 F\n1 bob 2021/03/12 M\n2 curt 2020/05/01 M\n3 dolly 2021/06/08 F", | |
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>animal_name</th>\n <th>animal_dob</th>\n <th>animal_sex</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>alice</td>\n <td>2021/01/18</td>\n <td>F</td>\n </tr>\n <tr>\n <th>1</th>\n <td>bob</td>\n <td>2021/03/12</td>\n <td>M</td>\n </tr>\n <tr>\n <th>2</th>\n <td>curt</td>\n <td>2020/05/01</td>\n <td>M</td>\n </tr>\n <tr>\n <th>3</th>\n <td>dolly</td>\n <td>2021/06/08</td>\n <td>F</td>\n </tr>\n </tbody>\n</table>\n</div>" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": "from pyodide.http import pyfetch\nfrom js import location\n\nresp = await pyfetch(location.origin + '/session/token', method='POST')\n\nprint(resp.ok, resp.status, resp.status_text)\n\nantiCsrfToken = await resp.string()\n\nheaders = {\n 'Content-type': \"application/vnd.api+json\",\n 'X-CSRF-Token': antiCsrfToken,\n}", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 62, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"text": "True 200 OK\n", | |
"output_type": "stream" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": "resp = await pyfetch(location.origin + '/api/taxonomy_term/animal_type?filter[name]=Sheep', method='GET')\n\ndata = await resp.json()\n\nterms = data['data']\n\nif not len(terms):\n resp = await pyfetch(location.origin + '/api/taxonomy_term/animal_type', method='POST', body=json.dumps({\n \"data\": {\n \"type\": \"taxonomy_term--animal_type\",\n \"attributes\": {\n \"name\": \"Sheep\",\n },\n },\n }), headers=headers)\n\n print(resp.ok, resp.status, resp.status_text)\n\n data = await resp.json()\n\n terms = [data['data']]\n\nsheep_animal_type_term = terms[0]", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 72, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": "import datetime\nimport json\n\nfor idx, animal in animals.iterrows():\n resp = await pyfetch(location.origin + '/api/asset/animal', method='POST', body=json.dumps({\n \"data\": {\n \"type\": \"asset--animal\",\n \"attributes\": {\n \"name\": animal['animal_name'],\n \"sex\": animal['animal_sex'],\n \"birthdate\": datetime.datetime.strptime(animal['animal_dob'], \"%Y/%m/%d\").strftime('%Y-%m-%dT%H:%M:%S+00:00'),\n },\n \"relationships\": {\n \"animal_type\": {\n \"data\": {\n \"type\": sheep_animal_type_term['type'],\n \"id\": sheep_animal_type_term['id'],\n },\n },\n }\n }\n }), headers=headers)\n\n print(animal['animal_name'], resp.ok, resp.status, resp.status_text)", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 70, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"text": "alice True 201 Created\nbob True 201 Created\ncurt True 201 Created\ndolly True 201 Created\n", | |
"output_type": "stream" | |
} | |
] | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animal_name | animal_dob | animal_sex | |
---|---|---|---|
alice | 2021/01/18 | F | |
bob | 2021/03/12 | M | |
curt | 2020/05/01 | M | |
dolly | 2021/06/08 | F |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment