【问题标题】:Display Python >>> prompt in code chunk output在代码块输出中显示 Python >>> 提示
【发布时间】:2017-08-31 21:04:33
【问题描述】:

我们正在使用 rmarkdown 制作一本关于 Python 编程的书。 eval = TRUEecho = TRUE 等代码块选项可用于在输出中显示格式良好的代码、运行它并在文本中显示结果。

不过,有用的是有时能够在代码前面加上 Python >>> 提示符,以指示在交互式终端中键入的代码。

有没有办法插入>>> 提示符作为前缀?

【问题讨论】:

    标签: r rstudio knitr r-markdown


    【解决方案1】:

    您可以在之前的代码块中设置options(prompt = '>>> ') 后使用knitr chunk option prompt = TRUE,例如

    ```{r include=FALSE}
    options(prompt = '>>> ')
    ```
    
    
    ```{python, prompt=TRUE}
    print("hello world")
    ```
    

    添加options(continue = '>>> ') 将为多行添加>>> 提示前缀。

    【讨论】:

    • 谢谢,很高兴能直接从马嘴里得到小费。不幸的是,它在 Python 中似乎不像在 R 中那样好用,就像在这份报告中一样:github.com/yihui/knitr/issues/1274
    • 您可能还需要设置options(continue = '>>> ')(我不知道 Python shell 使用哪些字符来继续执行命令)。
    • 很棒的@Yihui,continue 选项确实可以解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 2022-08-18
    • 2018-05-21
    • 1970-01-01
    • 2015-12-28
    相关资源
    最近更新 更多