Skip to content

Commit

Permalink
Add Python code to Connect preview HTML (#806)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
juliasilge committed Nov 16, 2023
1 parent ed3ac3b commit a3e52c7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pins (development version)

* Added example Python code to pin previews for Posit Connect (#806).

# pins 1.3.0

## Breaking changes
Expand Down
1 change: 1 addition & 0 deletions R/board_connect_bundle.R
Original file line number Diff line number Diff line change
Expand Up @@ -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("<a href=\"", metadata$urls, "\">", metadata$urls, "</a>", 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),
Expand Down
25 changes: 18 additions & 7 deletions inst/preview/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<h3>{{{pin_name}}}</h3>
{{#pin_metadata}}
<p>
{{#date}}<b>Last updated:</b> {{{.}}} &bull;{{/date}}
{{#date}}<b>Last updated from R:</b> {{{.}}} &bull;{{/date}}
<b>Format:</b> {{{format}}} &bull;
<b>API:</b> v{{{api_version}}}
</p>
Expand All @@ -52,14 +52,25 @@ <h3>Learn more about this pin at:</h3>
</section>

<section>
<h3>R Code</h3>
<pre id="pin-r" class="pin-code"><code class="r">library(pins)
<h3>R Code</h3>
<pre id="pin-r" class="pin-code"><code class="r">library(pins)
board <- {{{board_deparse}}}
pin_read(board, "{{pin_name}}")</code></pre>
<script type="text/javascript">
hljs.registerLanguage("r", highlight_r);
hljs.initHighlightingOnLoad();
</script>
<script type="text/javascript">
hljs.registerLanguage("r", highlight_r);
hljs.registerLanguage("python", highlight_python);
hljs.initHighlightingOnLoad();
</script>
</section>

<section style="{{show_python_style}}">
<h3>Python Code</h3>
<pre id="pin-python" class="pin-code"><code class="python">import pins
from dotenv import load_dotenv
load_dotenv()

board = pins.board_connect()
board.pin_read(board, "{{pin_name}}")</code></pre>
</section>

<section style="{{data_preview_style}}">
Expand Down
25 changes: 18 additions & 7 deletions tests/testthat/_snaps/board_connect_bundle.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<section>
<h3>TEST/test</h3>
<p>
<b>Last updated:</b> 2021-11-11 11:39:56 &bull;
<b>Last updated from R:</b> 2021-11-11 11:39:56 &bull;
<b>Format:</b> rds &bull;
<b>API:</b> v1.0
</p>
Expand Down Expand Up @@ -65,14 +65,25 @@
</section>

<section>
<h3>R Code</h3>
<pre id="pin-r" class="pin-code"><code class="r">library(pins)
<h3>R Code</h3>
<pre id="pin-r" class="pin-code"><code class="r">library(pins)
board <- board_connect(auth = "envvar")
pin_read(board, "TEST/test")</code></pre>
<script type="text/javascript">
hljs.registerLanguage("r", highlight_r);
hljs.initHighlightingOnLoad();
</script>
<script type="text/javascript">
hljs.registerLanguage("r", highlight_r);
hljs.registerLanguage("python", highlight_python);
hljs.initHighlightingOnLoad();
</script>
</section>

<section style="display:none">
<h3>Python Code</h3>
<pre id="pin-python" class="pin-code"><code class="python">import pins
from dotenv import load_dotenv
load_dotenv()

board = pins.board_connect()
board.pin_read(board, "TEST/test")</code></pre>
</section>

<section style="">
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-board_connect_bundle.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit a3e52c7

Please sign in to comment.