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

🚸 Improve UX of db$track() and db$finish() #120

Merged
merged 10 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Imports:
httr,
jsonlite,
purrr,
R.utils,
R6,
rlang,
tibble
Expand Down
27 changes: 23 additions & 4 deletions R/Instance.R
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,20 @@ Instance <- R6::R6Class( # nolint object_name_linter
py_lamin <- self$get_py_lamin(check = TRUE, what = "Tracking")

if (is.null(path)) {
cli::cli_abort("The {.arg path} argument must be provided")
path <- detect_path()
if (is.null(path)) {
cli::cli_abort(
"Failed to detect the path to track. Please set the {.arg path} argument."
)
}
}

if (is.null(transform)) {
transform <- tryCatch(
py_lamin$track(path = path),
error = function(err) {
py_err <- reticulate::py_last_error()
# please don't change the below without changing it in lamindb
if (py_err$type != "MissingContextUID") {
cli::cli_abort(c(
"Python {py_err$message}",
Expand All @@ -242,8 +248,7 @@ Instance <- R6::R6Class( # nolint object_name_linter

uid <- gsub(".*\\(\"(.*?)\"\\).*", "\\1", py_err$value)
cli::cli_inform(paste(
"Got UID {.val {uid}} for path {.file {path}}.",
"Run this function with {.code transform = \"{uid}\"} to track this path."
"To track this notebook, run: db$track(\"{uid}\")"
))
}
)
Expand All @@ -260,7 +265,21 @@ Instance <- R6::R6Class( # nolint object_name_linter
#' @description Finish a tracked run
finish = function() {
py_lamin <- self$get_py_lamin(check = TRUE, what = "Tracking")
py_lamin$finish()
tryCatch(
py_lamin$finish(),
error = function(err) {
py_err <- reticulate::py_last_error()
if (py_err$type != "NotebookNotSaved") {
cli::cli_abort(c(
"Python {py_err$message}",
"i" = "Run {.run reticulate::py_last_error()} for details"
))
}
# please don't change the below without changing it in lamindb
message <- gsub(".*NotebookNotSaved: (.*)$", "\\1", py_err$value)
cli::cli_inform(paste("NotebookNotSaved: {message}"))
}
)
},
#' @description
#' Print an `Instance`
Expand Down
1 change: 0 additions & 1 deletion R/InstanceAPI.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter
registry_name,
id_or_uid,
verbose = FALSE) {

url <- paste0(
private$.instance_settings$api_url,
"/instances/",
Expand Down
49 changes: 49 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,52 @@ is_knitr_notebook <- function() {
# check if we are in a notebook
!is.null(knitr::opts_knit$get("out.format"))
}

#' Detect path
#'
#' Find the path of the file where code is currently been run
#'
#' @return If found, path to the file relative to the working directory,
#' otherwise `NULL`
#' @noRd
detect_path <- function() {
# Based on various responses from https://stackoverflow.com/questions/47044068/get-the-path-of-current-script

current_path <- NULL

# Get path if in a running RMarkdown notebook
if (is_knitr_notebook()) {
current_path <- knitr::current_input()
}

# Get path if in a script run by `source("script.R")`
source_trace <- R.utils::findSourceTraceback()
if (is.null(current_path) && length(source_trace) > 0) {
current_path <- names(source_trace)[1]
}

# Get path if in a script run by `Rscript script.R`
if (is.null(current_path)) {
cmd_args <- R.utils::commandArgs(asValues = TRUE)
current_path <- cmd_args[["file"]]
}

# Get path if in a document in RStudio
if (
is.null(current_path) &&
requireNamespace("rstudioapi", quietly = TRUE) &&
rstudioapi::isAvailable()
) {
doc_context <- rstudioapi::getActiveDocumentContext()
if (doc_context$id != "#console") {
current_path <- doc_context$path
}
}

# Normalise the path relative to the working directory
if (!is.null(current_path)) {
current_path <- R.utils::getRelativePath(current_path)
}

return(current_path)
}
4 changes: 2 additions & 2 deletions vignettes/laminr.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ LaminDB tracks which code is used to create data.
To track the current source code, run:

```{r track, eval = submit_eval}
db$track("I8BlHXFXqZOG0000", path = "laminr.Rmd")
db$track("I8BlHXFXqZOG0000")
```

<div class="alert alert-info" role="alert">
**Tip**

The UID (here `"I8BlHXFXqZOG0000"`) is obtained by running `db$track(path = "your_file.R")` and copying the UID from the output.
The UID (here `"I8BlHXFXqZOG0000"`) is obtained by running `db$track()` and copying the UID from the output.
</div>

# Connect to other instances
Expand Down
Loading