From 2e9e9f382fa5b6021995910946ab3c26edcac7cd Mon Sep 17 00:00:00 2001 From: Hammad <30986043+HammadTheOne@users.noreply.github.com> Date: Wed, 22 Jan 2020 16:27:00 -0500 Subject: [PATCH 1/3] Added `get_relative_path` and `strip_relative_path` functions. --- R/dash.R | 19 +++++++++++++++++++ R/utils.R | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/R/dash.R b/R/dash.R index 9c86c5a0..6b3a7fcf 100644 --- a/R/dash.R +++ b/R/dash.R @@ -763,6 +763,25 @@ Dash <- R6::R6Class( sep="/"))) }, + # ------------------------------------------------------------------------ + # return relative asset URLs + # ------------------------------------------------------------------------ + + get_relative_path = function(path, requests_pathname_prefix = self$config$requests_pathname_prefix) { + asset = get_relative_path(requests_pathname = requests_pathname_prefix, path = path) + return(asset) + }, + + + # ------------------------------------------------------------------------ + # return relative asset URLs + # ------------------------------------------------------------------------ + + strip_relative_path = function(path, requests_pathname_prefix = self$config$requests_pathname_prefix) { + asset = strip_relative_path(requests_pathname = requests_pathname_prefix, path = path) + return(asset) + }, + # ------------------------------------------------------------------------ # convenient fiery wrappers # ------------------------------------------------------------------------ diff --git a/R/utils.R b/R/utils.R index ae0ed9ef..e64ca04a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1270,3 +1270,46 @@ tryCompress <- function(request, response) { } return(response$compress()) } + +get_relative_path <- function(requests_pathname, path) { + # Returns a path with the config setting 'requests_pathname_prefix' prefixed to + # it. This is particularly useful for apps deployed on Dash Enterprise, which makes + # it easier to serve apps under both URL prefixes and localhost. + + if (requests_pathname == "/" && path == "") { + return("/") + } + else if (requests_pathname != "/" && path == "") { + return(requests_pathname) + } + else if (!startsWith(path, "/")) { + stop(sprintf(paste0("Unsupported relative rath! Paths that aren't prefixed" , + "with a leading '/' are not supported. You supplied '%s'."), + path)) + } + else { + return(paste(gsub("/$", "", requests_pathname), gsub("^/", "", path), sep = "/")) + } +} + +strip_relative_path <- function(requests_pathname, path) { + # Returns a relative path with the `requests_pathname_prefix` and leadings and trailing + # slashes stripped from it. This function is particularly relevant to dccLocation pathname routing. + + if (is.null(path)) { + return(NULL) + } + else if ((requests_pathname != "/" && !startsWith(path, gsub("/$", "", requests_pathname))) + || (requests_pathname == "/" && !startsWith(path, "/"))) { + stop(sprintf(paste0("Unsupported relative path! Path's that are not prefixed ", + "with a leading 'requests_pathname_prefix` are not suported. ", + "You supplied '%s', and requests_pathname_prefix was '%s'."), + path, requests_pathname + )) + } + else if (requests_pathname != "/" && startsWith(path, gsub("/$", "", requests_pathname))) { + path = sub(gsub("/$", "", requests_pathname), "", path) + } + return(trimws(gsub("/", "", path))) +} + From 7cc2e7ce58831043932abf60fbd6ecd0742c8c92 Mon Sep 17 00:00:00 2001 From: Hammad <30986043+HammadTheOne@users.noreply.github.com> Date: Thu, 23 Jan 2020 13:14:29 -0500 Subject: [PATCH 2/3] Added description and examples for method usage. --- R/dash.R | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/R/dash.R b/R/dash.R index 6b3a7fcf..a5c2120f 100644 --- a/R/dash.R +++ b/R/dash.R @@ -114,6 +114,27 @@ #' 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{`get_relative_path(path, requests_pathname_prefix)`}{ +#' The `get_relative_path` method simplifies the handling of URLs and pathnames for apps +#' running locally and on a deployment server such as Dash Enterprise. It handles the prefix +#' for requesting assets similar to the `get_asset_url` method, but can also be used for URL handling +#' in components such as `dccLink` or `dccLocation`. For example, `app$get_relative_url("/page/")` +#' would return `/app/page/` for an app running on a deployment server. The path must be prefixed with +#' a `/`. +#' \describe{ +#' \item{path}{Character. A path string prefixed with a leading `/` which directs at a path or asset directory.} +#' \item{requests_pathname_prefix}{Character. The pathname prefix for the app on a deployed application. Defaults to the environment variable set by the server, or `""` if run locally.} +#' } +#' #' \item{`strip_relative_path(path, requests_pathname_prefix)`}{ +#' The `strip_relative_path` method simplifies the handling of URLs and pathnames for apps +#' running locally and on a deployment server such as Dash Enterprise. It acts almost opposite the `get_relative_path` +#' method, by taking a `relative path` as an input, and returning the `path` stripped of the `requests_pathname_prefiex`, +#' and any leading or trailing `/`. For example, a path string `/app/homepage/`, would be returned as +#' `homepage`. This is particularly useful for `dccLocation` URL routing. +#' \describe{ +#' \item{path}{Character. A path string prefixed with a leading `/` and `requests_pathname_prefix` which directs at a path or asset directory.} +#' \item{requests_pathname_prefix}{Character. The pathname prefix for the app on a deployed application. Defaults to the environment variable set by the server, or `""` if run locally.} +#' } #' \item{`run_server(host = Sys.getenv('HOST', "127.0.0.1"), #' port = Sys.getenv('PORT', 8050), block = TRUE, showcase = FALSE, ...)`}{ #' The `run_server` method has 13 formal arguments, several of which are optional: From 412137e04fd07d19f3a91899a135ebac9f4f5a44 Mon Sep 17 00:00:00 2001 From: Ryan Patrick Kyle Date: Tue, 11 Feb 2020 17:20:34 -0500 Subject: [PATCH 3/3] :hocho: grapes of rath --- R/utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index e64ca04a..4e28b7e1 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1283,7 +1283,7 @@ get_relative_path <- function(requests_pathname, path) { return(requests_pathname) } else if (!startsWith(path, "/")) { - stop(sprintf(paste0("Unsupported relative rath! Paths that aren't prefixed" , + stop(sprintf(paste0("Unsupported relative path! Paths that aren't prefixed" , "with a leading '/' are not supported. You supplied '%s'."), path)) }