Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Support auto-knitted html reports during ln.finish() #2213

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
31 changes: 25 additions & 6 deletions lamindb/_finish.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from lamin_utils import logger
from lamindb_setup.core.hashing import hash_file

from lamindb.core.exceptions import NotebookNotSaved

if TYPE_CHECKING:
from pathlib import Path

Expand All @@ -16,6 +18,10 @@
from ._query_set import QuerySet


def get_seconds_since_modified(filepath) -> float:
return datetime.now().timestamp() - filepath.stat().st_mtime


# this is from the get_title function in nbproject
# should be moved into lamindb sooner or later
def prepare_notebook(
Expand Down Expand Up @@ -104,7 +110,9 @@
# for scripts, things are easy
is_consecutive = True
is_ipynb = filepath.suffix == ".ipynb"
is_r_notebook = filepath.suffix in {".qmd", ".Rmd"}
source_code_path = filepath
report_path: Path | None = None
# for notebooks, we need more work
if is_ipynb:
try:
Expand Down Expand Up @@ -139,6 +147,13 @@
".ipynb", ".py"
)
notebook_to_script(transform, filepath, source_code_path)
elif is_r_notebook:
if not filepath.with_suffix(".nb.html").exists():
logger.warning(
f"no auto-knitted file {filepath.with_suffix('.nb.html')} found, save your manually rendered .html report via the CLI: lamin save {filepath}"
)
else:
report_path = filepath.with_suffix(".nb.html")

Check warning on line 156 in lamindb/_finish.py

View check run for this annotation

Codecov / codecov/patch

lamindb/_finish.py#L156

Added line #L156 was not covered by tests
ln.settings.creation.artifact_silence_missing_run_warning = True
# track source code
hash, _ = hash_file(source_code_path) # ignore hash_type for now
Expand Down Expand Up @@ -198,10 +213,16 @@
run.finished_at = datetime.now(timezone.utc)

# track report and set is_consecutive
if not is_ipynb:
run.is_consecutive = True
if report_path is None:
run.is_consecutive = is_consecutive
run.save()
else:
if not from_cli:
if get_seconds_since_modified(report_path) > 2 and not ln_setup._TESTING:
# this can happen when auto-knitting an html with RStudio
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a test setup for this is a bit more cumbersome, I will ship once I know this is meaningful.

raise NotebookNotSaved(

Check warning on line 223 in lamindb/_finish.py

View check run for this annotation

Codecov / codecov/patch

lamindb/_finish.py#L223

Added line #L223 was not covered by tests
"Please save the notebook in RStudio right before calling `db$finish()`"
)
if run.report_id is not None:
hash, _ = hash_file(report_path) # ignore hash_type for now
if hash != run.report.hash:
Expand Down Expand Up @@ -250,11 +271,9 @@
f"go to: https://lamin.ai/{identifier}/transform/{transform.uid}"
)
if not from_cli:
thing, name = (
("notebook", "notebook.ipynb") if is_ipynb else ("script", "script.py")
)
thing = "notebook" if (is_ipynb or is_r_notebook) else "script"
logger.important(
f"if you want to update your {thing} without re-running it, use `lamin save {name}`"
f"if you want to update your {thing} without re-running it, use `lamin save {filepath}`"
)
# because run & transform changed, update the global context
context._run = run
Expand Down
7 changes: 2 additions & 5 deletions lamindb/core/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def raise_missing_context(transform_type: str, key: str) -> bool:
f"you already have a transform with key '{key}': Transform('{transform.uid[:8]}')\n"
f' (1) to make a revision, run: ln.track("{new_uid}")\n (2) to create a new transform, rename your {transform_type} file and re-run: ln.track()'
)
if transform_type == "notebook":
if is_run_from_ipython:
print(f"→ {message}")
response = input("→ Ready to re-run? (y/n)")
if response == "y":
Expand Down Expand Up @@ -579,10 +579,7 @@ def finish(self, ignore_non_consecutive: None | bool = None) -> None:
`lamin save script.py` or `lamin save notebook.ipynb` → `docs </cli#lamin-save>`__

"""
from lamindb._finish import save_context_core

def get_seconds_since_modified(filepath) -> float:
return datetime.now().timestamp() - filepath.stat().st_mtime
from lamindb._finish import get_seconds_since_modified, save_context_core

def get_shortcut() -> str:
import platform
Expand Down