mpastell / Pweave

Pweave is a scientific report generator and a literate programming tool for Python. It can capture the results and plots from data analysis and works well with numpy, scipy and matplotlib.
http://mpastell.com/pweave
Other
435 stars 64 forks source link

Can chunk option values be expressions? #163

Open barryrowlingson opened 2 years ago

barryrowlingson commented 2 years ago

I'm familiar with Sweave and Knitr in the R world. If I'm writing student worksheets I like to write the source with question block and answer blocks such that I can render a PDF with or without the answers because chunk options are evaluated as R expressions. For example:

<<init, echo=FALSE>>=
answers=TRUE
@

<<q1>>
Is this a question?
@

<<a1, echo=answers>>=
Yes if this is a question
@

But I can't get this to work in pweave. If I include a variable in a chunk option value I get a Name Error:

<<example, echo=True>>=
# test1
a = True
@

<<e2, echo=a >>=
print("a is ", a)
@

I can put some expressions that evaluate to True/False into it, for example:

<<e2, echo=2>3 >>=
print("a is ", a)
@

but it doesn't appear to have access the any of the named variables created in earlier chunks, unlike knitr.

If theres an easy way round this then I'll be pleased to see it - I may have missed something in the docs, but if its ingrained deeply in the code the I'll have to find another solution (knitr with the python engine might work, it can be flaky)... and I understand this project is not really supported much in which case maybe I should move on...

Thanks

Barry