EnvConfig API#
Bases: AnyWidget
Environment variable configuration widget for secure API key management.
Displays a UI for configuring environment variables, with password-style masking and optional validation callbacks. Useful for notebooks that need API keys configured before proceeding.
Values are stored internally and never written back to os.environ.
Examples:
# Simple usage - just check vars exist
config = EnvConfig(["OPENAI_API_KEY", "ANTHROPIC_API_KEY"])
config
# With validators
config = EnvConfig({
"OPENAI_API_KEY": lambda k: openai.Client(api_key=k).models.list(),
"ANTHROPIC_API_KEY": None, # Just check existence
})
# Block until valid
config.require_valid()
# Access values
config["OPENAI_API_KEY"]
Create an EnvConfig widget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variables
|
Union[Sequence[str], dict[str, Optional[ValidatorFn]]]
|
Either a sequence of variable names, or a dict mapping names to optional validator functions. Validators should raise an exception on failure. |
required |
**kwargs
|
Any
|
Forwarded to |
{}
|
Source code in wigglystuff/env_config.py
__contains__ #
__getitem__ #
Get variable value by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The variable name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The value (from os.environ at init time, or entered via widget). |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the variable is not configured or not set. |
Source code in wigglystuff/env_config.py
require_valid #
Assert all environment variables are valid.
Raises:
| Type | Description |
|---|---|
EnvironmentError
|
If any variable is missing or invalid. |
Source code in wigglystuff/env_config.py
Synced traitlets#
| Traitlet | Type | Notes |
|---|---|---|
variables |
list |
List of variable info dicts with name, status, error, has_validator, value. |
all_valid |
bool |
True when all variables are valid. |
Helper methods#
| Method | Description |
|---|---|
require_valid() |
Raises EnvironmentError if any variable is missing/invalid. |
config["KEY"] |
Dictionary-style access to stored values. |
"KEY" in config |
Check if a variable is configured and has a value. |