Skip to content

Commit 61039ae

Browse files
authored
show serve url as result of quarto_preview (#259)
* show serve url in cli inform message * Add to doc * add example for using result * Add NEWS * Add test for ci * BUmp version
1 parent 85ee745 commit 61039ae

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# quarto (development version)
22

3+
- `quarto_preview()` now explicitly returns the preview server URL (invisibly) and documents this behavior. This enables programmatic workflows such as taking screenshots with **webshot2** or passing the URL to other automation tools (thanks, @cwickham, #233).
4+
35
- Added NA value detection in YAML processing to prevent silent failures when passing R's `NA` values to Quarto CLI. Functions `as_yaml()` and `write_yaml()` now validate for NA values and provide clear error messages with actionable suggestions. This addresses issues where R's `NA` values get converted to YAML strings (like `.na.real`) that Quarto doesn't recognize as missing values, because they are not supported in YAML 1.2 spec. This is to help users handle missing data appropriately before passing to Quarto (#168).
46

57
- Added `add_spin_preamble()` function to add YAML preambles to R scripts for use with Quarto Script rendering support. The function automatically detects existing preambles and provides flexible customization options through `title` and `preamble` parameters (#164).

R/daemon.R

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ run_serve_daemon <- function(
6262

6363
# port and host
6464
args <- c(args, "--port", port)
65-
if (!identical(host, "127.0.0.1")) {
65+
if (identical(host, "127.0.0.1")) {
66+
host <- "localhost" # use localhost for consistency
67+
}
68+
if (!identical(host, "localhost")) {
6669
args <- c(args, "--host", host)
6770
}
6871

@@ -148,12 +151,14 @@ run_serve_daemon <- function(
148151
}
149152
poll_process()
150153

154+
serve_url <- quarto[[url_key]] %||% sprintf("http://%s:%i", host, port)
155+
151156
# indicate server is running
152157
if (isFALSE(quiet)) {
153-
cli::cli
154158
cli::cli_inform(c(
155159
"",
156-
i = "Stop the preview with {.code quarto::quarto_{command}_stop()}"
160+
# "i" = "Preview server running at {.url {serve_url}}",
161+
">" = "Stop the preview with {.code quarto_{command}_stop()}"
157162
))
158163
}
159164

@@ -166,11 +171,10 @@ run_serve_daemon <- function(
166171
utils::browseURL
167172
)
168173
}
169-
serve_url <- quarto[[url_key]] %||% paste0("http://localhost:", port)
170174
browse(serve_url)
171175
}
172176

173-
invisible()
177+
invisible(serve_url)
174178
}
175179

176180
stop_serve_daemon <- function(command) {

R/preview.R

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#' @param quiet Suppress warning and other messages, from R and also Quarto CLI
2525
#' (i.e `--quiet` is passed as command line)
2626
#'
27+
#' @return The URL of the preview server (invisibly). This can be used to
28+
#' programmatically access the server location, for example to take screenshots
29+
#' with webshot2 or pass to other automation tools.
30+
#'
2731
#' @importFrom processx process
2832
#' @importFrom rstudioapi isAvailable
2933
#' @importFrom rstudioapi viewer
@@ -42,6 +46,16 @@
4246
#' # (rather than RStudio Viewer)
4347
#' quarto_preview("myproj", open = utils::browseURL)
4448
#'
49+
#' # Capture the preview URL for programmatic use
50+
#' preview_url <- quarto_preview("document.qmd", browse = FALSE)
51+
#' cat("Preview available at:", preview_url, "\n")
52+
#'
53+
#' # Take a screenshot of the preview using webshot2
54+
#' if (require(webshot2)) {
55+
#' preview_url <- quarto_preview("document.qmd", browse = FALSE)
56+
#' webshot2::webshot(preview_url, "preview.png")
57+
#' }
58+
#'
4559
#' # Stop any running quarto preview
4660
#' quarto_preview_stop()
4761
#' }
@@ -71,7 +85,7 @@ quarto_preview <- function(
7185
args <- c("--no-navigate")
7286
}
7387

74-
# serve
88+
# serve (return serve_url)
7589
run_serve_daemon(
7690
"preview",
7791
file,

man/quarto_preview.Rd

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-preview.R

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
test_that("quarto_preview default functionality", {
2+
skip_if_no_quarto()
3+
skip_on_cran()
4+
5+
tmp_dir <- withr::local_tempdir()
6+
withr::local_dir(tmp_dir)
7+
xfun::write_utf8(c("---", "title: Test", "---", "", "# Hello"), "test.qmd")
8+
9+
expect_no_error({
10+
url <- withr::with_dir(tmp_dir, {
11+
quarto_preview("test.qmd", browse = FALSE, quiet = TRUE)
12+
})
13+
})
14+
15+
# Always clean up
16+
withr::defer(quarto_preview_stop())
17+
18+
if (exists("url")) {
19+
expect_true(grepl("^https?://", url))
20+
}
21+
})
22+
23+
test_that("quarto_preview can change port", {
24+
skip_if_no_quarto()
25+
skip_on_cran()
26+
27+
tmp_dir <- withr::local_tempdir()
28+
withr::local_dir(tmp_dir)
29+
xfun::write_utf8(c("---", "title: Test", "---", "", "# Hello"), "test.qmd")
30+
31+
expect_no_error({
32+
url <- withr::with_dir(tmp_dir, {
33+
quarto_preview("test.qmd", port = 8888, browse = FALSE, quiet = TRUE)
34+
})
35+
})
36+
37+
# Always clean up
38+
withr::defer(quarto_preview_stop())
39+
40+
if (exists("url")) {
41+
expect_true(grepl("^https?://", url))
42+
expect_true(grepl(":8888", url))
43+
}
44+
})

0 commit comments

Comments
 (0)