Print TVMScript string with Pygments highlight and Black auto-formatting. Parameters ---------- printable : Union[IRModule, PrimFunc, str] The TVMScript to be printed style : str, optional Pygmentize printing style, auto-detected if None. black_format: bool
(
printable: Any | str,
style: str | None = None,
black_format: bool = False,
)
| 27 | |
| 28 | |
| 29 | def cprint( |
| 30 | printable: Any | str, |
| 31 | style: str | None = None, |
| 32 | black_format: bool = False, |
| 33 | ) -> None: |
| 34 | """Print TVMScript string with Pygments highlight and Black auto-formatting. |
| 35 | |
| 36 | Parameters |
| 37 | ---------- |
| 38 | printable : Union[IRModule, PrimFunc, str] |
| 39 | |
| 40 | The TVMScript to be printed |
| 41 | |
| 42 | style : str, optional |
| 43 | |
| 44 | Pygmentize printing style, auto-detected if None. |
| 45 | |
| 46 | black_format: bool |
| 47 | |
| 48 | If true, use the formatter Black to format the TVMScript |
| 49 | |
| 50 | Notes |
| 51 | ----- |
| 52 | |
| 53 | The style parameter follows the Pygments style names or Style objects. Three |
| 54 | built-in styles are extended: "light", "dark" and "ansi". By default, "light" |
| 55 | will be used for notebook environment and terminal style will be "ansi" for |
| 56 | better style consistency. As an fallback when the optional Pygment library is |
| 57 | not installed, plain text will be printed with a one-time warning to suggest |
| 58 | installing the Pygment library. Other Pygment styles can be found in |
| 59 | https://pygments.org/styles/ |
| 60 | |
| 61 | The default pygmentize style can also be set with the environment |
| 62 | variable "TVM_PYGMENTIZE_STYLE". |
| 63 | """ |
| 64 | if hasattr(printable, "script") and callable(getattr(printable, "script")): |
| 65 | printable = printable.script() |
| 66 | elif not isinstance(printable, str): |
| 67 | raise TypeError( |
| 68 | f"Only can print strings or objects with `script` method, but got: {type(printable)}" |
| 69 | ) |
| 70 | |
| 71 | if black_format: |
| 72 | printable = _format(printable) |
| 73 | |
| 74 | is_in_notebook = "ipykernel" in sys.modules # in notebook env (support html display). |
| 75 | |
| 76 | style = _get_pygments_style(style, is_in_notebook) |
| 77 | |
| 78 | if style is None: |
| 79 | print(printable) |
| 80 | return |
| 81 | |
| 82 | # pylint: disable=import-outside-toplevel |
| 83 | from pygments import highlight |
| 84 | from pygments.formatters import HtmlFormatter, Terminal256Formatter |
| 85 | from pygments.lexers.python import Python3Lexer |
| 86 |