Load & close an instance#

Hide code cell content
!lamin close
βœ… Closed testuser1/mydata
import lamindb as ln
πŸ”Ά You haven't yet setup an instance using the CLI: Please call `ln.setup.init()` or `ln.setup.load()`

Load your own instance by name#

If the user is the instance owner, just load the instance by name:

ln.setup.load("mydata")  # CLI: lamin load mydata
πŸ’¬ Found cached instance metadata: /home/runner/.lamin/testuser1-instance-mydata.env
βœ… Loaded instance: testuser1/mydata
'migrate-unnecessary'
Hide code cell content
from pathlib import Path

assert ln.setup.settings.instance.storage.is_cloud == False
assert ln.setup.settings.instance.name == "mydata"
assert (
    ln.setup.settings.instance.storage.root.as_posix()
    == Path("./mydata").resolve().as_posix()
)
assert ln.setup.settings.instance.storage.cache_dir is None
assert (
    ln.setup.settings.instance.db
    == f"sqlite:///{Path('./mydata').resolve().as_posix()}/mydata.lndb"
)
Hide code cell content
import pytest

# assume we move the storage location
!mv mydata mydata_new_loc
with pytest.raises(
    RuntimeError
):  # triggers because it does not find the SQLite file anymore
    ln.setup.load("mydata")
# now account for the new storage location
ln.setup.load("mydata", storage="./mydata_new_loc")
assert (
    ln.setup.settings.instance.storage.root.as_posix()
    == Path("./mydata_new_loc").resolve().as_posix()
)
assert ln.setup.settings.instance.storage.cache_dir is None
assert (
    ln.setup.settings.instance.db
    == f"sqlite:///{Path('./mydata_new_loc').resolve().as_posix()}/mydata.lndb"
)
πŸ’¬ Found cached instance metadata: /home/runner/.lamin/testuser1-instance-mydata.env
πŸ”Ά SQLite file /home/runner/work/lndb/lndb/docs/guide/mydata/mydata.lndb does not exist
πŸ’¬ Found cached instance metadata: /home/runner/.lamin/testuser1-instance-mydata.env
βœ… Updated storage root j9nEHTlN to /home/runner/work/lndb/lndb/docs/guide/mydata_new_loc
βœ… Loaded instance: testuser1/mydata

This also works for remote instances:

Hide code cell content
# ensure that the locally cached env file is deleted
from lndb.dev._settings_store import instance_settings_file

instance_settings_file("lndb-setup-ci", "testuser1").unlink()
ln.setup.load("lndb-setup-ci")  # lamin load lndb-setup-ci
2023-05-30 15:19:56,631:INFO - Found credentials in environment variables.
βœ… Loaded instance: testuser1/lndb-setup-ci
'migrate-unnecessary'
Hide code cell content
assert ln.setup.settings.instance.storage.is_cloud == True
assert ln.setup.settings.instance.storage.root_as_str == "s3://lndb-setup-ci"
assert (
    ln.setup.settings.instance._sqlite_file.as_posix()
    == "s3://lndb-setup-ci/lndb-setup-ci.lndb"
)

Load an instance from another owner#

If you have the permission, you can load an instance from another owner.

ln.setup.load("testuser1/mydata")  # lamin load "testuser1/mydata"
πŸ’¬ Found cached instance metadata: /home/runner/.lamin/testuser1-instance-mydata.env
βœ… Loaded instance: testuser1/mydata
'migrate-unnecessary'

Load an instance from wrong owner.

assert ln.setup.load("testuser2/mydata") == "instance-not-reachable"
❌ Instance testuser2/mydata neither loadable from hub nor local cache. Check whether instance exists and you have access: https://lamin.ai/testuser2/mydata?tab=collaborators

Close an instance#

ln.setup.close()
βœ… Closed testuser1/mydata