emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Babel python question: use of ipython and %cpaste
@ 2013-12-03  6:08 Arun Persaud
  2013-12-03 11:44 ` Rasmus
  2013-12-03 12:48 ` Andreas Röhler
  0 siblings, 2 replies; 4+ messages in thread
From: Arun Persaud @ 2013-12-03  6:08 UTC (permalink / raw)
  To: emacs-orgmode

Hi

being able to use python as a source block is great, but I often stumble
over the fact that when using sessions you have to treat empty lines in
a special way (i.e. as the end of an indentation block).

I was wondering if it would be easy to create an ipython mode, something
like

#+BEGIN_SRC ipython ...

where the content for a session is copied over to an ipython buffer
using the ipython magic %cpaste. This way empty lines should be treated
correctly.

It would also have the nice side effect of giving access to other magic
commands within org-babel, such as %timeit or profiling.

Unfortunately, I don't know how to modify ob-python.el to achieve this.
Apart from setting py-shell, I'm not sure how to copy the body of the
source block over to use %cpaste and also how to create a trigger for
BEGIN_SRC ipython...

Would this work? Any idea on how to implement this?

Thanks

Arun

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

* Re: Babel python question: use of ipython and %cpaste
  2013-12-03  6:08 Babel python question: use of ipython and %cpaste Arun Persaud
@ 2013-12-03 11:44 ` Rasmus
  2013-12-03 20:35   ` Arun Persaud
  2013-12-03 12:48 ` Andreas Röhler
  1 sibling, 1 reply; 4+ messages in thread
From: Rasmus @ 2013-12-03 11:44 UTC (permalink / raw)
  To: emacs-orgmode

Arun Persaud <apersaud@lbl.gov> writes:

> Hi
>
> being able to use python as a source block is great, but I often stumble
> over the fact that when using sessions you have to treat empty lines in
> a special way (i.e. as the end of an indentation block).
>
> I was wondering if it would be easy to create an ipython mode, something
> like
>
> #+BEGIN_SRC ipython ...

You can get some of the way already.  Personally, I don't see the need
of a ipython block as I see IPython as an interpreter.


> where the content for a session is copied over to an ipython buffer
> using the ipython magic %cpaste. This way empty lines should be treated
> correctly.
>
> It would also have the nice side effect of giving access to other magic
> commands within org-babel, such as %timeit or profiling.

You could still use python source blocks for this.

For python.el I use :

  (setq python-shell-interpreter "ipython"
        ;; org mode seems to work better with classic mode. . .
        python-shell-interpreter-args "--classic --no-banner"
        python-shell-completion-setup-code
        "from IPython.core.completerlib import module_completion"
        python-shell-completion-module-string-code
        "';'.join(module_completion('''%s'''))\n"
        python-shell-completion-string-code
        "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")


For Org you could do: 

  (setq org-babel-python-command "ipython --no-banner --classic --no-confirm-exit")

You should now be able to do

#+BEGIN_SRC python :results output
%timeit 1+1
#+END_SRC

#+RESULTS:
: >>> 10000000 loops, best of 3: 31.5 ns per loop
: >>> 

–Rasmus

-- 
Hooray!

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

* Re: Babel python question: use of ipython and %cpaste
  2013-12-03  6:08 Babel python question: use of ipython and %cpaste Arun Persaud
  2013-12-03 11:44 ` Rasmus
@ 2013-12-03 12:48 ` Andreas Röhler
  1 sibling, 0 replies; 4+ messages in thread
From: Andreas Röhler @ 2013-12-03 12:48 UTC (permalink / raw)
  To: emacs-orgmode

Am 03.12.2013 07:08, schrieb Arun Persaud:
> Hi
>
> being able to use python as a source block is great, but I often stumble
> over the fact that when using sessions you have to treat empty lines in
> a special way (i.e. as the end of an indentation block).
>
> I was wondering if it would be easy to create an ipython mode, something
> like
>
> #+BEGIN_SRC ipython ...
>
> where the content for a session is copied over to an ipython buffer
> using the ipython magic %cpaste. This way empty lines should be treated
> correctly.
>

Using the magic command, as shown in Rasmus' answer, should work.
If not, as for python-mode.el, please file a bug-report at

https://bugs.launchpad.net/python-mode

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

* Re: Babel python question: use of ipython and %cpaste
  2013-12-03 11:44 ` Rasmus
@ 2013-12-03 20:35   ` Arun Persaud
  0 siblings, 0 replies; 4+ messages in thread
From: Arun Persaud @ 2013-12-03 20:35 UTC (permalink / raw)
  To: emacs-orgmode

Hi

On 12/03/2013 03:44 AM, Rasmus wrote:
>> [ipython in org mode]
> For Org you could do: 
> 
>   (setq org-babel-python-command "ipython --no-banner --classic --no-confirm-exit")
> 
> You should now be able to do
> 
> #+BEGIN_SRC python :results output
> %timeit 1+1
> #+END_SRC
>[...]

nice ;) that works well for me. Thanks!

Since I now have ipython as an interpreter, is there a way to have org
mode use %cpaste to copy the code into the python buffer?

That way empty lines would be handled correctly, e.g.

#+BEGIN_SRC python :results output :session
  for i in range(2):
     print(i)

     print("next")

  print("done")
#+END_SRC

would work. I got it to work using something like this

; 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
  (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 also put a request in to have a %cpaste -q option to suppress output.
But I'm wondering if there is a better way of doing this...

thanks again

Arun

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

end of thread, other threads:[~2013-12-03 20:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-03  6:08 Babel python question: use of ipython and %cpaste Arun Persaud
2013-12-03 11:44 ` Rasmus
2013-12-03 20:35   ` Arun Persaud
2013-12-03 12:48 ` Andreas Röhler

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).