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

Can seaborn.objects be display in quarto with knitr engine? #8336

Closed papayoun closed 9 months ago

papayoun commented 9 months ago

Bug description

I want to do an exercise on python graphs using quarto (I do it within Rstudio). I want my document to contain the correction, but this correction should be displayed according to a params option. Therefore, i used the knitr engine in quarto (as I don't know how to use params option with the jupyter engine). However, using this engine, my graphs that use seaborn.objects are not displayed (whereas using seaborn + matplotlib, it works). However, the seaborn.objects graphs are well displayed when the engine is set to jupyter (just change knitr to jupyter in the code below). I would like to fix that:

Is there any solution?

Steps to reproduce

---
title: "Minimal example"
format: html
engine: knitr
params:
  correction: false # false or true, if I want the code to appear
---

**Exercise 1:** Make the following graph with `seaborn`

```{python echo=params$correction}
# Solution 1 (only to appear when params.correction is true)
# Imports 
import matplotlib.pyplot as plt
import seaborn as sns
# Toy data
tips = sns.load_dataset("tips") 
# Toy graph
sns.scatterplot(data=tips, x="total_bill", y="tip")
plt.show()

Exercise 2: Make the following graph with seaborn.objects

import seaborn.objects as so

# Toy graph (should be the same as above)
so.Plot(data=tips, x="total_bill", y="tip").add(so.Dot())


### Expected behavior

Both graph should appear, and the code should appear only when `correction` is set to `true`

### Actual behavior

For the `seaborn.objects`, the graph does not appear. Instead the message  `<seaborn._core.plot.Plot object at 0x7f43be583a10>` is printed. If I set the `engine` to `jupyter`, the code appears even for `correction: false`.

### Your environment

- IDE Rstudio 2023.12.0+369
- OS Ubuntu 23.04 (kernel version Linux 6.2.0-33 generic

### Quarto check output

[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.1.1: OK
      Dart Sass version 1.55.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.3.450
      Path: /usr/lib/rstudio/resources/app/bin/quarto/bin

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

[✓] Checking Python 3 installation....OK
      Version: 3.11.7 (Conda)
      Path: /home/gloaguen/miniconda3/envs/STA2121/bin/python
      Jupyter: 5.7.1
      Kernels: python3

(\) Checking Jupyter engine render....0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........OK
      Version: 4.2.2
      Path: /usr/lib/R
      LibPaths:
        - /home/gloaguen/R/x86_64-pc-linux-gnu-library/4.2
        - /usr/local/lib/R/site-library
        - /usr/lib/R/site-library
        - /usr/lib/R/library
      knitr: 1.45
      rmarkdown: 2.25

[✓] Checking Knitr engine render......OK
papayoun commented 9 months ago

The problem was not a bug, but my ignorance of seaborn objects. adding .show() to the seaborn.objects graph code was sufficient. Sorry for that!

cderv commented 9 months ago

(as I don't know how to use params option with the jupyter engine)

Using Parameters in Quarto for Jupyter engine is described https://quarto.org/docs/computations/parameters.html#jupyter

However, as you are using parameter to adapt echo this is indeed only possible with historical knitr option syntax.

In Quarto, conditional use this syntax: https://quarto.org/docs/authoring/conditional.html and it does not control the computation cell options

when-meta can be a way to set some conditional based on a metadata value you would set in configuration or using a computed value.

We'll try to improve this in the future