quarto-dev / quarto-cli

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

Quarto execution output ignores cell magics #10480

Open skwde opened 1 month ago

skwde commented 1 month ago

Bug description

Output of cell magics is ignored (in some cases).

Note, in some cases the output is shown, e.g. when using memory_profiler's %memit. I guess it depends on how the magic is implemented.

Steps to reproduce

Use test.qmd with content

---
title: "Quarto Basics"
format: pdf
jupyter: python3
---

```{python}
#| echo: false
#| output: true
import re
print("While it outputs print statements, it doesn't output all cell magics")
%psource re.compile

and run via

```sh
quarto preview test.qmd --no-browser

Expected behavior

The output of the %psource magic should be displayed as it is in the jupyter notebook.

I.e. I expect to see

def compile(pattern, flags=0):
    "Compile a regular expression pattern, returning a Pattern object."
    return _compile(pattern, flags)

Actual behavior

%psource output is not displayed

Your environment

Quarto check output

# quarto check
Quarto 1.5.54
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.2.0: OK
      Dart Sass version 1.70.0: OK
      Deno version 1.41.0: OK
      Typst version 0.11.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.5.54
      Path: /opt/quarto/bin

[✓] Checking tools....................OK
      TinyTeX: v2024.07.03
      Chromium: (not installed)

[✓] Checking LaTeX....................OK
      Using: TinyTex
      Path: /root/.TinyTeX/bin/x86_64-linux
      Version: 2024

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK
      Version: 3.12.4 (Conda)
      Path: /opt/conda/bin/python
      Jupyter: 5.7.2
      Kernels: python3

[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........(None)

      Unable to locate an installed version of R.
      Install R from https://cloud.r-project.org/
cscheid commented 4 weeks ago

Unfortunately, that magic doesn't emit anything: see the keep-ipynb: true output. So there's nothing Quarto can do about it.

skwde commented 1 week ago

@cscheid, not quite sure I get what the problem is here.

In any case, using %psource as above in ipython or in a jupyter-notebook shows the function definition.

cderv commented 1 week ago

I believe this is the same issue as

And this relates to nbconvert execution of notebook having no output for the cell magic.

If you do the following

To mimic this you can do

quarto convert index.qmd
jupyter nbconvert --to notebook --execute index.ipynb

you can reproduce the problem.

So possibly a nbconvert issue.