Skip to content
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
31 changes: 29 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 @@ -1113,6 +1132,14 @@ def head_content(*args: TagChildArg) -> HTMLDependency:
*args
The content to place in the ``<head>``.

Note
----
If the same content, ``x``, is included in a document multiple times via
``head_content(x)``, ``x`` will only appear once in the final HTML document's
``<head>``. More often than not, this is desirable behavior, but if you need the
same content included multiple times, you can add some irrelevant/empty tags (e.g.,
``TagList(x, Tag("meta"))``) to make sure ``x`` is included multiple times.

Example
-------
>>> from htmltools import *
Expand All @@ -1132,7 +1159,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
10 changes: 9 additions & 1 deletion htmltools/_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import hashlib
import importlib
import os
import re
import importlib
import tempfile
from typing import (
Any,
Expand Down Expand Up @@ -127,6 +128,13 @@ 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.
"""
return hashlib.sha1(s.encode('utf-8')).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_81fe8bfe87576c3ecb22426f8e57847382917acf[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_81fe8bfe87576c3ecb22426f8e57847382917acf[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_81fe8bfe87576c3ecb22426f8e57847382917acf[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_f51fa154cb6a6ca2ef221e02b00e3f2e48570fe7[0.0];headcontent_59a6679e93d43c2db5b3ef7e865480dc61a63cb3[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