Python输出在Quarto中的突出显示

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

Python output highlighting in Quarto

问题

Sure, here's the translated content:

继续讨论这个问题,关于在Quarto中突出显示代码输出:是否也可以使其适用于Python输出?

一个最小的工作示例:

  1. ---
  2. title: "Output line highlighting"
  3. format: revealjs
  4. editor: visual
  5. filters:
  6. - output-line-highlight.lua
  7. ---
  8. ## 回归表格
  9. R中:
  10. ```R
  11. #| class-output: highlight
  12. #| output-line-numbers: "9,10,11,12"
  13. LM <- lm(Sepal.Length ~ Sepal.Width, data = iris)
  14. summary(LM)

回归表格

在Python中:

  1. #| class-output: highlight
  2. #| output-line-numbers: "12,13,14,15,16,17"
  3. import pandas as pd
  4. import statsmodels.api as sm
  5. iris = sm.datasets.get_rdataset('iris').data
  6. LM = sm.OLS(iris['Sepal.Length'], sm.add_constant(iris['Sepal.Width'])).fit()
  7. LM.summary2()

渲染此文件显示Lua过滤器适用于R输出,但不适用于Python输出。

  1. Please note that I've kept the code parts as-is without translation, as per your request.
  2. <details>
  3. <summary>英文:</summary>
  4. 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?
  5. A minimal working example:
  6. ---
  7. title: &quot;Output line highlighting&quot;
  8. format: revealjs
  9. editor: visual
  10. filters:
  11. - output-line-highlight.lua
  12. ---
  13. ## Regression Table
  14. In R:
  15. ```{r}
  16. #| class-output: highlight
  17. #| output-line-numbers: &quot;9,10,11,12&quot;
  18. LM &lt;- lm(Sepal.Length ~ Sepal.Width, data = iris)
  19. summary(LM)
  20. ```
  21. ## Regression Table
  22. In Python:
  23. ```{python}
  24. #| class-output: highlight
  25. #| output-line-numbers: &quot;12,13,14,15,16,17&quot;
  26. import pandas as pd
  27. import statsmodels.api as sm
  28. iris = sm.datasets.get_rdataset(&#39;iris&#39;).data
  29. LM = sm.OLS(iris[&#39;Sepal.Length&#39;], sm.add_constant(iris[&#39;Sepal.Width&#39;])).fit()
  30. LM.summary2()
  31. ```
  32. Rendering this file shows the Lua filter works for the R output, but not for Python.
  33. </details>
  34. # 答案1
  35. **得分**: 1
  36. Python代码块不支持 `output-class` 选项,而应使用 `classes` 选项并使用类 `output-highlight`。至于 R 代码块,可以使用 `class-output: highlight` 或 `classes: output-highlight`。
  37. 已更新过滤器以支持 `classes: output-highlight`。
  38. **output-line-highlight.lua**
  39. ```lua
  40. function highlight(line_number)
  41. local highlighter = {
  42. CodeBlock = function(block)
  43. if block.classes:includes('highlight') then
  44. block.classes:insert('has-line-highlights')
  45. block.attributes["code-line-numbers"] = line_number
  46. return block
  47. end
  48. end
  49. }
  50. return highlighter
  51. end
  52. function add_highlight_class()
  53. return {
  54. CodeBlock = function(cb)
  55. if not cb.classes:includes('highlight') then
  56. cb.classes:insert('highlight')
  57. end
  58. return cb
  59. end
  60. }
  61. end
  62. function add_class_to_cb()
  63. return {
  64. Div = function(el)
  65. if el.classes:includes('cell-output') then
  66. return el:walk(add_highlight_class())
  67. end
  68. end
  69. }
  70. end
  71. function highlight_output_div()
  72. return {
  73. Div = function(div)
  74. if div.classes:includes('output-highlight') then
  75. return div:walk(add_class_to_cb())
  76. end
  77. end
  78. }
  79. end
  80. function add_output_lnum()
  81. return {
  82. Div = function(el)
  83. if el.classes:includes('cell') then
  84. line_number = tostring(el.attributes["output-line-numbers"])
  85. return el:walk(highlight(line_number))
  86. end
  87. end
  88. }
  89. end
  90. function Pandoc(doc)
  91. if FORMAT == 'revealjs' then
  92. local doc = doc:walk(highlight_output_div())
  93. return doc:walk(add_output_lnum())
  94. end
  95. end

presentation.qmd

  1. ---
  2. title: "Output line highlighting"
  3. format: revealjs
  4. filters:
  5. - output-line-highlight.lua
  6. ---
  7. ## Regression Table
  8. In R:
  9. ```{r}
  10. #| class-output: highlight
  11. #| output-line-numbers: "9,10,11,12"
  12. LM <- lm(Sepal.Length ~ Sepal.Width, data = iris)
  13. summary(LM)

Regression Table

In Python:

  1. #| classes: output-highlight
  2. #| output-line-numbers: "2,4"
  3. test = [1, 2, 3, 4]
  4. for i in test:
  5. if i%2 == 0:
  6. print("this line is highlighted")
  7. else:
  8. print("this line is not highlighted")

Python输出在Quarto中的突出显示

  1. 这是翻译后的内容,仅包括代码块和相关信息。
  2. <details>
  3. <summary>英文:</summary>
  4. python code chunk doesn&#39;t support the chunk option `output-class`. Instead, use the option `classes` and use the class `output-highlight`.
  5. And for r-code chunk, you may use either `class-output: highlight` or `classes: output-highlight`.
  6. And I have updated the filter to support the `classes: output-highlight` accordingly.
  7. **output-line-highlight.lua**
  8. ~~~ lua
  9. function highlight(line_number)
  10. local highlighter = {
  11. CodeBlock = function(block)
  12. if block.classes:includes(&#39;highlight&#39;) then
  13. block.classes:insert(&#39;has-line-highlights&#39;)
  14. block.attributes[&quot;code-line-numbers&quot;] = line_number
  15. return block
  16. end
  17. end
  18. }
  19. return highlighter
  20. end
  21. function add_highlight_class()
  22. return {
  23. CodeBlock = function(cb)
  24. if not cb.classes:includes(&#39;highlight&#39;) then
  25. cb.classes:insert(&#39;highlight&#39;)
  26. end
  27. return cb
  28. end
  29. }
  30. end
  31. function add_class_to_cb()
  32. return {
  33. Div = function(el)
  34. if el.classes:includes(&#39;cell-output&#39;) then
  35. return el:walk(add_highlight_class())
  36. end
  37. end
  38. }
  39. end
  40. function highlight_output_div()
  41. return {
  42. Div = function(div)
  43. if div.classes:includes(&#39;output-highlight&#39;) then
  44. return div:walk(add_class_to_cb())
  45. end
  46. end
  47. }
  48. end
  49. function add_output_lnum()
  50. return {
  51. Div = function(el)
  52. if el.classes:includes(&#39;cell&#39;) then
  53. line_number = tostring(el.attributes[&quot;output-line-numbers&quot;])
  54. return el:walk(highlight(line_number))
  55. end
  56. end
  57. }
  58. end
  59. function Pandoc(doc)
  60. if FORMAT == &#39;revealjs&#39; then
  61. local doc = doc:walk(highlight_output_div())
  62. return doc:walk(add_output_lnum())
  63. end
  64. end
  65. ~~~
  66. **presentation.qmd**
  67. ~~~
  68. ---
  69. title: &quot;Output line highlighting&quot;
  70. format: revealjs
  71. filters:
  72. - output-line-highlight.lua
  73. ---
  74. ## Regression Table
  75. In R:
  76. ```{r}
  77. #| class-output: highlight
  78. #| output-line-numbers: &quot;9,10,11,12&quot;
  79. LM &lt;- lm(Sepal.Length ~ Sepal.Width, data = iris)
  80. summary(LM)

Regression Table

In Python:

  1. #| classes: output-highlight
  2. #| output-line-numbers: &quot;2,4&quot;
  3. test = [1, 2, 3, 4]
  4. for i in test:
  5. if i%2 == 0:
  6. print(&quot;this line is highlighted&quot;)
  7. else:
  8. print(&quot;this line is not highlighted&quot;)
  1. &lt;hr&gt;
  2. [![highlighted lines][1]][1]
  3. [1]: https://i.stack.imgur.com/Gt84Y.png
  4. </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:

确定