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

Adding helpers that setup databricks notebook #43

Merged
merged 2 commits into from
May 3, 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 @@ -33,6 +33,7 @@ Imports:
Suggests:
testthat (>= 3.0.0),
htmltools,
htmlwidgets,
knitr,
magick,
rmarkdown,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export(get_and_start_cluster)
export(get_and_start_warehouse)
export(get_latest_dbr)
export(git_source)
export(in_databricks_nb)
export(init_script_info)
export(is.access_control_req_group)
export(is.access_control_req_user)
Expand Down Expand Up @@ -168,7 +169,9 @@ export(lib_pypi)
export(lib_whl)
export(libraries)
export(new_cluster)
export(notebook_enable_htmlwidgets)
export(notebook_task)
export(notebook_use_posit_repo)
export(open_workspace)
export(pipeline_task)
export(python_wheel_task)
Expand Down
1 change: 1 addition & 0 deletions R/brickster.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
utils::globalVariables(c("displayHTML"))
91 changes: 91 additions & 0 deletions R/notebook-helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#' Detect if running within Databricks Notebook
#'
#' @details
#' R sessions on Databricks can be detected via various environment variables
#' and directories.
#'
#' @return Boolean
#' @export
in_databricks_nb <- function() {
("/databricks/spark/R/lib" %in% .libPaths()) &&
exists("DATABRICKS_GUID", envir = .GlobalEnv)
}

#' Setup Databricks Notebook with Posit Package Manager
#'
#' @details
#' Databricks notebooks default repo for package installation is CRAN.
#' CRAN doesn't provide pre-compiled binaries for linux and this results in
#' packages taking longer than desired.
#'
#' This function can be called within a Databricks notebook to easily switch to
#' Posit and retrieve pre-compiled binaries.
#'
#' This function will behave correctly across different Databricks Runtimes,
#' even when the underlying linux version changes.
#'
#' @export
notebook_use_posit_repo <- function() {
if (in_databricks_nb()) {
agent <- sprintf("R/%s R (%s)", getRversion(), paste(getRversion(), R.version["platform"], R.version["arch"], R.version["os"]))
codename <- system("lsb_release -c --short", intern = T)
mirror <- paste0("https://packagemanager.posit.co/cran/__linux__/", codename, "/latest")
options(
HTTPUserAgent = agent,
repos = c(POSIT = mirror, getOption("repos"))
)
}
}

#' Enable htmlwidgets in Databricks Notebook
#'
#' @details
#' Databricks notebooks by default don't currently support htmlwidgets.
#' This behaviour can be corrected by:
#' - adjusting the print method in htmltools
#' - installing pandoc
#'
#' This is a invasive method to correct the behaviour as htmltools isn't
#' flexible to adjust via the `viewer` option directly.
#'
#' It only runs within a Databricks notebook cell.
#'
#' The height can be adjusted without running the function again by using the
#' `db_htmlwidget_height` option (e.g. `options(db_htmlwidget_height = 300)`).
#'
#'
#' @param height Measurement passed to height of htmlwidget. This overrides
#' existing values that may often be `NULL` to ensure the height is correctly
#' displayed within the iframe of notebook results cells (via `displayHTML()`).
#' Default is 450.
#'
#' @export
#'
#' @examples
#' notebook_enable_htmlwidgets()
#' # set default height to 800px
#' notebook_enable_htmlwidgets(height = 800)
notebook_enable_htmlwidgets <- function(height = 450) {
if (in_databricks_nb()) {

# new option to control default widget height, default is 450px
options(db_htmlwidget_height = height)

system("apt-get --yes install pandoc", intern = T)
if (!base::require("htmlwidgets")) {
utils::install.packages("htmlwidgets")
}

# new method will fetch height based on new option, or default to 450px
new_method <- function(x, ...) {
x$height <- getOption("db_htmlwidget_height", 450)
file <- tempfile(fileext = ".html")
htmlwidgets::saveWidget(x, file = file)
contents <- as.character(rvest::read_html(file))
displayHTML(contents)
}

utils::assignInNamespace("print.htmlwidget", new_method, ns = "htmlwidgets")
invisible(list(default_height = height, print = new_method))
}
}
5 changes: 5 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ reference:
contents:
- starts_with("db_libs", internal = TRUE)
- wait_for_lib_installs
- title: Databricks Notebook Helpers
contents:
- in_databricks_nb
- notebook_use_posit_repo
- notebook_enable_htmlwidgets
- title: DBFS
contents: starts_with("db_dbfs", internal = TRUE)
- title: Volume FileSystem
Expand Down
18 changes: 18 additions & 0 deletions man/in_databricks_nb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions man/notebook_enable_htmlwidgets.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/notebook_use_posit_repo.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/testthat/test-connection-pane.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,15 @@ test_that("Connection Pane Helpers", {
host = db_host(),
token = db_token()
)
# remove serverless clusters from test as they cause an issue
clusters <- clusters[!grepl("v2n", clusters$name), ]
cluster_id <- get_id_from_panel_name(clusters$name[[1]])
cluster_data <- get_cluster(
id = cluster_id,
host = db_host(),
token = db_token()
)

})
expect_type(clusters, "list")
expect_type(cluster_data, "list")
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-notebook-helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test_that("Databricks Notebook Helpers", {

# currently running tests outside of a databricks notebook
expect_false(in_databricks_nb())
expect_no_error(notebook_use_posit_repo())
expect_no_error(notebook_enable_htmlwidgets())

})
Loading