emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* babel: using empty lines in python code while using session
@ 2014-03-11  0:35 Arun Persaud
  2015-01-23 15:11 ` Puneeth Chaganti
  0 siblings, 1 reply; 2+ messages in thread
From: Arun Persaud @ 2014-03-11  0:35 UTC (permalink / raw)
  To: emacs-orgmode

Hi

I started using python in org babel. The manual, for example [1], points
to the fact that in session mode you can't have empty lines, since they
will be interpreted differently. However, if you use ipython you can get
around this, by using the following in your .emacs:

----------------------------

; use ipython in org mode
(setq org-babel-python-command "ipython3 --no-banner --classic
--no-confirm-exit")

; use %cpaste to paste code into ipython in org mode
(defadvice org-babel-python-evaluate-session
  (around org-python-use-cpaste
	  (session body &optional result-type result-params) activate)
  "Add a %cpaste and '--' to the body, so that ipython does the right
thing."
  (setq body (concat "%cpaste\n" body "\n--"))
  ad-do-it
  (if (stringp ad-return-value)
      (setq ad-return-value (replace-regexp-in-string "\\(^Pasting code;
enter '--' alone on the line to stop or use Ctrl-D\.[\r\n]:*\\)" ""
ad-return-value))))

-----------------------------

I found it much easier, if I don't have to worry about the empty lines
when writing python code. Might be useful for someone else, so I thought
I post it here.

Also, in ipython 2.0 you will be able to use %cpaste -q to suppress
cpaste output, which will simplify the above a bit.

thanks again for org-mode, it's getting more useful all the time ;)

Arun

[1] http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: babel: using empty lines in python code while using session
  2014-03-11  0:35 babel: using empty lines in python code while using session Arun Persaud
@ 2015-01-23 15:11 ` Puneeth Chaganti
  0 siblings, 0 replies; 2+ messages in thread
From: Puneeth Chaganti @ 2015-01-23 15:11 UTC (permalink / raw)
  To: Arun Persaud; +Cc: emacs-orgmode

On Tue, Mar 11, 2014 at 6:05 AM, Arun Persaud <apersaud@lbl.gov> wrote:
> Hi
>
> I started using python in org babel. The manual, for example [1], points
> to the fact that in session mode you can't have empty lines, since they
> will be interpreted differently. However, if you use ipython you can get
> around this, by using the following in your .emacs:

This is great!  Thanks for investigating this, and letting us all
know!  `%cpaste -q` is great!

The advice function required now is much smaller.  Adding it below, in
case anyone else finds it useful.

-------------------

(defun pc/ipython-use-cpaste (args)
  "Add a %cpaste and '--' to the body, for IPython magic!."
  (let ((body (nth 1 args)))
    (setcar (cdr args) (format "%%cpaste -q\n%s\n--\n" body))))

(advice-add 'org-babel-python-evaluate-session
            :filter-args 'pc/ipython-use-cpaste)

-------------------

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-01-23 15:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-11  0:35 babel: using empty lines in python code while using session Arun Persaud
2015-01-23 15:11 ` Puneeth Chaganti

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).