From 4dac0a7176fe4dd2a022c7b1b9c80ed9901efcbf Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Thu, 21 Dec 2023 10:01:43 +0100 Subject: [PATCH] feat: update notebooks to get credits transactions and balance --- ...xport_current_transactions_to_Excel.ipynb} | 94 ++++++++++++------- .../Naas_Credits_Get_Balance.ipynb | 13 +-- 2 files changed, 69 insertions(+), 38 deletions(-) rename Naas Credits/{Naas_Credits_Export_current_transaction.ipynb => Naas_Credits_Export_current_transactions_to_Excel.ipynb} (67%) rename {Naas => Naas Credits}/Naas_Credits_Get_Balance.ipynb (95%) diff --git a/Naas Credits/Naas_Credits_Export_current_transaction.ipynb b/Naas Credits/Naas_Credits_Export_current_transactions_to_Excel.ipynb similarity index 67% rename from Naas Credits/Naas_Credits_Export_current_transaction.ipynb rename to Naas Credits/Naas_Credits_Export_current_transactions_to_Excel.ipynb index 9294494d14..9242a90dce 100644 --- a/Naas Credits/Naas_Credits_Export_current_transaction.ipynb +++ b/Naas Credits/Naas_Credits_Export_current_transactions_to_Excel.ipynb @@ -19,7 +19,7 @@ "tags": [] }, "source": [ - "# Naas Credits - Export current transaction" + "# Naas Credits - Export current transactions to Excel" ] }, { @@ -30,7 +30,7 @@ "tags": [] }, "source": [ - "**Tags:** #naascredits #export #transaction #current #python #notebook" + "**Tags:** #naascredits #export #transaction #current #excel #naas" ] }, { @@ -63,7 +63,7 @@ "tags": [] }, "source": [ - "**Description:** This notebook will export the current transaction from Naas Credits. It will allow to have a better understanding of the current transaction and to analyze it." + "**Description:** This notebook will export your current transactions (transactions done this month) from Naas Credits API and save it to an Excel file." ] }, { @@ -74,7 +74,8 @@ "tags": [] }, "source": [ - "**References:**\n- [Naas Credits Documentation](https://docs.naascredits.com/)\n- [Naas Credits API Reference](https://api.naascredits.com/reference)" + "**References:**\n", + "- [Naas Credits driver](https://github.com/jupyter-naas/drivers/blob/main/naas_drivers/tools/naas_credits.py)" ] }, { @@ -107,30 +108,31 @@ "papermill": {}, "tags": [] }, - "source": "import requests\nimport json", - "outputs": [] + "outputs": [], + "source": [ + "from naas_drivers import naascredits\n", + "from datetime import datetime\n", + "import pandas as pd" + ] }, { "cell_type": "markdown", - "id": "367bb25c-0161-47b9-9514-611399f0d510", - "metadata": { - "papermill": {}, - "tags": [] - }, + "id": "35b0528a-1021-4cfc-8363-03ecbb0dd08a", + "metadata": {}, "source": [ - "### Setup variables\n- **api_key**: API key provided by Naas Credits. [How to get an API key?](https://docs.naascredits.com/getting-started/api-key)\n- **transaction_id**: ID of the transaction to be exported" + "### Setup variables\n", + "- `file_path`: Excel file path to be saved in your local env." ] }, { "cell_type": "code", "execution_count": null, - "id": "349d0cbe-c9fb-48e6-be73-ad5c0911839a", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": "api_key = \"YOUR_API_KEY\"\ntransaction_id = \"YOUR_TRANSACTION_ID\"", - "outputs": [] + "id": "d0ac086d-1dba-4bbf-a692-f7bf0832f24e", + "metadata": {}, + "outputs": [], + "source": [ + "file_path = f\"{datetime.now().isoformat()}_currents_transactions.xlsx\"" + ] }, { "cell_type": "markdown", @@ -151,30 +153,52 @@ "tags": [] }, "source": [ - "### Export current transaction" + "### Get current transaction" ] }, { - "cell_type": "markdown", - "id": "3f55d120-d3cc-441e-b928-69081078eb87", + "cell_type": "code", + "execution_count": null, + "id": "06a55817-cb91-4f9f-ae7e-25595be18237", "metadata": { "papermill": {}, "tags": [] }, + "outputs": [], "source": [ - "This function will export the current transaction from Naas Credits." + "data = naascredits.connect().transactions.get_currents()" + ] + }, + { + "cell_type": "markdown", + "id": "30a1a7a6-5121-4c76-859e-00e0ea6cb364", + "metadata": { + "execution": { + "iopub.execute_input": "2023-12-21T08:53:33.705308Z", + "iopub.status.busy": "2023-12-21T08:53:33.705055Z", + "iopub.status.idle": "2023-12-21T08:53:33.708305Z", + "shell.execute_reply": "2023-12-21T08:53:33.707644Z", + "shell.execute_reply.started": "2023-12-21T08:53:33.705284Z" + }, + "tags": [] + }, + "source": [ + "### Convert data to DataFrame" ] }, { "cell_type": "code", "execution_count": null, - "id": "06a55817-cb91-4f9f-ae7e-25595be18237", + "id": "eb2a040e-dd00-4afa-933b-9ea2497d83dc", "metadata": { "papermill": {}, "tags": [] }, - "source": "def export_transaction(api_key, transaction_id):\n url = \"https://api.naascredits.com/v1/transactions/\" + transaction_id\n headers = {\"Authorization\": \"Bearer \" + api_key}\n response = requests.get(url, headers=headers)\n if response.status_code == 200:\n return json.loads(response.content.decode(\"utf-8\"))\n else:\n return None", - "outputs": [] + "outputs": [], + "source": [ + "df = pd.DataFrame(data)\n", + "print(\"Current balance:\", df.CREDIT.sum())" + ] }, { "cell_type": "markdown", @@ -195,19 +219,25 @@ "tags": [] }, "source": [ - "### Display result" + "### Save data to Excel file" ] }, { "cell_type": "code", "execution_count": null, - "id": "eb2a040e-dd00-4afa-933b-9ea2497d83dc", + "id": "bc039287-6129-4f0b-8966-80bb8985b989", "metadata": { - "papermill": {}, "tags": [] }, - "source": "transaction = export_transaction(api_key, transaction_id)\nif transaction is not None:\n print(json.dumps(transaction, indent=4))\nelse:\n print(\"Error while exporting transaction\")", - "outputs": [] + "outputs": [], + "source": [ + "df.to_excel(\n", + " file_path,\n", + " sheet_name=\"Export\",\n", + " index=False,\n", + ")\n", + "print(f\"Excel file successfully saved: {file_path}\")" + ] }, { "cell_type": "markdown", @@ -249,4 +279,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +} diff --git a/Naas/Naas_Credits_Get_Balance.ipynb b/Naas Credits/Naas_Credits_Get_Balance.ipynb similarity index 95% rename from Naas/Naas_Credits_Get_Balance.ipynb rename to Naas Credits/Naas_Credits_Get_Balance.ipynb index cebf8386e9..81fa0bd531 100644 --- a/Naas/Naas_Credits_Get_Balance.ipynb +++ b/Naas Credits/Naas_Credits_Get_Balance.ipynb @@ -26,7 +26,7 @@ "tags": [] }, "source": [ - "# Naas - Credits Get Balance\n", + "# Naas Credits - Get Balance\n", "

Give Feedback | Bug report" ] }, @@ -60,7 +60,7 @@ "tags": [] }, "source": [ - "**Last update:** 2023-04-12 (Created: 2021-11-17)" + "**Last update:** 2023-12-21 (Created: 2021-11-17)" ] }, { @@ -130,7 +130,7 @@ "tags": [] }, "source": [ - "### Function" + "### Get balance" ] }, { @@ -143,7 +143,8 @@ }, "outputs": [], "source": [ - "balance = naascredits.connect().get_balance()" + "balance = naascredits.connect().get_balance()\n", + "print(balance)" ] }, { @@ -205,7 +206,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.8" + "version": "3.9.6" }, "naas": { "notebook_id": "b8e8498be8e2a6e4ed47256dd6b987fc934f372db1ce9b0ba198bd19514296ca", @@ -227,4 +228,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +}