-
-
Notifications
You must be signed in to change notification settings - Fork 31
Support for index page templating #168
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
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
f6c13a9
Dash for R v0.2.0 (#162)
rpkyle f81fcb5
Update README.md
rpkyle a58d4f2
Added initial implementation of index customization with strings
HammadTheOne 4d72243
Fixed issue with `collect_assets` being undefined
HammadTheOne 30251cf
Added description and details.
HammadTheOne 3346a93
Added description
HammadTheOne 2e58b37
Added error messages and warnings.
HammadTheOne 0984660
Merge branch 'dev' into 42-index-page-templating
HammadTheOne 3aa0f50
Updated version to 0.3.0
HammadTheOne a2410d5
Added CRLF
HammadTheOne a158a37
Updated environment variable names to `dev` versions.
HammadTheOne 1138e4c
Improved error message function, made parity changes for index keys.
HammadTheOne fd67f8a
Added available keys to description.
HammadTheOne 17a61d1
Parity changes to `app_entry` and `config` keys
HammadTheOne 13ebec4
Cleaned up description.
HammadTheOne ab54aa0
Added `interpolate_str` helper function to `utils.R`
HammadTheOne ce4a8b6
Changes to default index, consolidating `_dash-config`
HammadTheOne 036e4e5
Added template index and logic for choosing correct index.
HammadTheOne 09b0851
Added description for `interpolate_index`.
HammadTheOne 4023b67
Updated glue arguments in `index_string` method, added example index.
HammadTheOne 8e98370
Update to `interpolate_index` to reflect changes to `index_string`.
HammadTheOne 0ce3158
Bugfix, referenced correct private variable.
HammadTheOne 7fd1ebe
Update R/dash.R
rpkyle a0c2f7e
:hocho: cat(template)
5ae7549
:camel: make concise
79b0faa
replace = with <-
39d52e0
:dromedary_camel: DRY up key validation logic
8b71875
:black_large_square: use invisible
f3dbfb9
use latest pkgs
e3a40c3
update package docs
df3d780
update docs; remove name, add title
84f8464
note title method
5bbba6e
:see_no_evil: need default app name
2aef1f3
:rotating_light: add integration tests
56904ba
fix ref for dcc
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
Package: dash | ||
Title: An Interface to the Dash Ecosystem for Authoring Reactive Web Applications | ||
Version: 0.2.0 | ||
Version: 0.3.0 | ||
Authors@R: c(person("Chris", "Parmer", role = c("aut"), email = "[email protected]"), person("Ryan Patrick", "Kyle", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-5829-9867"), email = "[email protected]"), person("Carson", "Sievert", role = c("aut"), comment = c(ORCID = "0000-0002-4958-2844")), person(family = "Plotly Technologies", role = "cph")) | ||
Description: A framework for building analytical web applications, Dash offers a pleasant and productive development experience. No JavaScript required. | ||
Depends: | ||
R (>= 3.0.2) | ||
Imports: | ||
dashHtmlComponents (== 1.0.2), | ||
dashCoreComponents (== 1.6.0), | ||
dashTable (== 4.5.1), | ||
dashCoreComponents (== 1.8.0), | ||
dashTable (== 4.6.0), | ||
R6, | ||
fiery (> 1.0.0), | ||
routr (> 0.2.0), | ||
|
@@ -32,8 +32,8 @@ Collate: | |
'print.R' | ||
'internal.R' | ||
Remotes: plotly/dash-html-components@55c3884, | ||
rpkyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
plotly/dash-core-components@c107e0f, | ||
plotly/dash-table@3058bd5 | ||
plotly/dash-core-components@fc153b4, | ||
plotly/dash-table@79d46ca | ||
License: MIT + file LICENSE | ||
Encoding: UTF-8 | ||
LazyData: true | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
#' @usage Dash | ||
#' | ||
#' @section Constructor: Dash$new( | ||
#' name = "dash", | ||
#' name = NULL, | ||
#' server = fiery::Fire$new(), | ||
#' assets_folder = 'assets', | ||
#' assets_url_path = '/assets', | ||
|
@@ -24,7 +24,7 @@ | |
#' @section Arguments: | ||
#' \tabular{lll}{ | ||
#' `name` \tab \tab Character. The name of the Dash application (placed in the `<title>` | ||
#' of the HTML page).\cr | ||
#' of the HTML page). DEPRECATED; please use `index_string()` or `interpolate_index()` instead.\cr | ||
#' `server` \tab \tab The web server used to power the application. | ||
#' Must be a [fiery::Fire] object.\cr | ||
#' `assets_folder` \tab \tab Character. A path, relative to the current working directory, | ||
|
@@ -100,6 +100,9 @@ | |
#' from the Dash backend. The latter may offer improved performance relative | ||
#' to callbacks written in R. | ||
#' } | ||
#' \item{`title("dash")`}{ | ||
#' The title of the app. If no title is supplied, Dash for R will use 'dash'. | ||
#' } | ||
#' \item{`callback_context()`}{ | ||
#' The `callback_context` method permits retrieving the inputs which triggered | ||
#' the firing of a given callback, and allows introspection of the input/state | ||
|
@@ -114,7 +117,63 @@ | |
#' 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('HOST', "127.0.0.1"), | ||
#' \item{`index_string(string)`}{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 |
||
#' The `index_string` method allows the specification of a custom index by changing | ||
rpkyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' the default `HTML` template that is generated by the Dash UI. Meta tags, CSS, Javascript, | ||
#' are some examples of features that can be modified. | ||
#' This method will present a warning if your HTML template is missing any necessary elements | ||
#' and return an error if a valid index is not defined. The following interpolation keys are | ||
#' currently supported: | ||
#' \describe{ | ||
#' \item{`{%metas%}`}{Optional - The registered meta tags.} | ||
#' \item{`{%favicon%}`}{Optional - A favicon link tag if found in assets.} | ||
#' \item{`{%css%}`}{Optional - Link tags to css resources.} | ||
#' \item{`{%config%}`}{Required - Config generated by dash for the renderer.} | ||
#' \item{`{%app_entry%}`}{Required - The container where dash react components are rendered.} | ||
#' \item{`{%scripts%}`}{Required - Collected dependencies scripts tags.} | ||
#' } | ||
#' \describe{ | ||
#' \item{Example of a basic HTML index string:}{ | ||
#' \preformatted{ | ||
#' "<!DOCTYPE html> | ||
#' <html> | ||
#' <head> | ||
#' \{\%meta_tags\%\} | ||
#' <title>\{\{%css\%\}\}</title> | ||
#' \{\%favicon\%\} | ||
#' \{\%css_tags\%\} | ||
#' </head> | ||
#' <body> | ||
#' \{\%app_entry\%\} | ||
#' <footer> | ||
#' \{\%config\%\} | ||
#' \{\%scripts\%\} | ||
#' </footer> | ||
#' </body> | ||
#' </html>" | ||
#' } | ||
#' } | ||
#' } | ||
#' } | ||
#' \item{`interpolate_index(template_index, ...)`}{ | ||
#' With the `interpolate_index` method, we can pass a custom index with template string | ||
#' variables that are already evaluated. We can directly pass arguments to the `template_index` | ||
#' by assigning them to variables present in the template. This is similar to the `index_string` method | ||
#' but offers the ability to change the default components of the Dash index as seen in the example below: | ||
#' \preformatted{ | ||
#' app$interpolate_index( | ||
#' template_index, | ||
#' metas = "<meta_charset='UTF-8'/>", | ||
#' renderer = renderer, | ||
#' config = config) | ||
#' } | ||
#' \describe{ | ||
#' \item{template_index}{Character. A formatted string with the HTML index string. Defaults to the initial template} | ||
#' \item{...}{Named List. The unnamed arguments can be passed as individual named lists corresponding to the components | ||
#' of the Dash html index. These include the same arguments as those found in the `index_string()` template.} | ||
#' } | ||
#' } | ||
#' \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: | ||
#' \describe{ | ||
|
@@ -174,7 +233,7 @@ Dash <- R6::R6Class( | |
config = list(), | ||
|
||
# i.e., the Dash$new() method | ||
initialize = function(name = "dash", | ||
initialize = function(name = NULL, | ||
server = fiery::Fire$new(), | ||
assets_folder = 'assets', | ||
assets_url_path = '/assets', | ||
|
@@ -191,13 +250,18 @@ Dash <- R6::R6Class( | |
suppress_callback_exceptions = FALSE) { | ||
|
||
# argument type checking | ||
assertthat::assert_that(is.character(name)) | ||
assertthat::assert_that(inherits(server, "Fire")) | ||
assertthat::assert_that(is.logical(serve_locally)) | ||
assertthat::assert_that(is.logical(suppress_callback_exceptions)) | ||
|
||
# save relevant args as private fields | ||
private$name <- name | ||
if (!is.null(name)) { | ||
warning(sprintf( | ||
"The supplied application title, '%s', should be set using the title() method, or passed via index_string() or interpolate_index(); it has been ignored, and 'dash' will be used instead.", | ||
name), | ||
call. = FALSE | ||
) | ||
} | ||
private$serve_locally <- serve_locally | ||
private$eager_loading <- eager_loading | ||
# remove leading and trailing slash(es) if present | ||
|
@@ -763,11 +827,44 @@ Dash <- R6::R6Class( | |
sep="/"))) | ||
}, | ||
|
||
# ------------------------------------------------------------------------ | ||
# specify a custom index string | ||
# ------------------------------------------------------------------------ | ||
index_string = function(string) { | ||
private$custom_index <- validate_keys(string) | ||
}, | ||
rpkyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# ------------------------------------------------------------------------ | ||
# modify the templated variables by using the `interpolate_index` method. | ||
# ------------------------------------------------------------------------ | ||
interpolate_index = function(template_index = private$template_index[[1]], ...) { | ||
template = template_index | ||
kwargs <- list(...) | ||
|
||
for (name in names(kwargs)) { | ||
key = paste0('\\{\\%', name, '\\%\\}') | ||
template = sub(key, kwargs[[name]], template) | ||
} | ||
|
||
invisible(validate_keys(names(kwargs))) | ||
|
||
private$template_index <- template | ||
}, | ||
|
||
# ------------------------------------------------------------------------ | ||
# specify a custom title | ||
# ------------------------------------------------------------------------ | ||
title = function(string = "dash") { | ||
assertthat::assert_that(is.character(string)) | ||
private$name <- string | ||
}, | ||
|
||
# ------------------------------------------------------------------------ | ||
# convenient fiery wrappers | ||
# ------------------------------------------------------------------------ | ||
run_server = function(host = Sys.getenv('HOST', "127.0.0.1"), | ||
port = Sys.getenv('PORT', 8050), | ||
|
||
block = TRUE, | ||
showcase = FALSE, | ||
use_viewer = FALSE, | ||
|
@@ -1266,6 +1363,24 @@ Dash <- R6::R6Class( | |
|
||
# akin to https://github.com/plotly/dash/blob/d2ebc837/dash/dash.py#L338 | ||
# note discussion here https://github.com/plotly/dash/blob/d2ebc837/dash/dash.py#L279-L284 | ||
custom_index = NULL, | ||
template_index = c( | ||
"<!DOCTYPE html> | ||
<html> | ||
<head> | ||
{%meta_tags%} | ||
<title>{%title%}</title> | ||
{%favicon%} | ||
{%css_tags%} | ||
</head> | ||
<body> | ||
{%app_entry%} | ||
<footer> | ||
{%config%} | ||
{%scripts%} | ||
</footer> | ||
</body> | ||
</html>", NA), | ||
.index = NULL, | ||
|
||
generateReloadHash = function() { | ||
|
@@ -1434,13 +1549,32 @@ Dash <- R6::R6Class( | |
css_tags <- all_tags[["css_tags"]] | ||
|
||
# retrieve script tags for serving in the index | ||
scripts_tags <- all_tags[["scripts_tags"]] | ||
scripts <- all_tags[["scripts_tags"]] | ||
|
||
# insert meta tags if present | ||
meta_tags <- all_tags[["meta_tags"]] | ||
|
||
# define the react-entry-point | ||
app_entry <- "<div id='react-entry-point'><div class='_dash-loading'>Loading...</div></div>" | ||
# define the dash default config key | ||
config <- sprintf("<script id='_dash-config' type='application/json'> %s </script>", to_JSON(self$config)) | ||
|
||
if (is.null(private$name)) | ||
private$name <- 'dash' | ||
|
||
if (!is.null(private$custom_index)) { | ||
string_index <- glue::glue(private$custom_index, .open = "{%", .close = "%}") | ||
|
||
private$.index <- string_index | ||
} | ||
|
||
else if (length(private$template_index) == 1) { | ||
private$.index <- private$template_index | ||
} | ||
|
||
private$.index <- sprintf( | ||
'<!DOCTYPE html> | ||
else { | ||
private$.index <- sprintf( | ||
'<!DOCTYPE html> | ||
<html> | ||
<head> | ||
%s | ||
|
@@ -1450,23 +1584,22 @@ Dash <- R6::R6Class( | |
</head> | ||
|
||
<body> | ||
<div id="react-entry-point"> | ||
<div class="_dash-loading">Loading...</div> | ||
</div> | ||
|
||
%s | ||
<footer> | ||
<script id="_dash-config" type="application/json"> %s </script> | ||
%s | ||
%s | ||
</footer> | ||
</body> | ||
</html>', | ||
meta_tags, | ||
private$name, | ||
favicon, | ||
css_tags, | ||
to_JSON(self$config), | ||
scripts_tags | ||
) | ||
meta_tags, | ||
private$name, | ||
favicon, | ||
css_tags, | ||
app_entry, | ||
config, | ||
scripts | ||
) | ||
} | ||
} | ||
) | ||
) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.