Using Stela¶
Stela usage is very simple. Just import the env
object:
If you want to get the environment variable programmatically, use the get
function:
If the variable did not exist, Stela will raise a StelaValueError
. You can change this using the raise_on_missing
parameter:
from stela import env
var = "THIS_VAR_DOES_NOT_EXIST"
value = env.get(var, raise_on_missing=False)
#> None
If you want to return a default value if the variable does not exist, use the get_or_default
function:
from stela import env
var = "THIS_VAR_DOES_NOT_EXIST"
value = env.get_or_default(var, default="default_value")
#> "default_value"
You can also list all environment variables:
And you can use the env
object to get information about the current environment:
When Stela read the data?¶
Stela is imported once at module level - project settings are load and immediately available:
from stela import env
from flask import Flask
app = Flask(__name__)
app.config.update(
SECRET_KEY=env.FLASK_SECRET
)
@app.route("/")
def hello():
return f"Hello, Environment is {env.current_environment}"
Refreshing Stela settings¶
If you need to reload settings, use the stela.utils.read_env
function:
Logging data¶
Stela use the loguru package for logging, using INFO
for general messages
and DEBUG
for key/values retrieved in toml, environment keys, decorators, etc...
You can use the logs to debug data during Stela operation.
By default, the log is disabled. You can modify this behavior globally with the following configurations:
Also, you can use decorators for fine-tuning logging per function:
from stela import env
from stela.decorators import stela_enable_logs, stela_disable_logs
@stela_enable_logs
def my_bugged_code():
return env.MY_API_URL
@stela_disable_logs
def my_sensible_code():
return env.MY_SECRET_KEY
The log level can be defined using the LOGURU_LOG_LEVEL
as per loguru documentation.
For the next step, we will look at how IDEs can autocomplete Stela environment variables.