posit-dev / great-tables

Make awesome display tables using Python.
https://posit-dev.github.io/great-tables/
MIT License
1.43k stars 48 forks source link

Table output cannot be suppressed in Quarto when R code chunk present #291

Closed AlbertRapp closed 2 months ago

AlbertRapp commented 2 months ago

Prework

Description

Hi there :wave:

This is probably not a highly relevant edge case. But in Quarto the option output: false doesn't work with gt tables if there are both R and Python code chunks in the Quarto file. For other Python outputs, output suppression seems to be unaffected.

Reproducible example


```{r}
#| output: false
# R code chunk
1 + 1
#| output: false
# This PYTHON output is suppressed
1 + 1
#| output: false
import great_tables as gt

# This PYTHON output is ONLY suppressed if 
# the above R code chunk is not there
gt.GT(gt.data.gtcars)


## Expected result

No table in the output

## Development environment

- Operating System: Ubuntu
- great-tables==0.5.0
machow commented 2 months ago

Shoot, I wonder if this is a reticulate, or a quarto bug? It looks like a class with the IPython display hooks (e.g. _repr_html_()) gets displayed regardless of output

Here's the qmd I used to test:

```{r}
#| output: false
# R code chunk
1 + 1
#| output: false
# This PYTHON output is suppressed
1 + 1
#| output: false

class MyClass:
    def _repr_html_(self):
        return "<p>uh-oh</p>"

MyClass()
machow commented 2 months ago

I've opened an issue on quarto--thanks for such a helpful qmd reproducing the issue!

https://github.com/quarto-dev/quarto-cli/issues/9389

machow commented 2 months ago

Just to complete the loop -- I don't think there's currently a simple option for suppressing objects with a _repr_html_() method, for the case where quarto punts to knitr (which is what it does when there are R and python code chunks).

However, one quick solution is to put a semi colon to suppress the output altogether:

```{python}

class MyClass:
    def _repr_html_(self):
        return "<p>uh-oh</p>"

MyClass();               # <---- NOTE THE SEMI COLON