From a3e52c78e799d24359b72dffd4cac33a7bc0fe98 Mon Sep 17 00:00:00 2001 From: Julia Silge Date: Thu, 16 Nov 2023 10:12:48 -0700 Subject: [PATCH] Add Python code to Connect preview HTML (#806) * Add Python code to Connect preview HTML * Fix when to show Python code * Fix bundle test * Simpler logic * Python code needs to load env vars * Update snapshot * Update NEWS --- NEWS.md | 2 ++ R/board_connect_bundle.R | 1 + inst/preview/index.html | 25 +++++++++++++------ tests/testthat/_snaps/board_connect_bundle.md | 25 +++++++++++++------ tests/testthat/test-board_connect_bundle.R | 4 +-- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/NEWS.md b/NEWS.md index 032845c3..c1f2e10c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # pins (development version) +* Added example Python code to pin previews for Posit Connect (#806). + # pins 1.3.0 ## Breaking changes diff --git a/R/board_connect_bundle.R b/R/board_connect_bundle.R index 9d72b059..455c9e5e 100644 --- a/R/board_connect_bundle.R +++ b/R/board_connect_bundle.R @@ -62,6 +62,7 @@ rsc_bundle_preview_index <- function(board, name, x, metadata) { data_preview_style = if (is.data.frame(x)) "" else "display:none", urls = paste0("", metadata$urls, "", collapse = ", "), url_preview_style = if (!is.null(metadata$urls)) "" else "display:none", + show_python_style = if (metadata$type %in% c("rds", "qs")) "display:none" else "", pin_name = paste0(owner, "/", name$name), pin_metadata = list( as_yaml = yaml::as.yaml(metadata), diff --git a/inst/preview/index.html b/inst/preview/index.html index 1486461b..a1754c2a 100644 --- a/inst/preview/index.html +++ b/inst/preview/index.html @@ -33,7 +33,7 @@

{{{pin_name}}}

{{#pin_metadata}}

- {{#date}}Last updated: {{{.}}} •{{/date}} + {{#date}}Last updated from R: {{{.}}} •{{/date}} Format: {{{format}}} • API: v{{{api_version}}}

@@ -52,14 +52,25 @@

Learn more about this pin at:

-

R Code

-
library(pins)
+      

R Code

+
library(pins)
 board <- {{{board_deparse}}}
 pin_read(board, "{{pin_name}}")
- + +
+ +
+

Python Code

+
import pins
+from dotenv import load_dotenv
+load_dotenv()
+
+board = pins.board_connect()
+board.pin_read(board, "{{pin_name}}")
diff --git a/tests/testthat/_snaps/board_connect_bundle.md b/tests/testthat/_snaps/board_connect_bundle.md index 356b78e2..f96c4e7e 100644 --- a/tests/testthat/_snaps/board_connect_bundle.md +++ b/tests/testthat/_snaps/board_connect_bundle.md @@ -34,7 +34,7 @@

TEST/test

- Last updated: 2021-11-11 11:39:56 • + Last updated from R: 2021-11-11 11:39:56 • Format: rds • API: v1.0

@@ -65,14 +65,25 @@
-

R Code

-
library(pins)
+          

R Code

+
library(pins)
     board <- board_connect(auth = "envvar")
     pin_read(board, "TEST/test")
- + +
+ +
+

Python Code

+
import pins
+    from dotenv import load_dotenv
+    load_dotenv()
+    
+    board = pins.board_connect()
+    board.pin_read(board, "TEST/test")
diff --git a/tests/testthat/test-board_connect_bundle.R b/tests/testthat/test-board_connect_bundle.R index f3b2583b..17bd6b5b 100644 --- a/tests/testthat/test-board_connect_bundle.R +++ b/tests/testthat/test-board_connect_bundle.R @@ -5,10 +5,10 @@ test_that("bundle contains expected files", { saveRDS(df, fs::path(path, "test.rds")) board <- list(account = "TEST", server_name = "example.com") - metadata <- list(file = "test.rds") + metadata <- list(file = "test.rds", type = "rds") class(board) <- c("pins_board_connect", "pins_board") - out <- rsc_bundle(board, "test", fs::path(path, "test.rds"), list(), df) + out <- rsc_bundle(board, "test", fs::path(path, "test.rds"), metadata, df) files <- fs::dir_ls(out) files <- files[!fs::is_dir(files)]