On 08/29/2012 11:54 AM, Richard Thomas wrote:
> I have a desire to be able to run Reinteract worksheets outside of the
> Reinteract environment - i.e. running the code directly by calling
> python. This is partly so I can work within Reinteract, but hand code to
> someone who only has Python installed.
[...]
> It would be nice to able to do something similar (or is there a better
> way?) with refigure2 (assuming I could also get the PDF problem sorted).
> Through major kludgery I managed to get this working with the original
> refigure, but have had no success with refigure2. I am very rusty on my
> Python which doesn't help. Any suggestions?
Below is a short library which can work in place of refigure2 with pure
Python. Some caveats:
1) You have to use the 'with figure() as f: ...; f;' syntax instead of
the 'build figure(): ...' syntax, since, obviously, Python doesn't have
the build statement. (The currently-released refigure2 has a hack to
allow the with statement to produce output, but that'll go away in the
upcoming version [Real Soon Now].)
2) You can't use rclocal() or the refigurerc option files. This could
be added in relatively easily, but I didn't feel like it. If you need
this and can't figure it out, please let me know.
3) Figures are displayed for the user one at a time. By adjusting the
__exit__ method, you may be able to change this. (For example, you
could save each figure and move on.)
Hope this helps,
Robert
-----refigure2.py (fake version)---------------------------------------
from matplotlib.figure import Figure as _Figure
from matplotlib.pyplot import *
_Figure.__enter__ = lambda self: self
_Figure.__exit__ = lambda self, type, value, traceback: show()
-----------------------------------------------------------------------