如何避免Quarto GFM报告中Pandas表格上方出现HTML代码
解决Quarto GFM报告中Pandas表格上方出现HTML代码的问题
方法一:在Python代码中指定输出为纯Markdown表格
直接将DataFrame转换为Markdown格式输出,绕过Quarto默认的渲染逻辑,避免混入HTML代码:
import numpy as np import pandas as pd N = int(5e3) TRIALS = int(1) df = pd.DataFrame( { "A": np.random.binomial(TRIALS, 0.65, N), "B": np.random.binomial(TRIALS, 0.65, N), "C": np.random.binomial(TRIALS, 0.65, N), "D": np.random.binomial(TRIALS, 0.67, N) } ) print(df.to_markdown(index=False))
方法二:修改文档全局配置强制Markdown输出
在YAML头部添加执行配置,让Python代码块的输出统一使用Markdown格式:
--- title: 'HTML junk' author: 'Joseph Powers' date: 2024-03-14 format: gfm execute: python: options: display: formatter: markdown ---
方法三:配合Quarto代码块输出选项
给代码块添加#| output: asis标记,确保输出的Markdown被GitHub正确识别为表格:
#| output: asis import numpy as np import pandas as pd N = int(5e3) TRIALS = int(1) df = pd.DataFrame( { "A": np.random.binomial(TRIALS, 0.65, N), "B": np.random.binomial(TRIALS, 0.65, N), "C": np.random.binomial(TRIALS, 0.65, N), "D": np.random.binomial(TRIALS, 0.67, N) } ) print(df.to_markdown(index=False))
以上三种方法都能消除表格上方的多余HTML代码,让GitHub上的GFM报告显示正常的Markdown表格。
内容的提问来源于stack exchange,提问作者Joe
相关产品推荐
相关产品推荐

