Skip to content

Commit

Permalink
Merge pull request #1423 from dfalbel/jupyter-compat-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kalinowski authored Jul 25, 2023
2 parents 9639bd3 + 70d32f9 commit a003118
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 6 deletions.
16 changes: 10 additions & 6 deletions R/knitr-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,26 @@ eng_python <- function(options) {
# clear the last value object (so we can tell if it was updated)
py_compile_eval("'__reticulate_placeholder__'")

# use trailing semicolon to suppress output of return value
suppress <- grepl(";\\s*$", snippet) || (jupyter_compat & !last_range)
compile_mode <- if (suppress) "exec" else "single"

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

# 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 <- ""
if (suppress)
captured <- captured_stdout

# emit outputs if we have any
has_outputs <-
Expand Down
81 changes: 81 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,84 @@
bt$print("Hello world")

## Hello world

## Print statements should always be captured

In `jupyter_compat = FALSE`: we should only see the output of both
expressions.

print(1)

## 1

print(2)

## 2

In `jupyter_compat = TRUE`: we should only see the output of both
expressions.

print(1)

## 1

print(2)

## 2

## Only outputs of last expressions are captured in jupyter compat

In `jupyter_compat = FALSE`: we should only see the output of both
expressions.

1 + 0

## 1

1 + 1

## 2

`jupyter_compat = TRUE`: we should only see the output of the last
expression.

1 + 0
1 + 1

## 2

## One can disable outputs by using `;`

In `jupyter_compat = FALSE`: we should only see the print statement
output

print("hello");

## hello

1 + 0;
1 + 1;

`jupyter_compat = TRUE`: we should only see the print statement output

print("hello");

## hello

1 + 0;
1 + 1;

## `jupyter_compat` works with interleaved expressions

print('first_stdout')

## first_stdout

'first_expression'
print('second_stdout')

## second_stdout

'final_expression'

## 'final_expression'
81 changes: 81 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,84 @@
bt$print("Hello world")

## Hello world

## Print statements should always be captured

In `jupyter_compat = FALSE`: we should only see the output of both
expressions.

print(1)

## 1

print(2)

## 2

In `jupyter_compat = TRUE`: we should only see the output of both
expressions.

print(1)

## 1

print(2)

## 2

## Only outputs of last expressions are captured in jupyter compat

In `jupyter_compat = FALSE`: we should only see the output of both
expressions.

1 + 0

## 1

1 + 1

## 2

`jupyter_compat = TRUE`: we should only see the output of the last
expression.

1 + 0
1 + 1

## 2

## One can disable outputs by using `;`

In `jupyter_compat = FALSE`: we should only see the print statement
output

print("hello");

## hello

1 + 0;
1 + 1;

`jupyter_compat = TRUE`: we should only see the print statement output

print("hello");

## hello

1 + 0;
1 + 1;

## `jupyter_compat` works with interleaved expressions

print('first_stdout')

## first_stdout

'first_expression'
print('second_stdout')

## second_stdout

'final_expression'

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

## Print statements should always be captured

In `jupyter_compat = FALSE`: we should only see the output of both expressions.

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

In `jupyter_compat = TRUE`: we should only see the output of both expressions.

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

## Only outputs of last expressions are captured in jupyter compat

In `jupyter_compat = FALSE`: we should only see the output of both expressions.

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

`jupyter_compat = TRUE`: we should only see the output of the last expression.

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

## One can disable outputs by using `;`

In `jupyter_compat = FALSE`: we should only see the print statement output

```{python}
print("hello");
1 + 0;
1 + 1;
```

`jupyter_compat = TRUE`: we should only see the print statement output

```{python, jupyter_compat = TRUE}
print("hello");
1 + 0;
1 + 1;
```

## `jupyter_compat` works with interleaved expressions

```{python, jupyter_compat=TRUE}
print('first_stdout')
'first_expression'
print('second_stdout')
'final_expression'
```


0 comments on commit a003118

Please sign in to comment.