10

There is a lot of information on using Python code in a Markdown document. But it all seems to be about demonstrating Python snippets, not creating good looking documents.

Can I not combine Python and Markdown in a single document, like 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, :]))
```

Compiling this: 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 you not run Python code in Markdown? If not, what alternatives are there?

(Using the python-markdown package from Ubuntu.)

4
  • 2
    Markdown is only a text-markup format. There may or may not be libraries which also execute the code embedded in such documents, but by default that's way outside the scope of what Markdown is. Sphinx using ReST markup can execute such code IIRC; I have no idea whether the same thing exists for Markdown. Commented Oct 16, 2015 at 9:02
  • Thank you. But ReST does not do what I want. It seems to be an alternative to docbook. It's cool to generate documentation, but not to generate documents and presentations. I want to run python code within markdown, like you can do with R. (See answer below.) Commented Oct 16, 2015 at 9:12
  • 2
    I think you want to use Jupyter/IPython Notebook: jupyter.org, or Beaker if you want cross-language support: beakernotebook.com Commented Oct 16, 2015 at 9:27
  • 1
    Those are very cool platforms. But seem a bit overkill for my use case. I would prefer something simple like in R. You create a R+markdown document (file.Rmd), it runs the R part of the document and generates a pure markdown document (file.md) which you can then create slides with using pandoc or slidify or whatever. I was confused because I thought this was a process common to all languages. Anyhow, I found Pweave does exactly what I want. Commented Oct 17, 2015 at 10:42

1 Answer 1

5

Well, I just found a solution:

Use chunks as:

<<engine='python', engine.path='python3'>>=
# python code
@
  • The engine.path by default uses the python executable, which in most Linux systems still is python2. You can ommit it if you want Python 2.
  • Do not forget to pass echo=FALSE if you want to ommit code printout, and results='asis' so that it does not try to escape the output.

You can use the following chunk at the beggining of the document to set the defaults:

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

Save the file as markdown.Rmd, and use R with knitr to compile it. It will run the Python code using python.

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

Or just use RStudio.

Addendum: A native solution is apparently Pweave: it works with latex and markdown. I have not tried it yet though.

Sign up to request clarification or add additional context in comments.

1 Comment

So do you use R markdown syntax with python code in the blocks, like ---{r} <some_python_code> --- ? (three accent's aren't showing up in the comment for some reason so I replaced with three dashes)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.