From 9f6a3d67a52f45572f4a6e87885621353883d3c9 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Mon, 8 Aug 2022 08:06:53 -0700 Subject: [PATCH 1/3] add support for _repr_markdown_ IPython methods --- R/knitr-engine.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/knitr-engine.R b/R/knitr-engine.R index 95c281c92..7aacacd3b 100644 --- a/R/knitr-engine.R +++ b/R/knitr-engine.R @@ -618,6 +618,12 @@ eng_python_autoprint <- function(captured, options, autoshow) { return("") + } else if (py_has_method(value, "_repr_markdown_")) { + + data <- as_r_value(value$`_repr_markdown_`()) + .engine_context$pending_plots$push(knitr::asis_output(data)) + return("") + } else if (py_has_method(value, "to_html")) { data <- as_r_value(value$to_html()) From 65c8b9665489bca5f3b93512892e144cfe04de5a Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Mon, 8 Aug 2022 11:08:44 -0700 Subject: [PATCH 2/3] add news entry --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index f4c0305f4..0d67643f0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -25,6 +25,9 @@ - Fixed issue where `py_last_error()` would return unconverted Python objects (#1233) +- The Knitr engine now supports printing Python objects with + `_repr_markdown_` methods. (via quarto, + https://github.com/quarto-dev/quarto-cli/issues/1501) # reticulate 1.25 From 058023620f2d704f66428c27f496cbf022dc97d1 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Mon, 8 Aug 2022 11:13:19 -0700 Subject: [PATCH 3/3] add minimal test and dependencies --- .github/workflows/R-CMD-check.yaml | 4 ++-- .../resources/eng-reticulate-example.Rmd | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 1c9ae3396..f555269e0 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -65,8 +65,8 @@ jobs: remotes::install_local() reticulate::virtualenv_create("r-reticulate", Sys.which("python")) reticulate::virtualenv_install("r-reticulate", - c("docutils", "pandas", "scipy", "matplotlib", - "plotly", "psutil", "kaleido")) + c("docutils", "pandas", "scipy", "matplotlib", "ipython", + "tabulate", "plotly", "psutil", "kaleido")) python <- reticulate::virtualenv_python("r-reticulate") writeLines(sprintf("RETICULATE_PYTHON=%s", python), Sys.getenv("GITHUB_ENV")) diff --git a/tests/testthat/resources/eng-reticulate-example.Rmd b/tests/testthat/resources/eng-reticulate-example.Rmd index 3d30622e8..ce647f4cd 100644 --- a/tests/testthat/resources/eng-reticulate-example.Rmd +++ b/tests/testthat/resources/eng-reticulate-example.Rmd @@ -170,3 +170,20 @@ import pandas as pd pt = pd.DataFrame() type(pt) ``` + +`_repr_markdown_()` methods are supported +(https://github.com/quarto-dev/quarto-cli/issues/1501): + +```{python} +from IPython.display import Markdown +from tabulate import tabulate +table = [["Sun",696000,1989100000], + ["Earth",6371,5973.6], + ["Moon",1737,73.5], + ["Mars",3390,641.85]] +Markdown(tabulate( + table, + headers=["Planet","R (km)", "mass (x 10^29 kg)"], + tablefmt="pipe" +)) +```