Skip to content

Commit

Permalink
Merge pull request #1256 from cscheid/feature/_repr_markdown_
Browse files Browse the repository at this point in the history
add support for `_repr_markdown_` IPython methods
  • Loading branch information
t-kalinowski committed Aug 9, 2022
2 parents 7d37192 + 0580236 commit f77d611
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Expand Up @@ -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"))
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Expand Up @@ -29,6 +29,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

Expand Down
6 changes: 6 additions & 0 deletions R/knitr-engine.R
Expand Up @@ -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())
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/resources/eng-reticulate-example.Rmd
Expand Up @@ -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"
))
```

0 comments on commit f77d611

Please sign in to comment.