英文:
Python output highlighting in Quarto
问题
Sure, here's the translated content:
继续讨论这个问题,关于在Quarto中突出显示代码输出:是否也可以使其适用于Python输出?
一个最小的工作示例:
---
title: "Output line highlighting"
format: revealjs
editor: visual
filters:
- output-line-highlight.lua
---
## 回归表格
在R中:
```R
#| class-output: highlight
#| output-line-numbers: "9,10,11,12"
LM <- lm(Sepal.Length ~ Sepal.Width, data = iris)
summary(LM)
回归表格
在Python中:
#| class-output: highlight
#| output-line-numbers: "12,13,14,15,16,17"
import pandas as pd
import statsmodels.api as sm
iris = sm.datasets.get_rdataset('iris').data
LM = sm.OLS(iris['Sepal.Length'], sm.add_constant(iris['Sepal.Width'])).fit()
LM.summary2()
渲染此文件显示Lua过滤器适用于R输出,但不适用于Python输出。
Please note that I've kept the code parts as-is without translation, as per your request.
<details>
<summary>英文:</summary>
Continuing on [this question](https://stackoverflow.com/q/74981820/8770170) about code output highlighting in Quarto: Is it also possible to make this work with Python output?
A minimal working example:
---
title: "Output line highlighting"
format: revealjs
editor: visual
filters:
- output-line-highlight.lua
---
## Regression Table
In R:
```{r}
#| class-output: highlight
#| output-line-numbers: "9,10,11,12"
LM <- lm(Sepal.Length ~ Sepal.Width, data = iris)
summary(LM)
```
## Regression Table
In Python:
```{python}
#| class-output: highlight
#| output-line-numbers: "12,13,14,15,16,17"
import pandas as pd
import statsmodels.api as sm
iris = sm.datasets.get_rdataset('iris').data
LM = sm.OLS(iris['Sepal.Length'], sm.add_constant(iris['Sepal.Width'])).fit()
LM.summary2()
```
Rendering this file shows the Lua filter works for the R output, but not for Python.
</details>
# 答案1
**得分**: 1
Python代码块不支持 `output-class` 选项,而应使用 `classes` 选项并使用类 `output-highlight`。至于 R 代码块,可以使用 `class-output: highlight` 或 `classes: output-highlight`。
已更新过滤器以支持 `classes: output-highlight`。
**output-line-highlight.lua**
```lua
function highlight(line_number)
local highlighter = {
CodeBlock = function(block)
if block.classes:includes('highlight') then
block.classes:insert('has-line-highlights')
block.attributes["code-line-numbers"] = line_number
return block
end
end
}
return highlighter
end
function add_highlight_class()
return {
CodeBlock = function(cb)
if not cb.classes:includes('highlight') then
cb.classes:insert('highlight')
end
return cb
end
}
end
function add_class_to_cb()
return {
Div = function(el)
if el.classes:includes('cell-output') then
return el:walk(add_highlight_class())
end
end
}
end
function highlight_output_div()
return {
Div = function(div)
if div.classes:includes('output-highlight') then
return div:walk(add_class_to_cb())
end
end
}
end
function add_output_lnum()
return {
Div = function(el)
if el.classes:includes('cell') then
line_number = tostring(el.attributes["output-line-numbers"])
return el:walk(highlight(line_number))
end
end
}
end
function Pandoc(doc)
if FORMAT == 'revealjs' then
local doc = doc:walk(highlight_output_div())
return doc:walk(add_output_lnum())
end
end
presentation.qmd
---
title: "Output line highlighting"
format: revealjs
filters:
- output-line-highlight.lua
---
## Regression Table
In R:
```{r}
#| class-output: highlight
#| output-line-numbers: "9,10,11,12"
LM <- lm(Sepal.Length ~ Sepal.Width, data = iris)
summary(LM)
Regression Table
In Python:
#| classes: output-highlight
#| output-line-numbers: "2,4"
test = [1, 2, 3, 4]
for i in test:
if i%2 == 0:
print("this line is highlighted")
else:
print("this line is not highlighted")
这是翻译后的内容,仅包括代码块和相关信息。
<details>
<summary>英文:</summary>
python code chunk doesn't support the chunk option `output-class`. Instead, use the option `classes` and use the class `output-highlight`.
And for r-code chunk, you may use either `class-output: highlight` or `classes: output-highlight`.
And I have updated the filter to support the `classes: output-highlight` accordingly.
**output-line-highlight.lua**
~~~ lua
function highlight(line_number)
local highlighter = {
CodeBlock = function(block)
if block.classes:includes('highlight') then
block.classes:insert('has-line-highlights')
block.attributes["code-line-numbers"] = line_number
return block
end
end
}
return highlighter
end
function add_highlight_class()
return {
CodeBlock = function(cb)
if not cb.classes:includes('highlight') then
cb.classes:insert('highlight')
end
return cb
end
}
end
function add_class_to_cb()
return {
Div = function(el)
if el.classes:includes('cell-output') then
return el:walk(add_highlight_class())
end
end
}
end
function highlight_output_div()
return {
Div = function(div)
if div.classes:includes('output-highlight') then
return div:walk(add_class_to_cb())
end
end
}
end
function add_output_lnum()
return {
Div = function(el)
if el.classes:includes('cell') then
line_number = tostring(el.attributes["output-line-numbers"])
return el:walk(highlight(line_number))
end
end
}
end
function Pandoc(doc)
if FORMAT == 'revealjs' then
local doc = doc:walk(highlight_output_div())
return doc:walk(add_output_lnum())
end
end
~~~
**presentation.qmd**
~~~
---
title: "Output line highlighting"
format: revealjs
filters:
- output-line-highlight.lua
---
## Regression Table
In R:
```{r}
#| class-output: highlight
#| output-line-numbers: "9,10,11,12"
LM <- lm(Sepal.Length ~ Sepal.Width, data = iris)
summary(LM)
Regression Table
In Python:
#| classes: output-highlight
#| output-line-numbers: "2,4"
test = [1, 2, 3, 4]
for i in test:
if i%2 == 0:
print("this line is highlighted")
else:
print("this line is not highlighted")
<hr>
[![highlighted lines][1]][1]
[1]: https://i.stack.imgur.com/Gt84Y.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论