quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.93k stars 325 forks source link

Reveal.js with Julia: escape codes are not handled in output of executable code blocks #929

Open mroavi opened 2 years ago

mroavi commented 2 years ago

Here is a minimal working example to reproduce the problem. Note how the escape codes are not being handled but instead printed literally.

---
format:
  revealjs:
    code-fold: false
    echo: true
jupyter: julia-1.7
---

```{julia}
quote
  a = b + c
  d = e - f
end
```

image

This is what the output should look like:

image

Here is another example that uses the BenchmarkTools package to benchmark a function call:

---
format:
  revealjs:
    code-fold: false
    echo: true
jupyter: julia-1.7
---

```{julia}
using BenchmarkTools
@benchmark rand(100,100)
```

image

What the output should look like:

image

cscheid commented 2 years ago

I guess jupyter lab and jupyter notebook interpret the escape codes directly, which means we need to do that ourselves as well on text output.

@jjallaire I think a reasonable implementation of this would be to strip escape codes on anything but HTML output, and then use the HTML postprocessor to interpret them. (I'm pretty sure deno has a library to do that for us already, so it would be easiest there.)

cscheid commented 2 years ago

We will add support for color in the future (so I'm keeping this open) but for now we at least strip the ansi escape codes, so the output is more readable.

mroavi commented 2 years ago

ANSI escapes were stripped from plain text output here: https://github.com/quarto-dev/quarto-cli/commit/cd62560b2db5ed7c5f6a2994d19fb5bddcc9e79d

This is how it looks now:

image

Ignoring the colors, I still see some discrepancies between this image and the one produced by Jupyter. The shape of the character bars looks different. Does this have to do with the ANSI escape codes? or is it a different issue? It seems like a font issue to me.

cscheid commented 2 years ago

Yes, that's a font issue. Matching the graphical characters exactly to jupyter's particular output in this case would mean we can't support our own styling requirements.

But if you seek precise matching of those characters, you can override quarto's CSS with your own for code-formatted output.

cscheid commented 2 years ago

As of 2bd7b8719ecf8916f5754c0f3ac63ba5d7597c31, text output from jupyter engines containing escape codes renders with ansi-* classes, when in HTML output. By default we use the same stylesheet as IPython. But if you want to override that behavior, we wrap the pre element in a div with class ansi-escaped-output. This gives you a high-specificity selector to target: div.ansi-escaped-output pre for global changes, and div.ansi-escaped-output pre span.ansi-* for the particular colors and styles. This is the output I see now:

image

For non-HTML output, we presently just strip the escape codes. But it's on our radar to fix this for all formats.

mroavi commented 2 years ago

Thanks a lot for fixing this so quickly! It is looking pretty good right now.

jjallaire commented 2 years ago

@gaborcsardi We just implemented support for ansi color codes in quarto textual output. We currently have this working for Jupyter (see above). It seems like we should also be able to get this working for knitr as well w/ some combination of quarto and crayon being aware of each other and cooperating (e.g. via knit_print methods or hooks that we can install in quarto). LMK what you think.

gaborcsardi commented 2 years ago

Yeah, definitely. Right now crayon/cli rules out colors early in the detection process for knitr: https://github.com/r-lib/cli/blob/072ed21823cdedb5eb63adcc9dbd2685c8252383/R/num-ansi-colors.R#L77

You can force colors with an option or env var: https://github.com/r-lib/cli/blob/072ed21823cdedb5eb63adcc9dbd2685c8252383/R/num-ansi-colors.R#L39-L47 The problem with forcing is embedding, if you start another evaluation context or process, it might inherit these settings.

So often it is better to have crayon/cli auto-detect the configuration that supports colors, so e.g. if quarto is running and ANSI sequences are supported from a certain quarto version, we could detect that.

jjallaire commented 2 years ago

@cscheid Let's prototype this by setting the option/env var, then if it works well enough we can have crayon/cli check for being rendered under Quarto.

@gaborcsardi Prior to stable release (in June) we are always encouraging/requiring people to update to the latest Quarto, so if we get this in prior to then I don't think we need a version check.

gaborcsardi commented 2 years ago

Sounds good. Let me know if you want me to add quarto auto-detection to crayon/cli. That could handle the cases when a subprocess or another knitr run is started from within quarto/knitr.

jjallaire commented 2 years ago

Okay, will do once we have the prototype working. In terms of detecting Quarto, you can use these global knitr options: https://github.com/quarto-dev/quarto-cli/blob/main/src/resources/rmd/execute.R#L207-L210 (either quarto.version >= 1 or rmarkdown.version >= 3 will detect Quarto)

jjallaire commented 2 years ago

Hi there @mroavi, posting back here because I'm giving a talk on Quarto at JuliaCon next month and was hoping to find some good examples of using Julia w/ Quarto. If you are able to share any of your own (or are aware of others) please LMK over on this discussion thread: https://github.com/quarto-dev/quarto-cli/discussions/1261. Thanks!