modal.Mount
class Mount(modal.object.Provider)
Create a mount for a local directory or file that can be attached to one or more Modal functions.
Usage
import modal
import os
stub = modal.Stub()
@stub.function(mounts=[modal.Mount.from_local_dir("~/foo", remote_path="/root/foo")])
def f():
# `/root/foo` has the contents of `~/foo`.
print(os.listdir("/root/foo/"))
Modal syncs the contents of the local directory every time the app runs, but uses the hash of the file’s contents to skip uploading files that have been uploaded before.
def __init__(self, *args, **kwargs):
The Mount constructor is deprecated. Use static factory method Mount.from_local_dir or Mount.from_local_file
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"):
...
entries
@property
def entries(self):
add_local_dir
@typechecked
def add_local_dir(
self,
local_path: Union[str, Path],
*,
remote_path: Union[str, PurePosixPath, None] = None, # Where the directory is placed within in the mount
condition: Optional[
Callable[[str], bool]
] = None, # Filter function for file selection, default to include all files
recursive: bool = True, # add files from subdirectories as well
) -> "_Mount":
from_local_dir
@staticmethod
@typechecked
def from_local_dir(
local_path: Union[str, Path],
*,
remote_path: Union[str, PurePosixPath, None] = None, # Where the directory is placed within in the mount
condition: Optional[Callable[[str], bool]] = None, # Filter function for file selection - default all files
recursive: bool = True, # add files from subdirectories as well
):
add_local_file
@typechecked
def add_local_file(
self, local_path: Union[str, Path], remote_path: Union[str, PurePosixPath, None] = None
) -> "_Mount":
from_local_file
@staticmethod
@typechecked
def from_local_file(local_path: Union[str, Path], remote_path: Union[str, PurePosixPath, None] = None) -> "_Mount":