From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jacek Generowicz Subject: Re: Tangling without clutter? Date: Thu, 15 Mar 2012 09:42:26 +0100 Message-ID: <4f77ebe4-90ed-4a0d-a1d4-465dcbe198d7@CERNFE22.cern.ch> References: <44B0EAE8544C834188E8790873CDE1CC3EA752@ARCEXCHANGE.arc.local> <44B0EAE8544C834188E8790873CDE1CC3EA82A@ARCEXCHANGE.arc.local> <87y5r30zdd.fsf@gmx.com> <44B0EAE8544C834188E8790873CDE1CC3EA8C0@ARCEXCHANGE.arc.local> <20120315062547.GA28824@kenny.fritz.box> Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset="US-ASCII" Return-path: Received: from eggs.gnu.org ([208.118.235.92]:56605) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S86GZ-0007Yx-Jz for emacs-orgmode@gnu.org; Thu, 15 Mar 2012 04:42:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S86GU-0001LQ-KD for emacs-orgmode@gnu.org; Thu, 15 Mar 2012 04:42:35 -0400 Received: from cernmx30.cern.ch ([137.138.144.177]:24899) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S86GU-0001LE-Ar for emacs-orgmode@gnu.org; Thu, 15 Mar 2012 04:42:30 -0400 In-Reply-To: <20120315062547.GA28824@kenny.fritz.box> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org At Thu, 15 Mar 2012 07:25:47 +0100, Viktor Rosenfeld wrote: > > Hi Jos'h, > > have you looked at the :session header argument? I use it to define > environment variables in bash that are used in later code blocks. > > Something like this: > > #+BEGIN_SRC sh :session foo > export W="world." > #+END_SRC > > #+RESULTS: > > #+BEGIN_SRC sh :session foo > echo Hello $W > #+END_SRC > > If these code blocks are executed in order, the latter returns "Hello > world." Which is exactly how I was planning to use org babel to prepare a lot of Python-based teaching material. Unfortunately ... > Not sure though, how it interacts with Python. ... it interacts with Python in a less than ideal way. In effect, it pretends you typed the code in the Python source block, into Python's standard interactive shell. This leads to problems: Imagine that you want to demonstrate how to write a simple class in Python, and that you wish to follow Python's style guide, which states that "Method definitions inside a class are separated by a single blank line." So you present this example code #+begin_src python :session :results output class Foo: def __init__(self, state): self.state = state def get(self): return self.state #+end_src in the hope of writing some explanatory notes about it, before demonstrating its use #+begin_src python :session :results output f = Foo('frustrated') print f.get() #+end_src Unfortunately, the plan is foiled because the class definiton in the first block fails to execute properly. This happens because Python's standard interactive shell (in contrast to Python's non interactive mode) understands blank lines to close all currently open blocks. This results in the =class Foo:= block being closed before any of its components have been defined, at which point the whole thing goes pear-shaped. Currently there are two things you can do to get around this problem. 1. Remove any blank lines which are inside any block in any of your code (resulting in very ugly and heterodox formatting of your Python code: not the sort of thing you want to be doing in tutorials, lectures, documentation, etc.). 2. Hunt down all blank lines which are inside a block, and add enough whitespace to match the indentation of the next non-whitespace line in that block (very tedious and very fragile). Last week I had a short exchange about this, on list, with Eric Schulte, and seem to have persuaded him that the current state of affairs is not the desired one. I am planning to have a look at how the situation can be improved with the aim of providing a patch, but 1. I have zero time to devote to this in the next few weeks. 2. I have never tinkered with babel's internals before, so I cannot guarantee success, or quality of output. In the meantime, I have *slightly* changed my position, since the exchange with Eric. Last week I claimed that what the session mode should do is, essentially, emulate Emacs' python-mode's C-c C-c (execute the code block in the context of the session without choking on blank lines, and without echoing any of the code itself). I stand by this being the desired primary behaviour, but I do admit that an alternative valid (though less important) use case would be to have the code in the src block be 'typed' into a standard interactive session, and have the whole session (prompt, input, output) be produced as the result. (IIRC, this currently doesn't work properly either: the prompts and the inputs are out of sync, and you get a horrible mess.)