Skip to content

Commit

Permalink
Merge pull request #139 from plotly/favicon
Browse files Browse the repository at this point in the history
Favicon
  • Loading branch information
waralex authored Oct 25, 2021
2 parents cb40b5a + f064a4f commit e6d713d
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ JSON = "0.21"
JSON2 = "0.3"
MD5 = "0.2"
PlotlyBase = "0.8.5, 0.8.6"
julia = "1.3"
YAML = "0.4.7"
julia = "1.3"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
Binary file added src/favicon.ico
Binary file not shown.
39 changes: 27 additions & 12 deletions src/handler/index_page.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function resource_url(app::DashApp, resource::AppRelativeResource)
function resource_url(app::DashApp, resource::AppRelativeResource)
prefix = get_setting(app, :requests_pathname_prefix)
return string(prefix,
"_dash-component-suites/",
resource.namespace,
"/",
build_fingerprint(resource.relative_path, resource.version, resource.ts)
build_fingerprint(resource.relative_path, resource.version, resource.ts)
)
end

Expand All @@ -19,7 +19,7 @@ function asset_path(app::DashApp, path::AbstractString)
end

resource_url(app::DashApp, resource::AppExternalResource) = resource.url
function resource_url(app::DashApp, resource::AppAssetResource)
function resource_url(app::DashApp, resource::AppAssetResource)
return string(
asset_path(app, resource.path),
"?m=", resource.ts
Expand All @@ -42,7 +42,7 @@ function metas_html(app::DashApp)
get(tag, "http-equiv", "") == "X-UA-Compatible"
end
has_charset = any(tag -> haskey(tag, "charset"), meta_tags)

result = String[]
!has_ie_compat && push!(result, "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">")
!has_charset && push!(result, "<meta charset=\"UTF-8\">")
Expand All @@ -52,7 +52,7 @@ function metas_html(app::DashApp)

end

function css_html(app::DashApp, resources::ApplicationResources)
function css_html(app::DashApp, resources::ApplicationResources)
join(
make_css_tag.(Ref(app), resources.css), "\n "
)
Expand All @@ -77,7 +77,7 @@ app_entry_html() = """
</div>
"""

function config_html(app::DashApp)
function config_html(app::DashApp)
config = Dict{Symbol, Any}(
:url_base_pathname => get_setting(app, :url_base_pathname),
:requests_pathname_prefix => get_setting(app, :requests_pathname_prefix),
Expand All @@ -94,20 +94,35 @@ function config_html(app::DashApp)
)
end
return """<script id="_dash-config" type="application/json">$(JSON2.write(config))</script>"""
end
end


renderer_html() = """<script id="_dash-renderer" type="application/javascript">var renderer = new DashRenderer();</script>"""

favicon_html(app::DashApp) = ""
function favicon_html(app::DashApp, resources::ApplicationResources)
favicon_url = if !isnothing(resources.favicon)
asset_path(app, resources.favicon.path)
else
"$(get_setting(app, :requests_pathname_prefix))_favicon.ico?v=$(build_info().dash_version)"
end
return format_tag(
"link",
Dict(
"rel" => "icon",
"type" => "image/x-icon",
"href" => favicon_url
),
opened = true
)
end


function index_page(app::DashApp, resources::ApplicationResources)

function index_page(app::DashApp, resources::ApplicationResources)

result = interpolate_string(app.index_string,
metas = metas_html(app),
title = app.title,
favicon = favicon_html(app),
favicon = favicon_html(app, resources),
css = css_html(app, resources),
app_entry = app_entry_html(),
config = config_html(app),
Expand All @@ -121,7 +136,7 @@ function index_page(app::DashApp, resources::ApplicationResources)
"#_dash_config" => r"id=\"_dash-config\"",
"dash-renderer" => r"src=\"[^\"]*dash[-_]renderer[^\"]*\"",
"new DashRenderer" => r"id=\"_dash-renderer",
]
]
)
return result
end
2 changes: 2 additions & 0 deletions src/handler/make_handler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include("processors/index.jl")
include("processors/layout.jl")
include("processors/reload_hash.jl")
include("processors/resource.jl")
include("processors/default_favicon.jl")

function start_reload_poll(state::HandlerState)
folders = Set{String}()
Expand Down Expand Up @@ -124,6 +125,7 @@ function make_handler(app::DashApp, registry::ResourcesRegistry; check_layout =
add_route!(process_layout, router, "$(prefix)_dash-layout")
add_route!(process_dependencies, router, "$(prefix)_dash-dependencies")
add_route!(process_reload_hash, router, "$(prefix)_reload-hash")
add_route!(process_default_favicon, router, "$(prefix)_favicon.ico")
add_route!(process_resource, router, "$(prefix)_dash-component-suites/<namespace>/<path>")
add_route!(process_assets, router, "$(prefix)$(assets_url_path)/<file_path>")
add_route!(process_callback, router, "POST", "$(prefix)_dash-update-component")
Expand Down
10 changes: 10 additions & 0 deletions src/handler/processors/default_favicon.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function process_default_favicon(request::HTTP.Request, state::HandlerState)
ico_contents = read(
joinpath(ROOT_PATH, "src", "favicon.ico")
)
return HTTP.Response(
200,
["Content-Type" => "image/x-icon"],
body = ico_contents
)
end
20 changes: 10 additions & 10 deletions src/resources/application.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ struct ApplicationResources
files ::Dict{String, NamespaceFiles}
css ::Vector{AppResource}
js ::Vector{AppResource}
favicon ::Union{Nothing, String}
favicon ::Union{Nothing, AppAssetResource}
ApplicationResources(files, css, js, favicon) = new(files, css, js, favicon)
end


function ApplicationResources(app::DashApp, registry::ResourcesRegistry)
css = AppResource[]
js = AppResource[]
favicon::Union{Nothing, String} = nothing
favicon::Union{Nothing, AppResource} = nothing
files = Dict{String, NamespaceFiles}()

serve_locally = get_setting(app, :serve_locally)
assets_external_path = get_setting(app, :assets_external_path)
dev = get_devsetting(app, :serve_dev_bundles)
eager_loading = get_setting(app, :eager_loading)

append_pkg = function(pkg)
append!(css,
_convert_resource_pkg(pkg, :css, dev = dev, serve_locally = serve_locally, eager_loading = eager_loading)
Expand All @@ -58,10 +58,10 @@ function ApplicationResources(app::DashApp, registry::ResourcesRegistry)

append_pkg(get_dash_dependencies(registry, get_devsetting(app, :props_check)))

append!(css,
append!(css,
_convert_external.(get_setting(app, :external_stylesheets))
)
append!(js,
append!(js,
_convert_external.(get_setting(app, :external_scripts))
)

Expand All @@ -72,7 +72,7 @@ function ApplicationResources(app::DashApp, registry::ResourcesRegistry)
elseif type == :css
push!(css, asset_resource(url, modified))
elseif type == :favicon
favicon = url
favicon = AppAssetResource(url, modified)
end
end
end
Expand Down Expand Up @@ -113,7 +113,7 @@ function walk_assets(callback, app::DashApp)
assets_filter = isempty(assets_ignore) ?
(f) -> true :
(f) -> !occursin(assets_regex, f)

assets_path = get_assets_path(app)
if get_setting(app, :include_assets_files) && isdir(assets_path)
for (base, dirs, files) in walkdir(assets_path)
Expand Down Expand Up @@ -181,9 +181,9 @@ function _convert_resource_pkg(pkg::ResourcePkg, type::Symbol; dev, serve_locall
ts = ispath(pkg.path) ? trunc(Int64, stat(pkg.path).mtime) : 0
for resource in iterator
append!(
result,
result,
_convert_resource(
resource,
resource,
namespace = pkg.namespace,
version = pkg.version,
ts = ts,
Expand Down
Loading

0 comments on commit e6d713d

Please sign in to comment.