Skip to content

Commit

Permalink
Better handle echo:false and include: false
Browse files Browse the repository at this point in the history
Closes #62.
  • Loading branch information
georgestagg committed Sep 17, 2024
1 parent 3d4a251 commit 3f8a4e5
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 17 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

* Ensure variables defined at the top level of the the grading environment are available as globals when executing grading algorithms with Pyodide (#63).

* Better handle `echo: false` and `include: false` cell options (#62).

# Quarto Live 0.1.1

Initial release.
12 changes: 11 additions & 1 deletion _extensions/live/_knitr.qmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
```{r echo=FALSE}
# Setup knitr for handling {webr} and {pyodide} blocks
# TODO: With quarto-dev/quarto-cli#10169, we can implement this in a filter
# We'll handle `include: false` in Lua, always include cell in knitr output
knitr::opts_hooks$set(include = function(options) {
if (options$engine == "webr" || options$engine == "pyodide" ) {
options$include <- TRUE
}
options
})
# Passthrough engine for webr
knitr::knit_engines$set(webr = function(options) {
knitr:::one_string(c(
"```{webr}",
Expand All @@ -10,7 +20,7 @@ knitr::knit_engines$set(webr = function(options) {
))
})
# Pyodide
# Passthrough engine for pyodide
knitr::knit_engines$set(pyodide = function(options) {
knitr:::one_string(c(
"```{pyodide}",
Expand Down
27 changes: 17 additions & 10 deletions _extensions/live/live.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ function ParseBlock(block, engine)
end
end

-- When echo: false: disable the editor
if (attr.echo == false) then
attr.edit = false
end

-- When `include: false`: disable the editor, source block echo, and output
if (attr.include == false) then
attr.edit = false
attr.echo = false
attr.output = false
end

-- If we're not executing anything, there's no point showing an editor
if (attr.edit == nil) then
attr.edit = attr.eval
end

return {
code = code,
attr = attr
Expand Down Expand Up @@ -238,11 +255,6 @@ function PyodideCodeBlock(code)
block_input = input,
}

-- If we're not executing anything, there's no point showing an editor
if (block.attr.edit == nil) then
block.attr.edit = block.attr.eval
end

-- Render appropriate OJS for the type of client-side block we're working with
local ojs_source = nil
if (block.attr.exercise) then
Expand Down Expand Up @@ -356,11 +368,6 @@ function WebRCodeBlock(code)
block_input = input,
}

-- If we're not executing anything, there's no point showing an editor
if (block.attr.edit == nil) then
block.attr.edit = block.attr.eval
end

-- Render appropriate OJS for the type of client-side block we're working with
local ojs_source = nil
if (block.attr.exercise) then
Expand Down
4 changes: 2 additions & 2 deletions _extensions/live/resources/live-runtime.js

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions docs/getting_started/editor.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,39 @@ n_sd <- 5
# Type "n_" to see context aware suggestions
```

## Execute without output

Setting the cell option `include: false` will execute code in the WebAssembly engine without adding its source code or any output to the resulting document. The option is equivalent to setting `echo: false`, `output: false`.

#### Source

````markdown
```{{webr}}
#| include: false
123
456
include_false_n <- 789
```

```{{webr}}
print(include_false_n)
```
````

#### Output

```{webr}
#| include: false
123
456
include_false_n <- 789
```

```{webr}
print(include_false_n)
```


## Storing and recalling code

Initial editor contents are stored and can be recalled at any time by clicking the "Start Over" button. If required, the "Start Over" button can be disabled by setting the `startover: false` cell option.
Expand Down
3 changes: 1 addition & 2 deletions live-runtime/src/evaluate-pyodide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ export class PyodideEvaluator implements ExerciseEvaluator {
}

async evaluate(code: string, envLabel: EnvLabel, options: EvaluateOptions = this.options) {
// Early return if code is undefined, null, or if we're not evaluating
if (code == null || !options.include) {
if (code == null) {
return null;
}

Expand Down
3 changes: 1 addition & 2 deletions live-runtime/src/evaluate-webr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ export class WebREvaluator implements ExerciseEvaluator {
}

async evaluate(code: string, envLabel: EnvLabel, options: EvaluateOptions = this.options) {
// Early return if code is undefined, null, or if we're not evaluating
if (code == null || !options.include) {
if (code == null) {
return null;
}

Expand Down

0 comments on commit 3f8a4e5

Please sign in to comment.