modal.secret
modal.secret.Secret
class Secret(modal.object.Provider)
Secrets provide a dictionary of environment variables for images.
Secrets are a secure way to add credentials and other sensitive information to the containers your functions run in. You can create and edit secrets on the dashboard, or programmatically from Python code.
See the secrets guide page for more information.
def __init__(self, env_dict: Dict[str, str]):
Secret({...})
is deprecated. Please use Secret.from_dict({...})
instead.
persist
@typechecked
def persist(self, label: str, namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE):
Deploy a Modal app containing this object. This object can then be imported from other apps using
the returned reference, or by calling modal.SharedVolume.from_name(label)
(or the equivalent method
on respective class).
Example Usage
import modal
volume = modal.SharedVolume().persist("my-volume")
stub = modal.Stub()
# Volume refers to the same object, even across instances of `stub`.
@stub.function(shared_volumes={"/vol": volume})
def f():
pass
from_name
@classmethod
def from_name(
cls: Type[P],
app_name: str,
tag: Optional[str] = None,
namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
) -> P:
Returns a reference to an Modal object of any type
Useful for referring to already created/deployed objects, e.g., Secrets
import modal
stub = modal.Stub()
@stub.function(secret=modal.Secret.from_name("my-secret-name"))
def some_function():
pass
lookup
@classmethod
def lookup(
cls: Type[P],
app_name: str,
tag: Optional[str] = None,
namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
client: Optional[_Client] = None,
) -> H:
General purpose method to retrieve Modal objects such as functions, shared volumes, and secrets.
import modal
square = modal.Function.lookup("my-shared-app", "square")
assert square(3) == 9
vol = modal.SharedVolume.lookup("my-shared-volume")
for chunk in vol.read_file("my_db_dump.csv"):
...
from_dict
@typechecked
@staticmethod
def from_dict(
env_dict: Dict[
str, str
] = {}, # dict of entries to be inserted as environment variables in functions using the secret
template_type="", # internal use only
):
Create a secret from a str-str dictionary.
Usage:
@stub.function(secret=modal.Secret.from_dict({"FOO": "bar"})
def run():
print(os.environ["FOO"])
from_dotenv
@staticmethod
def from_dotenv(path=None):
Create secrets from a .env file automatically.
If no argument is provided, it will use the current working directory as the starting
point for finding a .env
file. Note that it does not use the location of the module
calling Secret.from_dotenv
.
If called with an argument, it will use that as a starting point for finding .env
files.
In particular, you can call it like this:
@stub.function(secret=modal.Secret.from_dotenv(__file__))
def run():
print(os.environ["USERNAME"]) # Assumes USERNAME is defined in your .env file
This will use the location of the script calling modal.Secret.from_dotenv
as a
starting point for finding the .env
file.