From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ken Mankoff Subject: Re: Help with "alternative" ipython session workflow Date: Sat, 11 Apr 2015 22:54:51 -0400 Message-ID: References: <5529A9B6.1050901@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:55389) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yh831-0001Ep-U9 for emacs-orgmode@gnu.org; Sat, 11 Apr 2015 22:55:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yh82x-00027r-8b for emacs-orgmode@gnu.org; Sat, 11 Apr 2015 22:54:59 -0400 Received: from mail-qk0-x231.google.com ([2607:f8b0:400d:c09::231]:33752) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yh82x-00027a-2Z for emacs-orgmode@gnu.org; Sat, 11 Apr 2015 22:54:55 -0400 Received: by qkx62 with SMTP id 62so107131025qkx.0 for ; Sat, 11 Apr 2015 19:54:54 -0700 (PDT) In-reply-to: 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: Ista Zahn Cc: Giacomo M , emacs-orgmode Mailinglist 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 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 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 >>