Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 21 additions & 2 deletions htmltools/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@
if sys.version_info >= (3, 8):
from typing import TypedDict, SupportsIndex, Protocol, runtime_checkable, Literal
else:
from typing_extensions import TypedDict, SupportsIndex, Protocol, runtime_checkable, Literal
from typing_extensions import (
TypedDict,
SupportsIndex,
Protocol,
runtime_checkable,
Literal,
)

from packaging.version import Version

Expand All @@ -32,6 +38,7 @@
_package_dir, # type: ignore
_html_escape, # type: ignore
_flatten, # type: ignore
hash_deterministic,
)

__all__ = (
Expand Down Expand Up @@ -793,7 +800,19 @@ def _hoist_head_content(
# Put <meta charset="utf-8"> at beginning of head, and other hoisted tags at the
# end. This matters only if the <head> tag starts out with some children.
head.insert(0, Tag("meta", charset="utf-8"))

# Add some metadata about the dependencies so that shiny.js' renderDependency
# logic knows not to re-render them.
deps = x.get_dependencies()
if len(deps) > 0:
head.append(
Tag(
"script",
";".join([d.name + "[" + str(d.version) + "]" for d in deps]),
type="application/html-dependencies",
)
)

head.extend(
[
d.as_html_tags(lib_prefix=lib_prefix, include_version=include_version)
Expand Down Expand Up @@ -1132,7 +1151,7 @@ def head_content(*args: TagChildArg) -> HTMLDependency:
head = TagList(*args)
head_str = head.get_html_string()
# Create unique ID to use as name
name = "headcontent_{:x}".format(abs(hash(head_str)))
name = "headcontent_" + hash_deterministic(head_str)
return HTMLDependency(name=name, version="0.0", head=head)


Expand Down
12 changes: 11 additions & 1 deletion htmltools/_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from hashlib import sha256
import importlib
import os
import re
import importlib
import tempfile
from typing import (
Any,
Expand Down Expand Up @@ -127,6 +128,15 @@ def _package_dir(package: str) -> str:
return os.path.dirname(pkg_file)


def hash_deterministic(s: str) -> str:
"""
Returns a deterministic hash of the given string.
"""
h = sha256()
h.update(s.encode("utf-8"))
return h.hexdigest()


class _HttpServerInfo(NamedTuple):
port: int
thread: Thread
Expand Down
5 changes: 5 additions & 0 deletions tests/snapshots/snap_test_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">a[1.1]</script>
<script src="a-1.1/a1.js"></script>
</head>
<body>
Expand All @@ -23,6 +24,7 @@
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">a[1.2]</script>
<script src="a-1.2/a2.js"></script>
</head>
<body>
Expand All @@ -35,6 +37,7 @@
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">a[1.1]</script>
<script src="a-1.1/a1.js"></script>
</head>
<body>
Expand All @@ -49,6 +52,7 @@
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">a[1.1]</script>
<script src="a-1.1/a1.js"></script>
</head>
<body>
Expand All @@ -61,6 +65,7 @@
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">a[1.1]</script>
<script src="a-1.1/a1.js"></script>
</head>
<body>
Expand Down
7 changes: 4 additions & 3 deletions tests/test_deps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import os
from tempfile import TemporaryDirectory
from typing import Union, Optional
import textwrap

from htmltools import *
Expand Down Expand Up @@ -32,6 +29,7 @@ def test_dep_resolution():
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">a[1.2.1];b[1.10];c[1.0]</script>
<script src="a-1.2.1/a3.js"></script>
<script src="b-1.10/b2.js"></script>
<script src="c-1.0/c1.js"></script>
Expand All @@ -46,6 +44,7 @@ def test_dep_resolution():
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">a[1.2.1];b[1.10];c[1.0]</script>
<script src="libfoo/a-1.2.1/a3.js"></script>
<script src="libfoo/b-1.10/b2.js"></script>
<script src="libfoo/c-1.0/c1.js"></script>
Expand Down Expand Up @@ -93,6 +92,7 @@ def test_append_deps():
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">a[1.2];b[1.0]</script>
<script src="a-1.2/a2.js"></script>
<script src="b-1.0/b1.js"></script>
</head>
Expand Down Expand Up @@ -138,6 +138,7 @@ def fake_dep(**kwargs):
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">a[1.0]</script>
<link href="lib/a-1.0/css/bar%20foo.css" rel="stylesheet"/>
<script src="lib/a-1.0/js/foo%20bar.js"></script>
</head>
Expand Down
4 changes: 4 additions & 0 deletions tests/test_html_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def test_html_document_html_input():
<head>
<meta charset="utf-8"/>
<title>Title</title>
<script type="application/html-dependencies">headcontent_88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589[0.0]</script>
abcd
</head>
<body>
Expand All @@ -49,6 +50,7 @@ def test_html_document_html_input():
<html lang="en">
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">headcontent_88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589[0.0]</script>
abcd
</head>
<body>
Expand All @@ -68,6 +70,7 @@ def test_html_document_html_input():
<html lang="en">
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">headcontent_88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589[0.0]</script>
abcd
</head>
<body>
Expand Down Expand Up @@ -96,6 +99,7 @@ def test_html_document_head_hoisting():
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">headcontent_30f3d4728150b01fae1b01ebae18b85a4bef02f3181870a56e1dc7ff1f8edd1e[0.0];headcontent_6841afc257df6c2d7be0dfbc893c02ba25b805c16f224d373303fd58ba37697e[0.0]</script>
<script>alert('1')</script>
<style>span {color: red;}</style>
<script>alert('2')</script>
Expand Down
6 changes: 4 additions & 2 deletions tests/test_jsx_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_jsx_tags():
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">react[%s];react-dom[%s]</script>
<script src="lib/react-%s/react.production.min.js"></script>
<script src="lib/react-dom-%s/react-dom.production.min.js"></script>
</head>
Expand All @@ -33,7 +34,7 @@ def test_jsx_tags():
</script>
</body>
</html>"""
% (react_ver, react_dom_ver)
% (react_ver, react_dom_ver, react_ver, react_dom_ver)
)

# Only the "top-level" tag gets wrapped in <script> tags
Expand All @@ -43,6 +44,7 @@ def test_jsx_tags():
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">react[%s];react-dom[%s]</script>
<script src="lib/react-%s/react.production.min.js"></script>
<script src="lib/react-dom-%s/react-dom.production.min.js"></script>
</head>
Expand All @@ -64,7 +66,7 @@ def test_jsx_tags():
</script>
</body>
</html>"""
% (react_ver, react_dom_ver)
% (react_ver, react_dom_ver, react_ver, react_dom_ver)
)

x = Foo(
Expand Down
2 changes: 2 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ def test_html_save():
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">foo[1.0]</script>
<link href="foo-1.0/testdep/testdep.css" rel="stylesheet"/>
<script src="foo-1.0/testdep/testdep.js"></script>
</head>
Expand All @@ -253,6 +254,7 @@ def test_html_save():
<html lang="en">
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">foo[1.0]</script>
<link href="lib/foo-1.0/testdep/testdep.css" rel="stylesheet"/>
<script src="lib/foo-1.0/testdep/testdep.js"></script>
</head>
Expand Down