all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Ken Mankoff <mankoff@gmail.com>
To: Ista Zahn <istazahn@gmail.com>
Cc: Giacomo M <jackjackk@gmail.com>,
	emacs-orgmode Mailinglist <emacs-orgmode@gnu.org>
Subject: Re: Help with "alternative" ipython session workflow
Date: Sat, 11 Apr 2015 22:54:51 -0400	[thread overview]
Message-ID: <m21tjqukck.fsf@gmail.com> (raw)
In-Reply-To: <CA+vqiLEbQorwmAUFv8X6ELxYfuqi8rGYVhqFMNnx-OX9vc_OtA@mail.gmail.com>


Yes, as the author of the linked post, those worked for me. I've made some minor changes/improvements, so I'll reply to the OP in full here.

My current setup follows.

This works 100% for me, but does not match what the Org manual says the behavior should be. For example, ":results value" doesn't work unless I use :session. Inline code doesn't work unless I have ":session" like this: src_python[:session]{}

Not really a problem... The advantage of using sessions more is that you can keep some of your existing behavior: Split the buffer and have the session visible along with the code block in Org, or the code block in its special edit buffer via "C-'".

One feature I need to implement... Currently I have emacs prompt me for custom python session names (last code chunk below). When Org generates a Python buffer (via "C-'", or org-edit-special), and that block has a :session property, the new buffer should have the session name variable that I create below pre-populated to the :session name, rather than prompting the user...

Hope the code below helps.

  -k.
  

* Org
** Babel 
*** IPython 
    #+BEGIN_SRC emacs-lisp :results none
      (setq org-babel-python-command "ipython --pylab=osx --pdb --nosep --classic --no-banner --no-confirm-exit")

      ;; https://github.com/jorgenschaefer/elpy/issues/191#issuecomment-42474850
      ;; make IPython work w/ Org
      (defadvice org-babel-python-evaluate
        (around org-python-use-cpaste
                (session body &optional result-type result-params preamble) activate)
        "Add a %cpaste and '--' to the body, so that ipython does the right thing."
        ;;(setq body (concat "%cpaste\n" body "\n--"))
        ;;(setq body (concat "%cpaste -q\n" body "\n--\n"))
        (setq body (concat "%cpaste -q\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))))
    #+END_SRC

*** Properly comment babel blocks with M-;
https://lists.gnu.org/archive/html/emacs-orgmode/2013-11/msg00318.html
#+BEGIN_SRC emacs-lisp :results none
    ;; allow comment region in the code edit buffer (according to language)
    (defun my-org-comment-dwim (&optional arg)
      (interactive "P")
      (or (org-babel-do-key-sequence-in-edit-buffer (kbd "M-;"))
          (comment-dwim arg)))

    ;; make `C-c C-v C-x M-;' more convenient
    (define-key org-mode-map
      (kbd "M-;") 'my-org-comment-dwim)
#+END_SRC

* Programming
** Python
https://github.com/jorgenschaefer/elpy/
*** Elpy
#+BEGIN_SRC emacs-lisp :results none
(setenv "PYTHONPATH"
;;	"~/local/anaconda/bin:/Users/mankoff/local/python")
	"~/Library/Enthought/Canopy_64bit/User/bin:/Users/mankoff/local/python")


(elpy-enable)
(elpy-use-ipython) ;; but now un-set some features
;;(elpy-clean-modeline)
;;;(makunbound 'ipython)
(setq python-shell-interpreter "ipython"
      python-shell-interpreter-args "--pylab=osx --pdb --nosep --classic"
      python-shell-prompt-regexp ">>> "
      python-shell-prompt-output-regexp "")

;(setq elpy-rpc-backend "rope")
(setq elpy-rpc-backend "jedi") ;;; faster than the default Rope
                               ;;; backend. Not as many features though...
;(setq elpy-rpc-python-command "/Users/mankoff/Library/Enthought/Canopy_64bit/User/bin/python")
(setq py-shell-switch-buffers-on-execute-p t)
(setq py-switch-buffers-on-execute-p t)

(require 'comint)
(define-key comint-mode-map [(control n)]
  'comint-next-input)
(define-key comint-mode-map [(control p)]
  'comint-previous-input)

#+END_SRC

*** Misc
#+BEGIN_SRC emacs-lisp :results none
;; Other
(add-hook 'python-mode-hook (lambda () (setq mode-name "🐍")))
;;(add-hook 'python-mode-hook 'turn-on-orgstruct)

#+END_SRC

*** Don't send cursor to buffer after execution
https://github.com/jorgenschaefer/elpy/issues/134
#+BEGIN_SRC emacs-lisp :results none
(defun elpy-shell-send-region-or-buffer (&optional arg)
  (interactive "P")
  (elpy-shell-get-or-create-process)
  (if (region-active-p)
      (python-shell-send-region (region-beginning)
				(region-end))
    (python-shell-send-buffer arg)))

#+END_SRC

*** Change size of REPL buffer
https://github.com/jorgenschaefer/elpy/issues/109

#+BEGIN_SRC emacs-lisp :results none
(add-to-list 'display-buffer-alist
	     '("^\\*.*\\*$"
	       (display-buffer-reuse-window
		display-buffer-pop-up-window)
	       (window-height . 13)))
;;  (set-window-text-height (get-buffer-window buf) (/(frame-height) 3))))
#+END_SRC

*** Custom Python Session Names

https://github.com/jorgenschaefer/elpy/issues/383

I want each python session to optionally have a unique name, so that I
can run multiple sessions in multiple windows/buffers/directories and
not have them interact/interfere.

Here I've copied =elpy-shell-get-or-create-process= from =elpy.el= and
modified it. I also have to modify =python-shell-get-process-name=.
This all is fairly easy, except when executing from Org it gets more
complicated, hence the special case if a function called from
=org-ctrl-c-ctrl-c=. 

#+BEGIN_SRC emacs-lisp :results none
  (defun elpy-shell-get-or-create-process ()
    "Get or create an inferior Python process for current buffer and return it.
    Customized by KDM to make dedicated sessions"
    (let* ((bufname (format "*%s*" (python-shell-get-process-name t)))
           (proc (get-buffer-process bufname)))
      (if proc
          proc
        (run-python (python-shell-parse-command) t nil) ;; DEDICATED!
        (get-buffer-process bufname))))

  (defun python-shell-get-process-name (dedicated)
    (if (equal this-command 'org-ctrl-c-ctrl-c)
        (kdm/orig-py-sh-get-proc-name dedicated)
      (kdm/my-py-sh-get-proc-name dedicated)))

  (defun kdm/orig-py-sh-get-proc-name (dedicated)
    "Calculate the appropriate process name for inferior Python process.
       If DEDICATED is t returns a string with the form
       `python-shell-buffer-name'[variable `buffer-name']."
    (let ((process-name
           (if (and dedicated
                    (buffer-name))
               (format "%s[%s]" python-shell-buffer-name (buffer-name))
             (format "%s" python-shell-buffer-name))))
      process-name))

  (defun kdm/my-py-sh-get-proc-name (dedicated)
    (if (boundp 'py-buf-proc-name)
        (format "%s" py-buf-proc-name)
      (setq-local py-buf-proc-name
                  (format "%s"
                          (completing-read "Python session name: "
                                           nil nil nil (buffer-name) nil (buffer-name)))
                  )))

  ;; (makunbound 'py-buf-proc-name)
#+END_SRC







On 2015-04-11 at 21:59, Ista Zahn <istazahn@gmail.com> wrote:
> The settings described at
> http://lists.gnu.org/archive/html/emacs-orgmode/2014-05/msg00793.html
> seem to work for me. (I replaced "--pylab=osx" with "--pylab" since
> I'm on Linux.)
>
> Best
> Ista
>
> On Sat, Apr 11, 2015 at 7:09 PM, Giacomo M <jackjackk@gmail.com> wrote:
>> I have never been able to make org sessions getting along properly with
>> ipython, so at the moment I'm using a sort of handmade hybrid between org
>> and an ipython notebook, doing the following:
>> 1. Enter the first source block in a .org file
>> 2. Start an ipython instance with C-c C-c
>> 3. Keep the org/src buffer and *Python* buffer side by side
>> 4. Copy paragraph of code around cursor (keeping cursor in position) (I
>> usually split sections of code I want to run individually with spaces)
>> 5. Select *Python* buffer
>> 6. Go to last position
>> 7. Type %paste
>> 8. Go back to other buffer (src/org)
>>
>> I probably sound like a caveman doing this over and over, but this is the
>> most stable setup I could find. Plus I keep experimenting w/ code, and this
>> gives me a very interactive environment. Macros helped me to automatize
>> steps 4-8, but still it's not very flexible, e.g. I need the side-by-side
>> buffers, with ipython already running.
>>
>> I was wondering whether anybody has any suggestions to improve this (and
>> make me save some minutes of life every day), or any good reference to make
>> ipython work smoothly with org src blocks
>>
>> Thanks,
>>
>> Giacomo
>>

      reply	other threads:[~2015-04-12  2:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-11 23:09 Help with "alternative" ipython session workflow Giacomo M
2015-04-12  1:59 ` Ista Zahn
2015-04-12  2:54   ` Ken Mankoff [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m21tjqukck.fsf@gmail.com \
    --to=mankoff@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=istazahn@gmail.com \
    --cc=jackjackk@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.