chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:17:40 +08:00
commit f1825c8ceb
10096 changed files with 2364182 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
from ray.widgets.render import Template
from ray.widgets.util import make_table_html_repr
__all__ = ["Template", "make_table_html_repr"]
+44
View File
@@ -0,0 +1,44 @@
import pathlib
from typing import List, Union
from ray.util.annotations import DeveloperAPI
@DeveloperAPI
class Template:
"""Class which provides basic HTML templating."""
def __init__(self, file: str):
with open(pathlib.Path(__file__).parent / "templates" / file, "r") as f:
self.template = f.read()
def render(self, **kwargs: Union[str, List]) -> str:
"""Render an HTML template with the given data.
This is done by replacing instances of `{{ key }}` with `value`
from the keyword arguments.
Args:
**kwargs: Mapping of template variable names to their substitution
values. Each value is either a string or a list of strings;
lists are joined together before substitution.
Returns:
HTML template with the keys of the kwargs replaced with corresponding
values.
"""
rendered = self.template
for key, value in kwargs.items():
if isinstance(value, List):
value = "".join(value)
rendered = rendered.replace("{{ " + key + " }}", value if value else "")
return rendered
@staticmethod
def list_templates() -> List[pathlib.Path]:
"""List the available HTML templates.
Returns:
A list of files with .html.j2 extensions inside ../templates/
"""
return (pathlib.Path(__file__).parent / "templates").glob("*.html.j2")
@@ -0,0 +1,6 @@
<div class="lm-Widget p-Widget lm-Panel p-Panel jp-Cell-outputWrapper">
<div style="margin-left: 50px;display: flex;flex-direction: row;align-items: center">
{{ context_logo }}
{{ context_table }}
</div>
</div>
@@ -0,0 +1,4 @@
<tr>
<td style="text-align: left"><b>Dashboard:</b></td>
<td style="text-align: left"><b><a href="{{ dashboard_url }}" target="_blank">{{ dashboard_url }}</a></b></td>
</tr>
@@ -0,0 +1,13 @@
<div class="jp-RenderedHTMLCommon" style="display: flex; flex-direction: row;">
<svg viewBox="0 0 567 224" fill="none" xmlns="http://www.w3.org/2000/svg" style="height: 3em;">
<g clip-path="url(#clip0_4338_178347)">
<path d="M341.29 165.561H355.29L330.13 129.051C345.63 123.991 354.21 112.051 354.21 94.2307C354.21 71.3707 338.72 58.1807 311.88 58.1807H271V165.561H283.27V131.661H311.8C314.25 131.661 316.71 131.501 319.01 131.351L341.25 165.561H341.29ZM283.29 119.851V70.0007H311.82C331.3 70.0007 342.34 78.2907 342.34 94.5507C342.34 111.271 331.34 119.861 311.82 119.861L283.29 119.851ZM451.4 138.411L463.4 165.561H476.74L428.74 58.1807H416L367.83 165.561H380.83L392.83 138.411H451.4ZM446.19 126.601H398L422 72.1407L446.24 126.601H446.19ZM526.11 128.741L566.91 58.1807H554.35L519.99 114.181L485.17 58.1807H472.44L514.01 129.181V165.541H526.13V128.741H526.11Z" fill="var(--jp-ui-font-color0)"/>
<path d="M82.35 104.44C84.0187 97.8827 87.8248 92.0678 93.1671 87.9146C98.5094 83.7614 105.083 81.5067 111.85 81.5067C118.617 81.5067 125.191 83.7614 130.533 87.9146C135.875 92.0678 139.681 97.8827 141.35 104.44H163.75C164.476 101.562 165.622 98.8057 167.15 96.2605L127.45 56.5605C121.071 60.3522 113.526 61.6823 106.235 60.3005C98.9443 58.9187 92.4094 54.9203 87.8602 49.0574C83.3109 43.1946 81.0609 35.8714 81.5332 28.4656C82.0056 21.0599 85.1679 14.0819 90.4252 8.8446C95.6824 3.60726 102.672 0.471508 110.08 0.0272655C117.487 -0.416977 124.802 1.86091 130.647 6.4324C136.493 11.0039 140.467 17.5539 141.821 24.8501C143.175 32.1463 141.816 39.6859 138 46.0505L177.69 85.7505C182.31 82.9877 187.58 81.4995 192.962 81.4375C198.345 81.3755 203.648 82.742 208.33 85.3976C213.012 88.0532 216.907 91.9029 219.616 96.5544C222.326 101.206 223.753 106.492 223.753 111.875C223.753 117.258 222.326 122.545 219.616 127.197C216.907 131.848 213.012 135.698 208.33 138.353C203.648 141.009 198.345 142.375 192.962 142.313C187.58 142.251 182.31 140.763 177.69 138L138 177.7C141.808 184.071 143.155 191.614 141.79 198.91C140.424 206.205 136.44 212.75 130.585 217.313C124.731 221.875 117.412 224.141 110.004 223.683C102.596 223.226 95.6103 220.077 90.3621 214.828C85.1139 209.58 81.9647 202.595 81.5072 195.187C81.0497 187.779 83.3154 180.459 87.878 174.605C92.4405 168.751 98.9853 164.766 106.281 163.401C113.576 162.035 121.119 163.383 127.49 167.19L167.19 127.49C165.664 124.941 164.518 122.182 163.79 119.3H141.39C139.721 125.858 135.915 131.673 130.573 135.826C125.231 139.98 118.657 142.234 111.89 142.234C105.123 142.234 98.5494 139.98 93.2071 135.826C87.8648 131.673 84.0587 125.858 82.39 119.3H60C58.1878 126.495 53.8086 132.78 47.6863 136.971C41.5641 141.163 34.1211 142.972 26.7579 142.059C19.3947 141.146 12.6191 137.574 7.70605 132.014C2.79302 126.454 0.0813599 119.29 0.0813599 111.87C0.0813599 104.451 2.79302 97.2871 7.70605 91.7272C12.6191 86.1673 19.3947 82.5947 26.7579 81.6817C34.1211 80.7686 41.5641 82.5781 47.6863 86.7696C53.8086 90.9611 58.1878 97.2456 60 104.44H82.35ZM100.86 204.32C103.407 206.868 106.759 208.453 110.345 208.806C113.93 209.159 117.527 208.258 120.522 206.256C123.517 204.254 125.725 201.276 126.771 197.828C127.816 194.38 127.633 190.677 126.253 187.349C124.874 184.021 122.383 181.274 119.205 179.577C116.027 177.88 112.359 177.337 108.826 178.042C105.293 178.746 102.113 180.654 99.8291 183.44C97.5451 186.226 96.2979 189.718 96.3 193.32C96.2985 195.364 96.7006 197.388 97.4831 199.275C98.2656 201.163 99.4132 202.877 100.86 204.32ZM204.32 122.88C206.868 120.333 208.453 116.981 208.806 113.396C209.159 109.811 208.258 106.214 206.256 103.219C204.254 100.223 201.275 98.0151 197.827 96.97C194.38 95.9249 190.676 96.1077 187.348 97.4873C184.02 98.8669 181.274 101.358 179.577 104.536C177.879 107.714 177.337 111.382 178.041 114.915C178.746 118.448 180.653 121.627 183.439 123.911C186.226 126.195 189.717 127.443 193.32 127.44C195.364 127.443 197.388 127.042 199.275 126.259C201.163 125.476 202.878 124.328 204.32 122.88ZM122.88 19.4205C120.333 16.8729 116.981 15.2876 113.395 14.9347C109.81 14.5817 106.213 15.483 103.218 17.4849C100.223 19.4868 98.0146 22.4654 96.9696 25.9131C95.9245 29.3608 96.1073 33.0642 97.4869 36.3922C98.8665 39.7202 101.358 42.4668 104.535 44.1639C107.713 45.861 111.381 46.4036 114.914 45.6992C118.447 44.9949 121.627 43.0871 123.911 40.301C126.195 37.515 127.442 34.0231 127.44 30.4205C127.44 28.3772 127.038 26.3539 126.255 24.4664C125.473 22.5788 124.326 20.8642 122.88 19.4205ZM19.42 100.86C16.8725 103.408 15.2872 106.76 14.9342 110.345C14.5813 113.93 15.4826 117.527 17.4844 120.522C19.4863 123.518 22.4649 125.726 25.9127 126.771C29.3604 127.816 33.0638 127.633 36.3918 126.254C39.7198 124.874 42.4664 122.383 44.1635 119.205C45.8606 116.027 46.4032 112.359 45.6988 108.826C44.9944 105.293 43.0866 102.114 40.3006 99.8296C37.5145 97.5455 34.0227 96.2983 30.42 96.3005C26.2938 96.3018 22.337 97.9421 19.42 100.86ZM100.86 100.86C98.3125 103.408 96.7272 106.76 96.3742 110.345C96.0213 113.93 96.9226 117.527 98.9244 120.522C100.926 123.518 103.905 125.726 107.353 126.771C110.8 127.816 114.504 127.633 117.832 126.254C121.16 124.874 123.906 122.383 125.604 119.205C127.301 116.027 127.843 112.359 127.139 108.826C126.434 105.293 124.527 102.114 121.741 99.8296C118.955 97.5455 115.463 96.2983 111.86 96.3005C109.817 96.299 107.793 96.701 105.905 97.4835C104.018 98.2661 102.303 99.4136 100.86 100.86Z" fill="#00AEEF"/>
</g>
<defs>
<clipPath id="clip0_4338_178347">
<rect width="566.93" height="223.75" fill="white"/>
</clipPath>
</defs>
</svg>
</div>
@@ -0,0 +1,11 @@
<table class="jp-RenderedHTMLCommon" style="border-collapse: collapse;color: var(--jp-ui-font-color1);font-size: var(--jp-ui-font-size1);">
<tr>
<td style="text-align: left"><b>Python version:</b></td>
<td style="text-align: left"><b>{{ python_version }}</b></td>
</tr>
<tr>
<td style="text-align: left"><b>Ray version:</b></td>
<td style="text-align: left"><b>{{ ray_version }}</b></td>
</tr>
{{ dashboard_row }}
</table>
@@ -0,0 +1,9 @@
<div class="vDivider"></div>
<style>
.vDivider {
border-left-width: var(--jp-border-width);
border-left-color: var(--jp-border-color0);
border-left-style: solid;
margin: 0.5em 1em 0.5em 1em;
}
</style>
@@ -0,0 +1,3 @@
<div class='jp-RenderedHTMLCommon'>
{{ content }}
</div>
@@ -0,0 +1,18 @@
<div class="runConfig">
<div class="generalSettings">
{{ settings }}
</div>
<div class="sideBySide">
{{ subconfigs }}
</div>
</div>
<style>
.sideBySide {
display: flex;
flex-direction: row;
gap: 1em;
}
.generalSettings {
border-bottom: var(--jp-border-width) solid var(--jp-border-color0);
}
</style>
@@ -0,0 +1,20 @@
<div class="scrollableTable jp-RenderedHTMLCommon">
{{ table }}
</div>
<style>
.scrollableTable {
overflow-y: auto;
max-height: {{ max_height }};
}
.scrollableTable table {
width: 100%;
}
.scrollableTable table :is(th,td) {
text-align: left !important;
}
.scrollableTable th {
background: var(--jp-layout-color1);
position: sticky;
top: 0;
}
</style>
@@ -0,0 +1,11 @@
<div class='titleData jp-RenderedHTMLCommon'>
<h3>{{ title }}</h3>
{{ data }}
</div>
<style>
.titleData h3 {
border-bottom-width: var(--jp-border-width);
border-bottom-color: var(--jp-border-color0);
border-bottom-style: solid;
}
</style>
@@ -0,0 +1,4 @@
<div class='miniTitleData jp-RenderedHTMLCommon'>
<h4><b>{{ title }}</b></h4>
{{ data }}
</div>
@@ -0,0 +1,17 @@
<div class="trialProgress">
<h3>Trial Progress</h3>
{{ table }}
</div>
<style>
.trialProgress {
display: flex;
flex-direction: column;
color: var(--jp-ui-font-color1);
}
.trialProgress h3 {
font-weight: bold;
}
.trialProgress td {
white-space: nowrap;
}
</style>
@@ -0,0 +1,49 @@
<div class="tuneStatus">
<div style="display: flex;flex-direction: row">
<div style="display: flex;flex-direction: column;">
<h3>Tune Status</h3>
{{ status_table }}
</div>
<div class="vDivider"></div>
<div class="systemInfo">
<h3>System Info</h3>
{{ sys_info_message }}
</div>
{{ messages }}
</div>
<div class="hDivider"></div>
<div class="trialStatus">
<h3>Trial Status</h3>
{{ trial_progress }}
</div>
</div>
<style>
.tuneStatus {
color: var(--jp-ui-font-color1);
}
.tuneStatus .systemInfo {
display: flex;
flex-direction: column;
}
.tuneStatus td {
white-space: nowrap;
}
.tuneStatus .trialStatus {
display: flex;
flex-direction: column;
}
.tuneStatus h3 {
font-weight: bold;
}
.tuneStatus .hDivider {
border-bottom-width: var(--jp-border-width);
border-bottom-color: var(--jp-border-color0);
border-bottom-style: solid;
}
.tuneStatus .vDivider {
border-left-width: var(--jp-border-width);
border-left-color: var(--jp-border-color0);
border-left-style: solid;
margin: 0.5em 1em 0.5em 1em;
}
</style>
@@ -0,0 +1,25 @@
<div class="vDivider"></div>
<div class="messages">
<h3>Messages</h3>
{{ memory_message }}
{{ trial_progress_messages }}
{{ trial_errors }}
</div>
<style>
.messages {
color: var(--jp-ui-font-color1);
display: flex;
flex-direction: column;
padding-left: 1em;
overflow-y: auto;
}
.messages h3 {
font-weight: bold;
}
.vDivider {
border-left-width: var(--jp-border-width);
border-left-color: var(--jp-border-color0);
border-left-style: solid;
margin: 0.5em 1em 0.5em 1em;
}
</style>
+208
View File
@@ -0,0 +1,208 @@
import importlib
import logging
import sys
import textwrap
from functools import wraps
from typing import Any, Callable, Iterable, Optional, TypeVar, Union
from packaging.version import Version
from ray._private.thirdparty.tabulate.tabulate import tabulate
from ray.util.annotations import DeveloperAPI
from ray.widgets import Template
logger = logging.getLogger(__name__)
F = TypeVar("F", bound=Callable[..., Any])
@DeveloperAPI
def make_table_html_repr(
obj: Any, title: Optional[str] = None, max_height: str = "none"
) -> str:
"""Generate a generic html repr using a table.
Args:
obj: Object for which a repr is to be generated
title: If present, a title for the section is included
max_height: Maximum height of the table; valid values
are given by the max-height CSS property
Returns:
HTML representation of the object
"""
data = {}
for k, v in vars(obj).items():
if isinstance(v, (str, bool, int, float)):
data[k] = str(v)
elif isinstance(v, dict) or hasattr(v, "__dict__"):
data[k] = Template("scrollableTable.html.j2").render(
table=tabulate(
v.items() if isinstance(v, dict) else vars(v).items(),
tablefmt="html",
showindex=False,
headers=["Setting", "Value"],
),
max_height="none",
)
table = Template("scrollableTable.html.j2").render(
table=tabulate(
data.items(),
tablefmt="unsafehtml",
showindex=False,
headers=["Setting", "Value"],
),
max_height=max_height,
)
if title:
content = Template("title_data.html.j2").render(title=title, data=table)
else:
content = table
return content
def _has_missing(
*deps: Iterable[Union[str, Optional[str]]], message: Optional[str] = None
):
"""Return a list of missing dependencies.
Args:
*deps: Dependencies to check for, as ``(lib, version)`` tuples.
message: Message to be emitted if a dependency isn't found
Returns:
A list of dependencies which can't be found, if any
"""
missing = []
for (lib, _) in deps:
if importlib.util.find_spec(lib) is None:
missing.append(lib)
if missing:
if not message:
message = f"Run `pip install {' '.join(missing)}` for rich notebook output."
# stacklevel=3: First level is this function, then ensure_notebook_deps,
# then the actual function affected.
logger.info(f"Missing packages: {missing}. {message}", stacklevel=3)
return missing
def _has_outdated(
*deps: Iterable[Union[str, Optional[str]]], message: Optional[str] = None
):
outdated = []
for (lib, version) in deps:
try:
module = importlib.import_module(lib)
if version and Version(module.__version__) < Version(version):
outdated.append([lib, version, module.__version__])
except ImportError:
pass
if outdated:
outdated_strs = []
install_args = []
for lib, version, installed in outdated:
outdated_strs.append(f"{lib}=={installed} found, needs {lib}>={version}")
install_args.append(f"{lib}>={version}")
outdated_str = textwrap.indent("\n".join(outdated_strs), " ")
install_str = " ".join(install_args)
if not message:
message = f"Run `pip install -U {install_str}` for rich notebook output."
# stacklevel=3: First level is this function, then ensure_notebook_deps,
# then the actual function affected.
logger.info(f"Outdated packages:\n{outdated_str}\n{message}", stacklevel=3)
return outdated
@DeveloperAPI
def repr_with_fallback(
*notebook_deps: Iterable[Union[str, Optional[str]]]
) -> Callable[[F], F]:
"""Decorator which strips rich notebook output from mimebundles in certain cases.
Fallback to plaintext and don't use rich output in the following cases:
1. In a notebook environment and the appropriate dependencies are not installed.
2. In a ipython shell environment.
3. In Google Colab environment.
See https://github.com/googlecolab/colabtools/ issues/60 for more information
about the status of this issue.
Args:
*notebook_deps: The required dependencies and version for notebook
environment, as ``(lib, version)`` tuples.
Returns:
A function that returns the usual _repr_mimebundle_, unless any of the 3
conditions above hold, in which case it returns a mimebundle that only contains
a single text/plain mimetype.
"""
message = (
"Run `pip install -U ipywidgets`, then restart "
"the notebook server for rich notebook output."
)
if _can_display_ipywidgets(*notebook_deps, message=message):
def wrapper(func: F) -> F:
@wraps(func)
def wrapped(self, *args, **kwargs):
return func(self, *args, **kwargs)
return wrapped
else:
def wrapper(func: F) -> F:
@wraps(func)
def wrapped(self, *args, **kwargs):
return {"text/plain": repr(self)}
return wrapped
return wrapper
def _get_ipython_shell_name() -> str:
if "IPython" in sys.modules:
from IPython import get_ipython
return get_ipython().__class__.__name__
return ""
def _can_display_ipywidgets(*deps, message) -> bool:
# Default to safe behavior: only display widgets if running in a notebook
# that has valid dependencies
if in_notebook() and not (
_has_missing(*deps, message=message) or _has_outdated(*deps, message=message)
):
return True
return False
@DeveloperAPI
def in_notebook(shell_name: Optional[str] = None) -> bool:
"""Return whether we are in a Jupyter notebook or qtconsole."""
if not shell_name:
shell_name = _get_ipython_shell_name()
return shell_name == "ZMQInteractiveShell"
@DeveloperAPI
def in_ipython_shell(shell_name: Optional[str] = None) -> bool:
"""Return whether we are in a terminal running IPython"""
if not shell_name:
shell_name = _get_ipython_shell_name()
return shell_name == "TerminalInteractiveShell"