-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analyse): notebook with basic Grist data download #93
- Loading branch information
1 parent
27b3bef
commit 7e7655b
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 | ||
} |