From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kyle Meyer Subject: Re: Extraneous output from Python code blocks using :session option Date: Wed, 11 Mar 2015 14:29:45 -0400 Message-ID: <87r3sv2xra.fsf@kyleam.com> References: <1931A590-8B23-4412-86C7-F3571EC466FF@haas.berkeley.edu> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:48076) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YVlOC-0007Rp-Kk for emacs-orgmode@gnu.org; Wed, 11 Mar 2015 14:29:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YVlO8-0004CW-HW for emacs-orgmode@gnu.org; Wed, 11 Mar 2015 14:29:52 -0400 Received: from mail-qc0-f179.google.com ([209.85.216.179]:34294) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YVlO8-0004CI-DX for emacs-orgmode@gnu.org; Wed, 11 Mar 2015 14:29:48 -0400 Received: by qcvp6 with SMTP id p6so12548208qcv.1 for ; Wed, 11 Mar 2015 11:29:47 -0700 (PDT) In-Reply-To: <1931A590-8B23-4412-86C7-F3571EC466FF@haas.berkeley.edu> (Richard Stanton's message of "Tue, 10 Mar 2015 20:38:25 -0700") 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: Richard Stanton Cc: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain 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: > > #+BEGIN_SRC python :session :results output > a = 2 > b = 3 > c = 4 > print 'a= ', a > print 'b = ', b > print 'a + b = ', a+b > #+END_SRC > > #+RESULTS: > : > : >>> >>> a= 2 > : b = 3 > : a + b = 5 I also see this behavior. I asked the list some time ago [1], but I didn't follow up beyond the initial email. > 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? 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. [1] http://thread.gmane.org/gmane.emacs.orgmode/79014 --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-ob-python.el-Strip-leading-session-characters.patch >From 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 * 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(-) 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)) +(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) -- 2.3.2 --=-=-= Content-Type: text/plain -- Kyle --=-=-=--