Skip to content

Commit

Permalink
feat(analyse): notebook with basic Grist data download #93
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinMaudry committed May 19, 2023
1 parent 27b3bef commit 7e7655b
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions analyse/notebooks/extract_grist/extract_grist.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import os\n",
"GRIST_API_KEY=''\n",
"grist_api='https://grist.incubateur.anct.gouv.fr/api'\n",
"\n",
"data = requests.get(grist_api + '/orgs', headers={'Authorization': f'Bearer {GRIST_API_KEY}'})\n",
"\n",
"try:\n",
" data.json()[0]['name']\n",
"except:\n",
" print(\"Something went wrong: host name, API key...\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get list of documents from a workspace ID\n",
"\n",
"import pprint\n",
"pp = pprint.PrettyPrinter(indent=4)\n",
"workspaceId = '1679'\n",
"\n",
"def get_documents_of_workspace(id: str) -> list:\n",
" workspace = requests.get(grist_api + f'/workspaces/{workspaceId}',\n",
" headers={'Authorization': f'Bearer {GRIST_API_KEY}'})\n",
" return workspace.json()[\"docs\"]\n",
" \n",
"\n",
"pp.pprint(get_workspace_details(workspaceId))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get list of tables from a document ID\n",
"\n",
"doc_id = 'aH6bfmYwwjpNJ3oqvLKrfx'\n",
"\n",
"def get_tables_from_document(doc_id: str) -> list:\n",
" tables = requests.get(grist_api + f'/docs/{doc_id}/tables',\n",
" headers={'Authorization': f'Bearer {GRIST_API_KEY}'})\n",
" return tables.json()['tables']\n",
"\n",
"get_tables_from_document(doc_id)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get a dataframe from a document ID and a table ID\n",
"\n",
"from io import StringIO\n",
"import pandas as pd\n",
"\n",
"table_id='Import_de_structures_53__Mayenne'\n",
"\n",
"def get_dataframe_of_table(doc_id: str, table_id: str) -> pd.Dataframe:\n",
" csv = requests.get(grist_api + f'/docs/{doc_id}/download/csv?tableId={table_id}',\n",
" headers={'Authorization': f'Bearer {GRIST_API_KEY}'})\n",
" return pd.read_csv(StringIO(csv.text), dtype=str)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 7e7655b

Please sign in to comment.