Re: [O] Babel python question: use of ipython and %cpaste

2013-12-03 Thread Rasmus
Arun Persaud apers...@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:
:  1000 loops, best of 3: 31.5 ns per loop
:  

–Rasmus

-- 
Hooray!




Re: [O] Babel python question: use of ipython and %cpaste

2013-12-03 Thread Andreas Röhler

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



Re: [O] Babel python question: use of ipython and %cpaste

2013-12-03 Thread Arun Persaud
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