Create secretsload.py
Browse files- secretsload.py +25 -0
secretsload.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import toml
|
3 |
+
from typing import Dict
|
4 |
+
|
5 |
+
def load_stsecrets(config_path: str = '.streamlit/secrets.toml') -> Dict:
|
6 |
+
"""
|
7 |
+
Load configuration from Streamlit secrets TOML file and replace placeholders with environment variables.
|
8 |
+
|
9 |
+
:param config_path: Path to the Streamlit secrets TOML file
|
10 |
+
:return: Dictionary containing the credentials
|
11 |
+
"""
|
12 |
+
# Read the TOML file
|
13 |
+
with open(config_path, 'r') as f:
|
14 |
+
config = toml.load(f)
|
15 |
+
|
16 |
+
# Replace placeholders with environment variables
|
17 |
+
credentials = config.get('credentials', {})
|
18 |
+
for key, value in credentials.items():
|
19 |
+
if isinstance(value, str) and value.startswith('${') and value.endswith('}'):
|
20 |
+
env_var = value[2:-1] # Remove ${ and }
|
21 |
+
credentials[key] = os.environ.get(env_var)
|
22 |
+
if credentials[key] is None:
|
23 |
+
raise ValueError(f"Environment variable {env_var} is not set")
|
24 |
+
|
25 |
+
return credentials
|