From mboxrd@z Thu Jan 1 00:00:00 1970 From: Richard Stanton Subject: Re: Extraneous output from Python code blocks using :session option Date: Wed, 11 Mar 2015 11:54:54 -0700 Message-ID: <48F4EA81-A674-40E5-A7F1-3844C7A4D977@haas.berkeley.edu> References: <1931A590-8B23-4412-86C7-F3571EC466FF@haas.berkeley.edu> <87r3sv2xra.fsf@kyleam.com> Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2070.6\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:53598) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YVlmb-0004fP-Kp for emacs-orgmode@gnu.org; Wed, 11 Mar 2015 14:55:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YVlmW-0006MA-LE for emacs-orgmode@gnu.org; Wed, 11 Mar 2015 14:55:05 -0400 Received: from mail-pa0-f54.google.com ([209.85.220.54]:38128) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YVlmW-0006Lu-DH for emacs-orgmode@gnu.org; Wed, 11 Mar 2015 14:55:00 -0400 Received: by pabrd3 with SMTP id rd3so13593711pab.5 for ; Wed, 11 Mar 2015 11:54:58 -0700 (PDT) In-Reply-To: <87r3sv2xra.fsf@kyleam.com> 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: Kyle Meyer Cc: emacs-orgmode@gnu.org > On Mar 11, 2015, at 11:29 AM, Kyle Meyer wrote: >=20 > Richard Stanton wrote: >> I'm trying to use the :session option so I can import modules, etc., >> just once at the beginning of my document, like with am IPython >> notebook. Unfortunately, the output from these code blocks contains >> some extraneous characters. For example: >>=20 >> #+BEGIN_SRC python :session :results output >> a =3D 2 >> b =3D 3 >> c =3D 4 >> print 'a=3D ', a >> print 'b =3D ', b >> print 'a + b =3D ', a+b >> #+END_SRC >>=20 >> #+RESULTS: >> : >> : >>> >>> a=3D 2 >> : b =3D 3 >> : a + b =3D 5 >=20 > I also see this behavior. I asked the list some time ago [1], but I > didn't follow up beyond the initial email. >=20 >> How can I stop the production of all those ">" signs (sometimes >> they're dots), which don't appear if I run the same code block = without >> the :session option? >=20 > I've attached a patch that seems to fix the example you gave and the > cases from my earlier email. I don't know enough about babel's > internals to know if it is a good way to fix it, but at least it might > serve as a quick fix for you until there is a better solution. >=20 > [1] http://thread.gmane.org/gmane.emacs.orgmode/79014 >=20 > =46rom cfa2783e03152871c0d703a6f79e96254dbe6c44 Mon Sep 17 00:00:00 = 2001 > From: Kyle Meyer > Date: Wed, 11 Mar 2015 14:17:09 -0400 > Subject: [PATCH] ob-python.el: Strip leading session characters >=20 > * lisp/ob-python.el (org-babel-python-evaluate-session): Strip leading > "..." and ">>>" from session output. > (org-babel-python-strip-session-chars): New function. > --- > lisp/ob-python.el | 30 ++++++++++++++++++++---------- > 1 file changed, 20 insertions(+), 10 deletions(-) >=20 > diff --git a/lisp/ob-python.el b/lisp/ob-python.el > index dd3cc66..0267f22 100644 > --- a/lisp/ob-python.el > +++ b/lisp/ob-python.el > @@ -306,16 +306,17 @@ (defun org-babel-python-evaluate-session > (results > (case result-type > (output > - (mapconcat > - #'org-babel-trim > - (butlast > - (org-babel-comint-with-output > - (session org-babel-python-eoe-indicator t body) > - (funcall input-body body) > - (funcall send-wait) (funcall send-wait) > - (insert org-babel-python-eoe-indicator) > - (funcall send-wait)) > - 2) "\n")) > + (org-babel-python-strip-session-chars > + (mapconcat > + #'org-babel-chomp > + (butlast > + (org-babel-comint-with-output > + (session org-babel-python-eoe-indicator t body) > + (funcall input-body body) > + (funcall send-wait) (funcall send-wait) > + (insert org-babel-python-eoe-indicator) > + (funcall send-wait)) > + 2) "\n"))) > (value > (let ((tmp-file (org-babel-temp-file "python-"))) > (org-babel-comint-with-output > @@ -339,6 +340,15 @@ (defun org-babel-python-read-string (string) > (match-string 1 string) > string)) >=20 > +(defun org-babel-python-strip-session-chars (string) > + "Remove leading >>> and ... from Python session output." > + (with-temp-buffer > + (insert string) > + (goto-char (point-min)) > + (when (looking-at "\\s-*\\(\\(>>> \\)*\\(\\.\\.\\. \\)*\\)") > + (delete-region (match-beginning 1) (match-end 1))) > + (buffer-string))) > + > (provide 'ob-python) >=20 >=20 > --=20 > 2.3.2 >=20 >=20 > =E2=80=94 > Kyle Many thanks, Kyle. It would be great if this could be fixed in the main = code, but I=E2=80=99ll try your patch today.