Select a pygments style to use Parameters ---------- style: str The style specifier to use. If None, auto-select a style. is_in_notebook: bool Whether python is currently running in a jupyter notebook. Used for automatic selection. Returns ------
(
style: str | None, is_in_notebook: bool
)
| 186 | |
| 187 | |
| 188 | def _get_pygments_style( |
| 189 | style: str | None, is_in_notebook: bool |
| 190 | ) -> Union["pygments.style.Style", str] | None: |
| 191 | """Select a pygments style to use |
| 192 | |
| 193 | Parameters |
| 194 | ---------- |
| 195 | style: str |
| 196 | |
| 197 | The style specifier to use. If None, auto-select a style. |
| 198 | |
| 199 | is_in_notebook: bool |
| 200 | |
| 201 | Whether python is currently running in a jupyter notebook. |
| 202 | Used for automatic selection. |
| 203 | |
| 204 | Returns |
| 205 | ------- |
| 206 | style: Optional[Union['pygments.style.Style',str]] |
| 207 | |
| 208 | If pygments is installed, the style object or string, suitable |
| 209 | for use as the "style" argument to pygments formatters. If |
| 210 | pygments is not installed, returns None. |
| 211 | |
| 212 | """ |
| 213 | try: |
| 214 | # pylint: disable=import-outside-toplevel |
| 215 | import pygments |
| 216 | from packaging import version |
| 217 | from pygments.style import Style |
| 218 | from pygments.token import Comment, Keyword, Name, Number, Operator, String |
| 219 | |
| 220 | if version.parse(pygments.__version__) < version.parse("2.4.0"): |
| 221 | raise ImportError("Required Pygments version >= 2.4.0 but got " + pygments.__version__) |
| 222 | except ImportError as err: |
| 223 | if err.name == "packaging": |
| 224 | name = "packaging" |
| 225 | elif err.name == "pygments": |
| 226 | name = "Pygments>=2.4.0" |
| 227 | else: |
| 228 | raise ValueError(f'Package "{err.name}" should not be used') |
| 229 | |
| 230 | with warnings.catch_warnings(): |
| 231 | warnings.simplefilter("once", UserWarning) |
| 232 | install_cmd = sys.executable + f' -m pip install "{name}" --upgrade --user' |
| 233 | warnings.warn( |
| 234 | str(err) |
| 235 | + "\n" |
| 236 | + f"To print highlighted TVM script, please install {name}:\n" |
| 237 | + install_cmd, |
| 238 | category=UserWarning, |
| 239 | ) |
| 240 | return None |
| 241 | |
| 242 | class JupyterLight(Style): |
| 243 | """A Jupyter-Notebook-like Pygments style configuration (aka. "light")""" |
| 244 | |
| 245 | background_color = "" |