Skip to content

Commit

Permalink
Close #137: fix logic for inferring window title from Tag/TagList obj…
Browse files Browse the repository at this point in the history
…ects
  • Loading branch information
cpsievert committed Apr 25, 2022
1 parent ba5e120 commit 669a966
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
2 changes: 1 addition & 1 deletion shiny/ui/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

from .._docstring import add_example
from ._html_dependencies import jqui_deps
from ._page import get_window_title
from ._utils import get_window_title


# TODO: make a python version of the layout guide?
Expand Down
33 changes: 2 additions & 31 deletions shiny/ui/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,26 @@
)

import sys
from typing import Optional, Any, List, Union
from warnings import warn
from typing import Optional, Any, Union

if sys.version_info >= (3, 8):
from typing import Literal
else:
from typing_extensions import Literal

from htmltools import (
HTMLDependency,
tags,
Tag,
TagList,
div,
TagChildArg,
head_content,
)

from .._docstring import add_example
from ._html_dependencies import bootstrap_deps
from ._navs import navs_bar
from ..types import MISSING, MISSING_TYPE
from ._utils import get_window_title


def page_navbar(
Expand Down Expand Up @@ -127,24 +125,6 @@ def page_navbar(
)


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):
# Try to infer window_title from contents of title
window_title = " ".join(_find_characters(title))
if not window_title:
warn(
"Unable to infer a `window_title` default from `title`. Consider providing a character string to `window_title`."
)

if isinstance(window_title, MISSING_TYPE):
return None
else:
return head_content(tags.title(window_title))


@add_example()
def page_fluid(
*args: Any, title: Optional[str] = None, lang: Optional[str] = None, **kwargs: str
Expand Down Expand Up @@ -254,12 +234,3 @@ def page_bootstrap(
page = TagList(*bootstrap_deps(), *args)
head = tags.title(title) if title else None
return tags.html(tags.head(head), tags.body(page), lang=lang)


def _find_characters(x: Any) -> List[str]:
if isinstance(x, str):
return [x]
elif isinstance(x, list):
return [y for y in x if isinstance(y, str)]
else:
return []
36 changes: 35 additions & 1 deletion shiny/ui/_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
from htmltools import tags, Tag, TagChildArg
from typing import Optional, Union
import warnings

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):
x = x.children
if isinstance(x, TagList):
return " ".join([_find_child_strings(y) for y in x])
return x if isinstance(x, str) else ""

0 comments on commit 669a966

Please sign in to comment.