Running Python code in Markdown

There is a lot of information about using Python code in a Markdown document. But all this is like a demonstration of Python snippets, rather than creating good documents.

Can I not combine Python and Markdown in one document, as you can with R and Markdown?

MWE:

Output some text from Python in **Markdown**:
```python
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
print(clf.predict_proba(iris.data[:1, :]))
```

Compilation: markdown_py markdown.txt

<p>Output some text from Python in <strong>Markdown</strong>:
<code>python
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
clf.predict_proba(iris.data[:1, :])</code></p>

It displays the code (cool), but does not actually run it.

Can't you run Python code in Markdown? If not, what are the alternatives?

(Using the python-markdown package from Ubuntu.)

+4
source share
2 answers

Well, I just found a solution:

Use pieces like:

<<engine='python', engine.path='python3'>>=
# python code
@
  • engine.path python, Linux - python2. , Python 2.
  • echo=FALSE, results='asis', .

:

<<r setup, include=FALSE>>=
knitr::opts_chunk$set(echo=FALSE, engine='whathaveyou', ...)
@

markdown.Rmd R knitr . Python python.

R: rmarkdown::render('markdown.Rmd','output.html')

RStudio.

: , -, Pweave: . , .

+2

2018 CRAN- reticulate.

, Python. "Python" R "Python". "Python" R, R. "Python" >= 2.7.

0

Source: https://habr.com/ru/post/1611901/


All Articles