Code
print(f"{CYAN}This text is cyan!{RESET}")
print(f"{MAGENTA}This text is magenta!{RESET}")
print(f"{BLUE}This text is blue!{RESET}")
[96mThis text is cyan![0m [95mThis text is magenta![0m [94mThis text is blue![0m
Colorful console output makes it easier to distinguish between different types of messages (e.g., errors, warnings, and informational messages). It also enhances readability and provides a more engaging experience for readers. Imagine debugging a complex script and instantly spotting errors in red or warnings in yellow. With Quarto, you can bring this experience to your rendered documents.
To enable ANSI-colored output in Quarto, you need to configure the document’s YAML header and include a JavaScript library (ansi_up
) to render the colors correctly. Here’s how to do it:
python
In Python, you can define color constants for different colors using ANSI escape sequences.
= "\033[96m"
CYAN = "\033[95m"
MAGENTA = "\033[94m"
BLUE = "\033[0m" RESET
In your Quarto document’s YAML header, add the following configuration:
format:
html:
code-ansi: true
ansi-colors:
enable: true
include-in-header:
- text: |
<script type="module">
import { AnsiUp } from "https://cdn.jsdelivr.net/npm/ansi_up@6.0.2/+esm";
document.addEventListener("DOMContentLoaded", function () {
const ansiUp = new AnsiUp({ escape_for_html: false });
const selector = '[data-class-output="code-rendered"] p';
const codeOutputs = document.querySelectorAll(selector);
const LINE_BREAKS = ["[BR", "<br />"]
codeOutputs.forEach((output) => {
const lines = output.innerText.split(LINE_BREAKS[0]);
let processedLines = lines.map((line) => ansiUp.ansi_to_html(line));
processedLines = processedLines.map(line => line.replace(/:::$/g, ''));
output.innerHTML = processedLines.join(LINE_BREAKS[1]);
});
}); </script>
class-output
for Code BlocksTo render colorful output, use the class-output
attribute in your code blocks. For example:
print(f"{CYAN}This text is cyan!{RESET}")
print(f"{MAGENTA}This text is magenta!{RESET}")
print(f"{BLUE}This text is blue!{RESET}")
[96mThis text is cyan![0m [95mThis text is magenta![0m [94mThis text is blue![0m
To correctly handle line breaks, use a custom line break character in your code (e.g., [BR
) and render it in the header section using JavaScript.
See: ansi_up issue #73
= "[BR"
LINE_BREAK print(f"{CYAN}This text is cyan!{RESET}", end=LINE_BREAK)
print(f"{MAGENTA}This text is magenta!{RESET}", end=LINE_BREAK)
print(f"{BLUE}This text is blue!{RESET}", end=LINE_BREAK)
[96mThis text is cyan![0m[BR[95mThis text is magenta![0m[BR[94mThis text is blue![0m[BR:::
The JavaScript snippet in the header processes these custom line breaks and converts them into HTML <br />
tags:
const LINE_BREAKS = ["[BR", "<br />"]
.forEach((output) => {
codeOutputsconst lines = output.innerText.split(LINE_BREAKS[0]);
let processedLines = lines.map((line) => ansiUp.ansi_to_html(line));
= processedLines.map(line => line.replace(/:::$/g, ''));
processedLines .innerHTML = processedLines.join(LINE_BREAKS[1]);
output; })
code-ansi: true
: This enables ANSI color support in Quarto.ansi_up
Library: The JavaScript library ansi_up
converts ANSI escape codes into HTML spans with appropriate styles.class-output: code-rendered
: This ensures that the output is processed by ansi_up
and rendered with colors.By configuring Quarto to support ANSI-colored output, you can create more engaging and readable documents. Whether you’re showcasing terminal workflows, debugging logs, or simply adding visual flair, colorful console output is a powerful tool in your Quarto arsenal.