What's the best way to do literate programming in Python on Windows? [closed]
Asked Answered
D

7

46

I've been playing with various ways of doing literate programming in Python. I like noweb, but I have two main problems with it: first, it is hard to build on Windows, where I spend about half my development time; and second, it requires me to indent each chunk of code as it will be in the final program --- which I don't necessarily know when I write it. I don't want to use Leo, because I'm very attached to Emacs.

Is there a good literate programming tool that:

  1. Runs on Windows
  2. Allows me to set the indentation of the chunks when they're used, not when they're written
  3. Still lets me work in Emacs

Thanks!


Correction: noweb does allow me to indent later --- I misread the paper I found on it.

By default, notangle preserves whitespace and maintains indentation when expanding chunks. It can therefore be used with languages like Miranda and Haskell, in which indentation is significant

That leaves me with only the "Runs on Windows" problem.

Declarative answered 12/8, 2009 at 16:32 Comment(6)
I think point two is never going to be easy to solve: indentation is key to the Python language so my guess is you will never solve that one.Vanish
@jkp: Actually, it's relatively easy to resolve the indendentation when emitting the final source distinct from the literate programming document. As long as your LP language imposes some rational rules on what kind of Python statement suites it will work with.Sanctum
Apparently someone has implemented the python equivalent: noweb.py.Decennium
Why was this question closed? Seems to me that any question that gets this many upvotes for the question, 40 votes for answers is answering a need. Revisit the rules?Belligerency
@Sherwood Botsford you must be new here.Declarative
@Declarative can you change the >>> in question to > ? thanks. i would have suggested an edit, but ...Bouffe
S
7

I did this:

http://sourceforge.net/projects/pywebtool/

You can get any number of web/weave products that will help you construct a document and code in one swoop.

You can -- pretty easily -- write your own. It's not rocket science to yank the Python code blocks out of RST source and assemble it. Indeed, I suggest you write your own Docutils directives to assemble the Python code from an RST source document.

You run the RST through docutils rst2html (or Sphinx) to produce your final HTML report.

You run your own utility on the same RST source to extract the Python code blocks and produce the final modules.

Sanctum answered 12/8, 2009 at 20:39 Comment(4)
I'm looking through your approach --- it looks pretty close to what I'd like to have. I'd much rather adapt myself to someone else's tool than write one of my own; look what's happened with web frameworks!Declarative
Everything about this looks good, except that when I generate LaTeX, there's a bunch of nuweb markup that MiKTeX and TeTeX don't seem to understand; I can't find a way to remedy that. Can you explain how it works? I read your excellent documents, but I'm not sure what you mean when you say, "The biggest gap in the LATEX support is a complete lack of understanding of the original markup in nuweb, and the very real damage done to that markup when creating pyWeb."Declarative
@JsonFruit: Without understanding any LaTeX, I converted some parts of nuweb. Since I didn't take the time to understand any of the LaTeX, I suspected it was wrong. You've found out for a fact that I converted it wrong. Since the LaTeX markup is relatively easy to isolate, you might want to consider fixing the Markup. Or use nuweb (or noweb) directly -- they might be better.Sanctum
I'll play with the LaTeX a bit - the parsing code is simple and easy to read. If I come up with a satisfying solution, I'll send you some patches.Declarative
S
15

I have written Pweave http://mpastell.com/pweave, that is aimed for dynamic report generation and uses noweb syntax. It is a pure python script so it also runs on Windows. It doesn't fix your indent problem, but maybe you can modify it for that, the code is really quite simple.

Savina answered 22/5, 2010 at 6:1 Comment(0)
G
11

The de-facto standard in the community is IPython notebooks.

Excellent example in which Peter Norvig demonstrates algorithms to solve the Travelling Salesman Problem: https://nbviewer.org/url/norvig.com/ipython/TSP.ipynb

More examples listed at https://github.com/jupyter/jupyter/wiki

Graeco answered 2/3, 2014 at 18:38 Comment(6)
Ipython/Jupyter Notebooks are good for exploration, but they really don't compare to rmarkdown/Sweave - they are painful to work with in git, because they store a lot of metadata and output in the XML file that's not necessary to keep a history of (really, you only want the generators, not the output). Pweave is a better solution for literate programming.Leporide
@Colonel-panic and all Thanks for the point this out..Couple of questions...Digitalis
Is there a way to extract out the python code into a separate python file in Jupyter notebooks?Digitalis
Can I run a jupyter notebook at the commend line, such that it will execute the entire code and capture the resulted generated images/other ouput into the jupyter notebook?Digitalis
There's Jupytext for Jupyter.Flannelette
stop calling ipython/jupyter notebooks as literate programming. they are NOT. literate programming allows using macros and building code gradually - whereas these "modern" semi-literate notebooks just focus on markup rendering part.Bouffe
S
7

I did this:

http://sourceforge.net/projects/pywebtool/

You can get any number of web/weave products that will help you construct a document and code in one swoop.

You can -- pretty easily -- write your own. It's not rocket science to yank the Python code blocks out of RST source and assemble it. Indeed, I suggest you write your own Docutils directives to assemble the Python code from an RST source document.

You run the RST through docutils rst2html (or Sphinx) to produce your final HTML report.

You run your own utility on the same RST source to extract the Python code blocks and produce the final modules.

Sanctum answered 12/8, 2009 at 20:39 Comment(4)
I'm looking through your approach --- it looks pretty close to what I'd like to have. I'd much rather adapt myself to someone else's tool than write one of my own; look what's happened with web frameworks!Declarative
Everything about this looks good, except that when I generate LaTeX, there's a bunch of nuweb markup that MiKTeX and TeTeX don't seem to understand; I can't find a way to remedy that. Can you explain how it works? I read your excellent documents, but I'm not sure what you mean when you say, "The biggest gap in the LATEX support is a complete lack of understanding of the original markup in nuweb, and the very real damage done to that markup when creating pyWeb."Declarative
@JsonFruit: Without understanding any LaTeX, I converted some parts of nuweb. Since I didn't take the time to understand any of the LaTeX, I suspected it was wrong. You've found out for a fact that I converted it wrong. Since the LaTeX markup is relatively easy to isolate, you might want to consider fixing the Markup. Or use nuweb (or noweb) directly -- they might be better.Sanctum
I'll play with the LaTeX a bit - the parsing code is simple and easy to read. If I come up with a satisfying solution, I'll send you some patches.Declarative
C
7

You could use org-mode and babel-tangle.

That works quite well, since you can give :noweb-ref to source blocks.

Here’s a minimal example: Activate org-babel-tangle, then put this into the file noweb-test.org:

#+begin_src python :exports none :noweb-ref c
abc = "abc"
#+end_src

#+begin_src python :noweb yes :tangle noweb-test.py
def x():
  <<c>>
  return abc

print(x())
#+end_src

You can also use properties of headlines for giving the noweb-ref. It can then even automatically concatenate several source blocks into one noweb reference.

Add :results output to the #+begin_src line of the second block to see the print results under that block when you hit C-c C-c in the block.

Cistercian answered 22/6, 2012 at 11:27 Comment(0)
S
3

You might find noweb 3 easier to build on Windows. It was designed to be more portable than standard noweb.

Slatternly answered 25/4, 2010 at 20:2 Comment(2)
I'll check it out once the site is responding again.Declarative
I gave it a look --- it seems much more likely to work for me than the previous version. I quite likely would have gone with this had I realized it was available before I settled on another tool.Declarative
H
0

See also my last LP tool: https://code.google.com/archive/p/nano-lp/. It does not requires special input format, supports Markdown/MultiMarkdown, reStructuredText, OpenOffice/LibreOffice, Creole, TeX/LaTeX and has super light and clean syntax - no more cryptic literate programs.

Hollowell answered 15/1, 2013 at 15:46 Comment(2)
Please edit your answer to have a current link. The present (2021-Jan-6) one has a terse description and no source.Belligerency
@Hollowell can you please add that to some more modern hosting site like codeberg, github, gitlab or the like? there are various other "exports to github" for nano-lp, but they are all essentially blank repos with a readme.Bouffe
P
0

Found this tool to be useful: https://github.com/bslatkin/pyliterate

Prochronism answered 24/4, 2017 at 22:40 Comment(1)
In this case it's not your fault because the question was off-topic, but please note that in general, an answer that recommends some tool or library must demonstrate how it solves the problem at hand in the answer itself.Lamm

© 2022 - 2024 — McMap. All rights reserved.