tags:

views:

361

answers:

5
+6  Q: 

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 equivalent 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

+5  A: 

I don't believe that there's a direct equivalent, so Romain Francois's suggestion (in your link) is probably the best. You might also want to consider the following:

  1. Have a look at PyLit and PyReport which are intended for literate programming with Python.
  2. Sphinx is great for documenting with python, and can output LaTex.
  3. Here's a list of tools for literate programming. Some of these work with any programming language.
Shane
+1  A: 

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.

Sharpie
+1  A: 

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
Matti Pastell
+5  A: 

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/

Matti Pastell
Very nice! Can't wait to try it.
Shane
+1  A: 

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/

John Horton