Python输出在Quarto中的突出显示

huangapple go评论75阅读模式
英文:

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: &quot;Output line highlighting&quot;
    format: revealjs
    editor: visual
    filters:
      - output-line-highlight.lua
    ---
    
    ## Regression Table 
    
    In R:
    
    ```{r}
    #| class-output: highlight
    #| output-line-numbers: &quot;9,10,11,12&quot;
    LM &lt;- lm(Sepal.Length ~ Sepal.Width, data = iris)
    summary(LM)
    ```
    
    ## Regression Table
    
    In Python:
    
    ```{python}
    #| class-output: highlight
    #| output-line-numbers: &quot;12,13,14,15,16,17&quot;
    import pandas as pd
    import statsmodels.api as sm
    
    iris = sm.datasets.get_rdataset(&#39;iris&#39;).data
    LM = sm.OLS(iris[&#39;Sepal.Length&#39;], sm.add_constant(iris[&#39;Sepal.Width&#39;])).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")

Python输出在Quarto中的突出显示


这是翻译后的内容,仅包括代码块和相关信息。

<details>
<summary>英文:</summary>

python code chunk doesn&#39;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(&#39;highlight&#39;) then
        block.classes:insert(&#39;has-line-highlights&#39;)
        block.attributes[&quot;code-line-numbers&quot;] = line_number
        return block
      end
  end
  }
  return highlighter
end

function add_highlight_class()
  return {
    CodeBlock = function(cb)
      if not cb.classes:includes(&#39;highlight&#39;) then
        cb.classes:insert(&#39;highlight&#39;)
      end
      return cb
    end
  }
end


function add_class_to_cb()
  return {
    Div = function(el)
      if el.classes:includes(&#39;cell-output&#39;) then
        return el:walk(add_highlight_class())
      end
    end
  }
end

function highlight_output_div()
  return {
    Div = function(div)
      if div.classes:includes(&#39;output-highlight&#39;) then
        return div:walk(add_class_to_cb())
      end
    end  
  }
end

function add_output_lnum()
  return {
    Div = function(el)
      if el.classes:includes(&#39;cell&#39;) then
        line_number = tostring(el.attributes[&quot;output-line-numbers&quot;])
        return el:walk(highlight(line_number))
      end
    end
  }
end

function Pandoc(doc)
  if FORMAT == &#39;revealjs&#39; then
    local doc = doc:walk(highlight_output_div())
    return doc:walk(add_output_lnum())
  end
end
~~~

**presentation.qmd**

~~~
---
title: &quot;Output line highlighting&quot;
format: revealjs
filters:
  - output-line-highlight.lua
---

## Regression Table 

In R:

```{r}
#| class-output: highlight
#| output-line-numbers: &quot;9,10,11,12&quot;
LM &lt;- lm(Sepal.Length ~ Sepal.Width, data = iris)
summary(LM)

Regression Table

In Python:

#| classes: output-highlight
#| output-line-numbers: &quot;2,4&quot;

test = [1, 2, 3, 4]

for i in test:
  if i%2 == 0:
    print(&quot;this line is highlighted&quot;)
  else:
    print(&quot;this line is not highlighted&quot;)

&lt;hr&gt;

[![highlighted lines][1]][1]


  [1]: https://i.stack.imgur.com/Gt84Y.png

</details>



huangapple
  • 本文由 发表于 2023年5月26日 16:06:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定