Skip to content

Minor fix for get_asset_url and documentation for same #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
41 changes: 28 additions & 13 deletions R/dash.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 `<meta>`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.
#' }
#'
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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)
}
}
}
Expand Down
31 changes: 22 additions & 9 deletions man/Dash.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.