Secrets
Guidelines for managing secrets in DataCards.
Step 1: Create a Cell to Generate the .env File
You can create a .env file in any cell of your project to store your secrets:
import os
from pathlib import Path
# Define your environment variables
env_variables = {
'API_KEY': 'your_secret_api_key_here',
'MY_DATABASE_URL': 'postgresql://user:password@localhost:5432/mydb',
'SECRET_TOKEN': 'your_secret_token_123',
'DEBUG_MODE': 'True'
}
env_content = '\n'.join([f'{key}={value}' for key, value in env_variables.items()])
with open('.env', 'w') as f:
f.write(env_content)
print(f"✅ .env file created successfully")
To prevent users from viewing your secrets, simply delete the cell after execution.
Step 2: Install Required Package
To load environment variables, you may need the python-dotenv
package. Install it first:
Execute this command in a separate cell or dedicated package management notebook:
!uv pip install python-dotenv
Step 3: Access Environment Variables in Any Notebook
Load the .env file and utilize the environment variables with:
from dotenv import load_dotenv
import os
load_dotenv('.env')
Then access them using os.getenv()
:
# Now you can access the environment variables
api_key = os.getenv('API_KEY')
database_url = os.getenv('MY_DATABASE_URL')
secret_token = os.getenv('SECRET_TOKEN')
debug_mode = os.getenv('DEBUG_MODE')
print(f"API Key: {api_key[:10]}...")
print(f"My Database URL: {database_url}")
print(f"Secret Token: {secret_token[:10]}...")
print(f"Debug Mode: {debug_mode}")
A dedicated user interface for controlling environment variables is planned for future releases
Last updated on