-
Notifications
You must be signed in to change notification settings - Fork 81
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
Fix logic for inferring window title from Tag/TagList objects #138
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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,6 +1,42 @@ | ||
from htmltools import tags, Tag, TagChildArg | ||
from typing import Optional, Union | ||
|
||
from htmltools import ( | ||
tags, | ||
Tag, | ||
TagList, | ||
TagChildArg, | ||
TagChild, | ||
HTMLDependency, | ||
head_content, | ||
) | ||
|
||
from ..types import MISSING, MISSING_TYPE | ||
|
||
|
||
def shiny_input_label(id: str, label: TagChildArg = None) -> Tag: | ||
cls = "control-label" + ("" if label else " shiny-label-null") | ||
return tags.label(label, class_=cls, id=id + "-label", for_=id) | ||
|
||
|
||
def get_window_title( | ||
title: Optional[Union[str, Tag, TagList]], | ||
window_title: Union[str, MISSING_TYPE] = MISSING, | ||
) -> Optional[HTMLDependency]: | ||
if title is not None and isinstance(window_title, MISSING_TYPE): | ||
window_title = _find_child_strings(title) | ||
|
||
if isinstance(window_title, MISSING_TYPE): | ||
return None | ||
else: | ||
return head_content(tags.title(window_title)) | ||
|
||
|
||
def _find_child_strings(x: Union[Tag, TagList, TagChild]) -> str: | ||
if isinstance(x, Tag) and x.name not in ("script", "style"): | ||
x = x.children | ||
if isinstance(x, TagList): | ||
strings = [_find_child_strings(y) for y in x] | ||
return " ".join(filter(lambda x: x != "", strings)) | ||
if isinstance(x, str): | ||
return x | ||
return "" |
This file contains 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import textwrap | ||
|
||
from shiny import ui | ||
from htmltools import HTMLDocument, TagList, tags | ||
|
||
|
||
def test_panel_title(): | ||
x = HTMLDocument(ui.panel_title("Hello Shiny UI")).render()["html"] | ||
assert x == textwrap.dedent( | ||
"""\ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"/> | ||
<script type="application/html-dependencies">headcontent_648f698a6bce952fe1b165140306e01f4da9e164[0.0]</script> | ||
<title>Hello Shiny UI</title> | ||
</head> | ||
<body> | ||
<h2>Hello Shiny UI</h2> | ||
</body> | ||
</html>""" | ||
) | ||
|
||
title = TagList( | ||
tags.h1("A title"), | ||
tags.script("foo"), | ||
tags.style("foo"), | ||
tags.h5(tags.script("foo"), "A subtitle"), | ||
) | ||
|
||
x = HTMLDocument(ui.panel_title(title)).render()["html"] | ||
assert x == textwrap.dedent( | ||
"""\ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"/> | ||
<script type="application/html-dependencies">headcontent_9e055af8ef9fa0fb1ba72eeba8053498fbc0d075[0.0]</script> | ||
<title>A title A subtitle</title> | ||
</head> | ||
<body> | ||
<h1>A title</h1> | ||
<script>foo</script> | ||
<style>foo</style> | ||
<h5> | ||
<script>foo</script> | ||
A subtitle | ||
</h5> | ||
</body> | ||
</html>""" | ||
) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you filter as part of the list comprehension?
[_find_child_strings(y) for y in x if y != ""]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish, but it would miss cases where
y != ""
and_find_child_strings(y) == ""