Skip to content

Commit

Permalink
Still include the stdout output captured before the autoprint mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
dfalbel committed Jul 25, 2023
1 parent e869c27 commit b401fcb
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
6 changes: 3 additions & 3 deletions R/knitr-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,21 @@ eng_python <- function(options) {
py_compile_eval("'__reticulate_placeholder__'")

# run code and capture output
captured <- if (capture_errors)
captured_stdout <- if (capture_errors)
tryCatch(py_compile_eval(snippet, 'single'), error = identity)
else
py_compile_eval(snippet, 'single')

# handle matplotlib plots and other special output
captured <- eng_python_autoprint(
captured = captured,
captured = captured_stdout,
options = options
)

# A trailing ';' suppresses output.
# In jupyter mode, only the last expression in a chunk has repr() output.
if (grepl(";\\s*$", snippet) | (jupyter_compat & !last_range))
captured <- ""
captured <- captured_stdout

# emit outputs if we have any
has_outputs <-
Expand Down
39 changes: 39 additions & 0 deletions tests/testthat/_snaps/python-knitr-engine/knitr-print.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,42 @@
bt$print("Hello world")

## Hello world

Both in `jupyter_compat = TRUE` and `jupyter_compat = FALSE` we should
see the results, because a `print` was called:

print(1)

## 1

print(2)

## 2

print(1)

## 1

print(2)

## 2

For the `jupyter_compat = FALSE` mode we should see the output of both
expressions. In `jupyter_compat`, we should only see the output for the
last expression.

1 + 0

## 1

1 + 1

## 2

1 + 0

## 1

1 + 1

## 2
33 changes: 33 additions & 0 deletions tests/testthat/_snaps/python-knitr-engine/knitr-print2.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,36 @@
bt$print("Hello world")

## Hello world

Both in `jupyter_compat = TRUE` and `jupyter_compat = FALSE` we should
see the results, because a `print` was called:

print(1)

## 1

print(2)

## 2

print(1)
print(2)

## 2

For the `jupyter_compat = FALSE` mode we should see the output of both
expressions. In `jupyter_compat`, we should only see the output for the
last expression.

1 + 0

## 1

1 + 1

## 2

1 + 0
1 + 1

## 2
25 changes: 25 additions & 0 deletions tests/testthat/resources/knitr-print.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,28 @@ bt <- reticulate::import_builtins()
bt$print("Hello world")
```

Both in `jupyter_compat = TRUE` and `jupyter_compat = FALSE` we should see the
results, because a `print` was called:

```{python}
print(1)
print(2)
```

```{python, jupyter_compat = TRUE}
print(1)
print(2)
```

For the `jupyter_compat = FALSE` mode we should see the output of both expressions.
In `jupyter_compat`, we should only see the output for the last expression.

```{python}
1 + 0
1 + 1
```

```{python, jupyter_compat = TRUE}
1 + 0
1 + 1
```

0 comments on commit b401fcb

Please sign in to comment.