Skip to content

Commit 08d8d76

Browse files
author
Ryan Patrick Kyle
committed
quick 🐛 fix and add 📚 for get_asset_url
1 parent ca6613c commit 08d8d76

File tree

3 files changed

+56
-24
lines changed

3 files changed

+56
-24
lines changed

R/dash.R

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
#' assets_ignore = '',
1414
#' serve_locally = TRUE,
1515
#' meta_tags = NULL,
16-
#' routes_pathname_prefix = '/',
17-
#' requests_pathname_prefix = '/',
16+
#' url_base_pathname = '/',
17+
#' routes_pathname_prefix = NULL,
18+
#' requests_pathname_prefix = NULL,
1819
#' external_scripts = NULL,
1920
#' external_stylesheets = NULL,
2021
#' suppress_callback_exceptions = FALSE
@@ -35,19 +36,23 @@
3536
#' `assets_ignore` \tab \tab Character. A regular expression, to match assets to omit from
3637
#' immediate loading. Ignored files will still be served if specifically requested. You
3738
#' cannot use this to prevent access to sensitive files. \cr
38-
#' `serve_locally` \tab \tab Whether to serve HTML dependencies locally or
39+
#' `serve_locally` \tab \tab Logical. Whether to serve HTML dependencies locally or
3940
#' remotely (via URL).\cr
4041
#' `meta_tags` \tab \tab List of lists. HTML `<meta>`tags to be added to the index page.
4142
#' Each list element should have the attributes and values for one tag, eg:
4243
#' `list(name = 'description', content = 'My App')`.\cr
43-
#' `routes_pathname_prefix` \tab \tab a prefix applied to the backend routes.\cr
44-
#' `requests_pathname_prefix` \tab \tab a prefix applied to request endpoints
45-
#' made by Dash's front-end.\cr
46-
#' `external_scripts` \tab \tab An optional list of valid URLs from which
44+
#' `url_base_pathname` \tab \tab Character. A local URL prefix to use app-wide. Default is
45+
#' `/`. Both `requests_pathname_prefix` and `routes_pathname_prefix` default to `url_base_pathname`.
46+
#' Environment variable is `DASH_URL_BASE_PATHNAME`.\cr
47+
#' `routes_pathname_prefix` \tab \tab Character. A prefix applied to the backend routes.
48+
#' Environment variable is `DASH_ROUTES_PATHNAME_PREFIX`.\cr
49+
#' `requests_pathname_prefix` \tab \tab Character. A prefix applied to request endpoints
50+
#' made by Dash's front-end. Environment variable is `DASH_REQUESTS_PATHNAME_PREFIX`.\cr
51+
#' `external_scripts` \tab \tab List. An optional list of valid URLs from which
4752
#' to serve JavaScript source for rendered pages.\cr
48-
#' `external_stylesheets` \tab \tab An optional list of valid URLs from which
53+
#' `external_stylesheets` \tab \tab List. An optional list of valid URLs from which
4954
#' to serve CSS for rendered pages.\cr
50-
#' `suppress_callback_exceptions` \tab \tab Whether to relay warnings about
55+
#' `suppress_callback_exceptions` \tab \tab Logical. Whether to relay warnings about
5156
#' possible layout mis-specifications when registering a callback.
5257
#' }
5358
#'
@@ -101,6 +106,14 @@
101106
#' values given their names. It is only available from within a callback;
102107
#' attempting to use this method outside of a callback will result in a warning.
103108
#' }
109+
#' \item{`get_asset_url(asset_path, prefix)`}{
110+
#' The `get_asset_url` method permits retrieval of an asset's URL given its filename.
111+
#' For example, `app$get_asset_url('style.css')` should return `/assets/style.css` when
112+
#' `assets_folder = 'assets'`. By default, the prefix is the value of `requests_pathname_prefix`,
113+
#' but this is configurable via the `prefix` parameter. Note: this method will
114+
#' present a warning and return `NULL` if the Dash app was not loaded via `source()`
115+
#' if the `DASH_APP_PATH` environment variable is undefined.
116+
#' }
104117
#' \item{`run_server(host = Sys.getenv('DASH_HOST', "127.0.0.1"),
105118
#' port = Sys.getenv('DASH_PORT', 8050), block = TRUE, showcase = FALSE, ...)`}{
106119
#' The `run_server` method has 13 formal arguments, several of which are optional:
@@ -169,6 +182,7 @@ Dash <- R6::R6Class(
169182
assets_ignore = '',
170183
serve_locally = TRUE,
171184
meta_tags = NULL,
185+
url_base_pathname = "/",
172186
routes_pathname_prefix = NULL,
173187
requests_pathname_prefix = NULL,
174188
external_scripts = NULL,
@@ -199,8 +213,8 @@ Dash <- R6::R6Class(
199213
private$in_viewer <- FALSE
200214

201215
# config options
202-
self$config$routes_pathname_prefix <- resolve_prefix(routes_pathname_prefix, "DASH_ROUTES_PATHNAME_PREFIX")
203-
self$config$requests_pathname_prefix <- resolve_prefix(requests_pathname_prefix, "DASH_REQUESTS_PATHNAME_PREFIX")
216+
self$config$routes_pathname_prefix <- resolve_prefix(routes_pathname_prefix, "DASH_ROUTES_PATHNAME_PREFIX", url_base_pathname)
217+
self$config$requests_pathname_prefix <- resolve_prefix(requests_pathname_prefix, "DASH_REQUESTS_PATHNAME_PREFIX", url_base_pathname)
204218
self$config$external_scripts <- external_scripts
205219
self$config$external_stylesheets <- external_stylesheets
206220

@@ -706,13 +720,13 @@ Dash <- R6::R6Class(
706720
# ------------------------------------------------------------------------
707721
# return asset URLs
708722
# ------------------------------------------------------------------------
709-
get_asset_url = function(asset_path, prefix = "/") {
723+
get_asset_url = function(asset_path, prefix = self$config$requests_pathname_prefix) {
710724
app_root_path <- Sys.getenv("DASH_APP_PATH")
711725

712726
if (app_root_path == "" && getAppPath() != FALSE) {
713727
# app loaded via source(), root path is known
714728
app_root_path <- dirname(private$app_root_path)
715-
} else {
729+
} else if (getAppPath() == FALSE) {
716730
# app not loaded via source(), env var not set, no reliable way to ascertain root path
717731
warning("application not started via source(), and DASH_APP_PATH environment variable is undefined. get_asset_url returns NULL since root path cannot be reliably identified.")
718732
return(NULL)
@@ -954,6 +968,7 @@ Dash <- R6::R6Class(
954968
assets_folder = NULL,
955969
assets_url_path = NULL,
956970
assets_ignore = NULL,
971+
url_base_pathname = NULL,
957972
routes_pathname_prefix = NULL,
958973
requests_pathname_prefix = NULL,
959974
suppress_callback_exceptions = NULL,

R/utils.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ valid_seq <- function(params) {
457457
}
458458
}
459459

460-
resolve_prefix <- function(prefix, environment_var) {
460+
resolve_prefix <- function(prefix, environment_var, base_pathname) {
461461
if (!(is.null(prefix))) {
462462
assertthat::assert_that(is.character(prefix))
463463

@@ -467,7 +467,11 @@ resolve_prefix <- function(prefix, environment_var) {
467467
if (prefix_env != "") {
468468
return(prefix_env)
469469
} else {
470-
return("/")
470+
env_base_pathname <- Sys.getenv("DASH_URL_BASE_PATHNAME")
471+
if (env_base_pathname != "")
472+
return(env_base_pathname)
473+
else
474+
return(base_pathname)
471475
}
472476
}
473477
}

man/Dash.Rd

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

0 commit comments

Comments
 (0)