diff --git a/CHANGELOG.md b/CHANGELOG.md index 87042b2c..3e4f728f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. - Additional line number context inserted when available within stack traces [#133](https://github.com/plotly/dashR/pull/133) - Integration and unit tests are now performed when commits are made to open pull requests - Support returning asset URLs via `app$get_asset_url` when app is loaded via `source()` or `APP_ROOT_PATH` environment variable is defined [#160](https://github.com/plotly/dashR/pull/160) +- `url_base_pathname` added; mimics functionality in Dash for Python, sets defaults for `routes_pathname_prefix` and `requests_pathname_prefix` when not otherwise provided [#161](https://github.com/plotly/dashR/pull/161) ### Changed - `dash-renderer` updated to v1.2.2 [#137](https://github.com/plotly/dashR/pull/137) @@ -25,6 +26,7 @@ All notable changes to this project will be documented in this file. - Patch for `reqres` package to handle cookies containing multiple "=" [#122](https://github.com/plotly/dashR/pull/122) - Handling for user-defined errors in callbacks implemented [#116](https://github.com/plotly/dashR/pull/116) - Fixes for hot reloading interval handling and refreshing apps within viewer pane [#148](https://github.com/plotly/dashR/pull/148) +- `get_asset_url` checks `getAppPath()` as well as `DASH_APP_ROOT_PATH` environment variable when invoked [#161](https://github.com/plotly/dashR/pull/161) ## [0.1.0] - 2019-07-10 ### Added diff --git a/R/dash.R b/R/dash.R index e12c7f70..ba122302 100644 --- a/R/dash.R +++ b/R/dash.R @@ -13,8 +13,9 @@ #' assets_ignore = '', #' serve_locally = TRUE, #' meta_tags = NULL, -#' routes_pathname_prefix = '/', -#' requests_pathname_prefix = '/', +#' url_base_pathname = '/', +#' routes_pathname_prefix = NULL, +#' requests_pathname_prefix = NULL, #' external_scripts = NULL, #' external_stylesheets = NULL, #' suppress_callback_exceptions = FALSE @@ -35,19 +36,23 @@ #' `assets_ignore` \tab \tab Character. A regular expression, to match assets to omit from #' immediate loading. Ignored files will still be served if specifically requested. You #' cannot use this to prevent access to sensitive files. \cr -#' `serve_locally` \tab \tab Whether to serve HTML dependencies locally or +#' `serve_locally` \tab \tab Logical. Whether to serve HTML dependencies locally or #' remotely (via URL).\cr #' `meta_tags` \tab \tab List of lists. HTML ``tags to be added to the index page. #' Each list element should have the attributes and values for one tag, eg: #' `list(name = 'description', content = 'My App')`.\cr -#' `routes_pathname_prefix` \tab \tab a prefix applied to the backend routes.\cr -#' `requests_pathname_prefix` \tab \tab a prefix applied to request endpoints -#' made by Dash's front-end.\cr -#' `external_scripts` \tab \tab An optional list of valid URLs from which +#' `url_base_pathname` \tab \tab Character. A local URL prefix to use app-wide. Default is +#' `/`. Both `requests_pathname_prefix` and `routes_pathname_prefix` default to `url_base_pathname`. +#' Environment variable is `DASH_URL_BASE_PATHNAME`.\cr +#' `routes_pathname_prefix` \tab \tab Character. A prefix applied to the backend routes. +#' Environment variable is `DASH_ROUTES_PATHNAME_PREFIX`.\cr +#' `requests_pathname_prefix` \tab \tab Character. A prefix applied to request endpoints +#' made by Dash's front-end. Environment variable is `DASH_REQUESTS_PATHNAME_PREFIX`.\cr +#' `external_scripts` \tab \tab List. An optional list of valid URLs from which #' to serve JavaScript source for rendered pages.\cr -#' `external_stylesheets` \tab \tab An optional list of valid URLs from which +#' `external_stylesheets` \tab \tab List. An optional list of valid URLs from which #' to serve CSS for rendered pages.\cr -#' `suppress_callback_exceptions` \tab \tab Whether to relay warnings about +#' `suppress_callback_exceptions` \tab \tab Logical. Whether to relay warnings about #' possible layout mis-specifications when registering a callback. #' } #' @@ -101,6 +106,14 @@ #' values given their names. It is only available from within a callback; #' attempting to use this method outside of a callback will result in a warning. #' } +#' \item{`get_asset_url(asset_path, prefix)`}{ +#' The `get_asset_url` method permits retrieval of an asset's URL given its filename. +#' For example, `app$get_asset_url('style.css')` should return `/assets/style.css` when +#' `assets_folder = 'assets'`. By default, the prefix is the value of `requests_pathname_prefix`, +#' but this is configurable via the `prefix` parameter. Note: this method will +#' present a warning and return `NULL` if the Dash app was not loaded via `source()` +#' if the `DASH_APP_PATH` environment variable is undefined. +#' } #' \item{`run_server(host = Sys.getenv('DASH_HOST', "127.0.0.1"), #' port = Sys.getenv('DASH_PORT', 8050), block = TRUE, showcase = FALSE, ...)`}{ #' The `run_server` method has 13 formal arguments, several of which are optional: @@ -169,6 +182,7 @@ Dash <- R6::R6Class( assets_ignore = '', serve_locally = TRUE, meta_tags = NULL, + url_base_pathname = "/", routes_pathname_prefix = NULL, requests_pathname_prefix = NULL, external_scripts = NULL, @@ -199,8 +213,8 @@ Dash <- R6::R6Class( private$in_viewer <- FALSE # config options - self$config$routes_pathname_prefix <- resolve_prefix(routes_pathname_prefix, "DASH_ROUTES_PATHNAME_PREFIX") - self$config$requests_pathname_prefix <- resolve_prefix(requests_pathname_prefix, "DASH_REQUESTS_PATHNAME_PREFIX") + self$config$routes_pathname_prefix <- resolve_prefix(routes_pathname_prefix, "DASH_ROUTES_PATHNAME_PREFIX", url_base_pathname) + self$config$requests_pathname_prefix <- resolve_prefix(requests_pathname_prefix, "DASH_REQUESTS_PATHNAME_PREFIX", url_base_pathname) self$config$external_scripts <- external_scripts self$config$external_stylesheets <- external_stylesheets @@ -706,13 +720,13 @@ Dash <- R6::R6Class( # ------------------------------------------------------------------------ # return asset URLs # ------------------------------------------------------------------------ - get_asset_url = function(asset_path, prefix = "/") { + get_asset_url = function(asset_path, prefix = self$config$requests_pathname_prefix) { app_root_path <- Sys.getenv("DASH_APP_PATH") if (app_root_path == "" && getAppPath() != FALSE) { # app loaded via source(), root path is known app_root_path <- dirname(private$app_root_path) - } else { + } else if (getAppPath() == FALSE) { # app not loaded via source(), env var not set, no reliable way to ascertain root path 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.") return(NULL) @@ -954,6 +968,7 @@ Dash <- R6::R6Class( assets_folder = NULL, assets_url_path = NULL, assets_ignore = NULL, + url_base_pathname = NULL, routes_pathname_prefix = NULL, requests_pathname_prefix = NULL, suppress_callback_exceptions = NULL, diff --git a/R/utils.R b/R/utils.R index 2d1f65df..450040c0 100644 --- a/R/utils.R +++ b/R/utils.R @@ -457,7 +457,7 @@ valid_seq <- function(params) { } } -resolve_prefix <- function(prefix, environment_var) { +resolve_prefix <- function(prefix, environment_var, base_pathname) { if (!(is.null(prefix))) { assertthat::assert_that(is.character(prefix)) @@ -467,7 +467,11 @@ resolve_prefix <- function(prefix, environment_var) { if (prefix_env != "") { return(prefix_env) } else { - return("/") + env_base_pathname <- Sys.getenv("DASH_URL_BASE_PATHNAME") + if (env_base_pathname != "") + return(env_base_pathname) + else + return(base_pathname) } } } diff --git a/man/Dash.Rd b/man/Dash.Rd index a27f4a62..a93a5191 100644 --- a/man/Dash.Rd +++ b/man/Dash.Rd @@ -21,8 +21,9 @@ eager_loading = FALSE, assets_ignore = '', serve_locally = TRUE, meta_tags = NULL, -routes_pathname_prefix = '/', -requests_pathname_prefix = '/', +url_base_pathname = '/', +routes_pathname_prefix = NULL, +requests_pathname_prefix = NULL, external_scripts = NULL, external_stylesheets = NULL, suppress_callback_exceptions = FALSE @@ -45,19 +46,23 @@ and other files such as images will be served if requested. Default is \code{ass \code{assets_ignore} \tab \tab Character. A regular expression, to match assets to omit from immediate loading. Ignored files will still be served if specifically requested. You cannot use this to prevent access to sensitive files. \cr -\code{serve_locally} \tab \tab Whether to serve HTML dependencies locally or +\code{serve_locally} \tab \tab Logical. Whether to serve HTML dependencies locally or remotely (via URL).\cr \code{meta_tags} \tab \tab List of lists. HTML \code{}tags to be added to the index page. Each list element should have the attributes and values for one tag, eg: \code{list(name = 'description', content = 'My App')}.\cr -\code{routes_pathname_prefix} \tab \tab a prefix applied to the backend routes.\cr -\code{requests_pathname_prefix} \tab \tab a prefix applied to request endpoints -made by Dash's front-end.\cr -\code{external_scripts} \tab \tab An optional list of valid URLs from which +\code{url_base_pathname} \tab \tab Character. A local URL prefix to use app-wide. Default is +\code{/}. Both \code{requests_pathname_prefix} and \code{routes_pathname_prefix} default to \code{url_base_pathname}. +Environment variable is \code{DASH_URL_BASE_PATHNAME}.\cr +\code{routes_pathname_prefix} \tab \tab Character. A prefix applied to the backend routes. +Environment variable is \code{DASH_ROUTES_PATHNAME_PREFIX}.\cr +\code{requests_pathname_prefix} \tab \tab Character. A prefix applied to request endpoints +made by Dash's front-end. Environment variable is \code{DASH_REQUESTS_PATHNAME_PREFIX}.\cr +\code{external_scripts} \tab \tab List. An optional list of valid URLs from which to serve JavaScript source for rendered pages.\cr -\code{external_stylesheets} \tab \tab An optional list of valid URLs from which +\code{external_stylesheets} \tab \tab List. An optional list of valid URLs from which to serve CSS for rendered pages.\cr -\code{suppress_callback_exceptions} \tab \tab Whether to relay warnings about +\code{suppress_callback_exceptions} \tab \tab Logical. Whether to relay warnings about possible layout mis-specifications when registering a callback. } } @@ -115,6 +120,14 @@ the firing of a given callback, and allows introspection of the input/state values given their names. It is only available from within a callback; attempting to use this method outside of a callback will result in a warning. } +\item{\code{get_asset_url(asset_path, prefix)}}{ +The \code{get_asset_url} method permits retrieval of an asset's URL given its filename. +For example, \code{app$get_asset_url('style.css')} should return \code{/assets/style.css} when +\code{assets_folder = 'assets'}. By default, the prefix is the value of \code{requests_pathname_prefix}, +but this is configurable via the \code{prefix} parameter. Note: this method will +present a warning and return \code{NULL} if the Dash app was not loaded via \code{source()} +if the \code{DASH_APP_PATH} environment variable is undefined. +} \item{\code{run_server(host = Sys.getenv('DASH_HOST', "127.0.0.1"), port = Sys.getenv('DASH_PORT', 8050), block = TRUE, showcase = FALSE, ...)}}{ The \code{run_server} method has 13 formal arguments, several of which are optional: \describe{