开发者

Sweave for python

I've recently started using Sweave* for creating reports of analyses run with R, and am now looking to do the same with my python scripts.

I've found references to embedding python in Sweave docs, but that seems like a bit of a hack. Has anyone worked out a better solution, or is there an equival开发者_运维知识库ent for python I'm not aware of?

* Sweave is a tool that allows to embed the R code for complete data analyses in latex documents


I have written a Python implementation of Sweave called Pweave that implements basic functionality and some options of Sweave for Python code embedded in reST or Latex document. You can get it here: http://mpastell.com/pweave and see the original blog post here: http://mpastell.com/2010/03/03/pweave-sweave-for-python/


Some suggestions:

  1. I have been using Pweave for several years now, and it is very similar to Sweave. Highly recommended.
  2. The most popular tool for embedded reports in python at this stage is Jupyter notebooks, which allow you to embed markdown, and they are quite useful although I personally still like writing things in LaTeX...
  3. You can also have a look at PyLit, which is intended for literate programming with Python, but not as well maintained as some of the alternatives.
  4. Sphinx is great for documenting with python, and can output LaTex.
  5. Here's a list of tools for literate programming. Some of these work with any programming language.


Dexy is a very similar product to Sweave. One advantage of Dexy is that it is not exclusive to one single language. You could create a Dexy document that included R code, Python code, or about anything else.


This is a bit late, but for future reference you might consider my PythonTeX package for LaTeX. PythonTeX allows you enter Python code in a LaTeX document, run it, and bring back the output. But unlike Sweave, the document you actually edit is a valid .tex document (not .Snw or .Rnw), so editing the non-code part of the document is fast and convenient.

PythonTeX provides many features, including the following:

  • The document can be compiled without running any Python code; code only needs to be executed when it is modified.
  • All Python output is saved or cached.
  • Code runs in user-defined sessions. If there are multiple sessions, sessions automatically run in parallel using all available cores.
  • Errors and warnings are synchronized with the line numbers of the .tex document, so you know exactly where they came from.
  • Code can be executed, typeset, or typeset and executed. Syntax highlighting is provided by Pygments.
  • Anything printed by Python is automatically brought into the .tex document.
  • You can customize when code is re-executed (modified, errors, warnings, etc.).
  • The PythonTeX utilities class is available in all code that is executed. It allows you to automatically track dependencies and specify created files that should be cleaned up. For example, you can set the document to detect when the data it depends on is modified, so that code will be re-executed.

A basic PythonTeX file looks like this:

\documentclass{article}
\usepackage{pythontex}

\begin{document}

\begin{pycode}
#Whatever you want here!
\end{pycode}

\end{document}


You might consider noweb, which is language independent and is the basis for Sweave. I've used it for Python and it works well.

http://www.cs.tufts.edu/~nr/noweb/


I've restructured Matti's Pweave a bit, so that it is possible to define arbitrary "chunk-processors" as plugin-modules. This makes it easy to extend for several chunk-based text-preprocessing applications. The restructured version is available at https://bitbucket.org/edgimar/pweave/src. As an example, you could write the following LaTeX-Pweave document (notice the "processor name" in this example is specified with the name 'mplfig'):

\documentclass[a4paper]{article}
\usepackage{graphicx}
\begin{document}
\title{Test document}
\maketitle

Don't miss the great information in Figure \ref{myfig}!


<<p=mplfig, label=myfig, caption = "Figure caption...">>=
import sys
import pylab as pl

pl.plot([1,2,3,4,5],['2,4,6,8,10'], 'b.', markersize=15)
pl.axis('scaled')
pl.axis([-3,3, -3,3]) # [xmin,xmax, ymin,ymax]
@

\end{document}


You could try SageTeX which implements Sweave-Like functionality for the SAGE mathematics platform. I haven't played around with it as much as I would like to, but SAGE is basically a python shell and evaluates python as it's native language.


I have also thought about the same thing many times. After reading your questions and looking into your link I made small modifications to the custom python Sweave driver, that you link to. I modified it to also keep the source code and produce the output as well the same way that Sweave does for R.

I posted the modified version and an example here: http://mpastell.com/2010/02/09/python-in-sweave-document/

Granted, it is not optimal but I'm quite happy with the output and I like the ability to include both R and Python in the same document.

Edit about PyLit:

I also like PyLit and contrary to my original answer you can catch ouput with it as well, although it not as elegant as Sweave! Here is a small example how to do it:

import sys

# Catch PyLit output

a = range(3)
sys.stdout = open('output.txt', 'w')
print a
sys.stdout = sys.__stdout__

# .. include:: output.txt


What you're looking for is achieved with GNU Emacs and org-mode*. org-mode does far more than can be detailed in a single response, but the relevant points are:

  • Support for literate programming with the ability to integrate multiple languages within the same document (including using one language's results as the input for another language).
  • Graphics integration.
  • Export to LaTeX, HTML, PDF, and a variety of other formats natively, automatically generating the markup (but you can do it manually, too).
  • Everything is 100% customizable, allowing you to adapt the editor to your needs.

I don't have Python installed on my system, but below is an example of two different languages being run within the same session. The excerpt is modified from the wonderful org-mode R tutorial by Erik Iverson which explains the set up and effective use of org-mode for literate programming tasks. This SciPy 2013 presentation demonstrates how org-mode can be integrated into a workflow (and happens to use Python).

Sweave for python

Emacs may seem intimidating. But for statistics/data science, it offers tremendous capabilities that either aren't offered anywhere else or are spread across various systems. Emacs allows you to integrate them all into a single interface. I think Daniel Gopar says it best in his Emacs tutorial,

Are you guys that lazy? I mean, c'mon, just read the tutorial, man.

An hour or so with the Emacs tutorial opens the door to some extremely powerful tools.

* Emacs comes with org-mode. No separate install is required.


Well, with reticulate which is a recent best implementation of a Python interface in R you could continue using Sweave and call Python inline using the R interpreter. For example this now works in a .Rnw or .Rmd markdown file.

```{r example, include=FALSE}
library(reticulate)
use_python("./dir/python")
```

```{python}
import pandas
data = pandas.read_csv("./data.csv")
print(data.head())
```


I think that Jupyter-book may do what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜