From 92f86c1bc81b8fead0f3aa4f349e746cdc9763bb Mon Sep 17 00:00:00 2001 From: Chang CL Date: Thu, 21 Aug 2025 21:20:00 +0800 Subject: [PATCH] using .env --- .gitignore | 2 ++ os_scripts.ipynb | 66 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5f1fd1e..a863374 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ node_modules/ # Created by https://www.gitignore.io/api/python # Edit at https://www.gitignore.io/?templates=python +.env + ### Python ### # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/os_scripts.ipynb b/os_scripts.ipynb index bcfba8a..3e08d6d 100644 --- a/os_scripts.ipynb +++ b/os_scripts.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "id": "ab869d03-ed90-4c48-affe-02eb74938022", "metadata": {}, "outputs": [ @@ -18,6 +18,70 @@ "import os\n", "print(os.getcwd())" ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "10f377d9-24ec-4d64-8dd5-2cfbe14a0245", + "metadata": {}, + "outputs": [], + "source": [ + "# /home/changcl/apps/jupyter\n", + "# uv add python-dotenv\n", + "from dotenv import load_dotenv" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "149cbe5b-8365-47c1-a88b-9726865ef16a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "load_dotenv()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "64fe12a2-a517-4336-adfc-4965531b19d9", + "metadata": {}, + "outputs": [], + "source": [ + "database_url = os.getenv(\"DATABASE_URL\")\n", + "api_key = os.getenv(\"API_KEY\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9fb9cbb7-e13e-44f9-84c7-9124a1274ede", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Database URL: postgresql://user:password@host:port/database\n", + "API Key: your_secret_api_key\n" + ] + } + ], + "source": [ + "print(f\"Database URL: {database_url}\")\n", + "print(f\"API Key: {api_key}\")" + ] } ], "metadata": {