emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* named python session
@ 2015-08-12 15:06 Andreas Leha
  2015-08-12 16:34 ` Ken Mankoff
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Leha @ 2015-08-12 15:06 UTC (permalink / raw)
  To: emacs-orgmode

Hi python users,

I have a simple question to all python users.  I have a source block
with a named python session, e.g. 
 #+begin_src python :results output :session mypy
   x = 1
   print x
 #+end_src

Now I
- execute that source block and then I
- edit that block in its own buffer via C-c '.

My question:
How do I associate that with the python process in *mypy*?

I am asked to start python when I run python-shell-send-region.

Thanks,
Andreas

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

* Re: named python session
  2015-08-12 15:06 named python session Andreas Leha
@ 2015-08-12 16:34 ` Ken Mankoff
  2015-08-13 13:12   ` Andreas Leha
  0 siblings, 1 reply; 4+ messages in thread
From: Ken Mankoff @ 2015-08-12 16:34 UTC (permalink / raw)
  To: Andreas Leha; +Cc: emacs-orgmode

Hi Andreas,

On 2015-08-12 at 08:06, Andreas Leha <andreas.leha@med.uni-goettingen.de> wrote:
> How do I associate that with the python process in *mypy*?
> I am asked to start python when I run python-shell-send-region.


I have set up my system so that Org asks for a buffer name every time =org-edit-special= is called. This may be helpful to you.

  -k.
 

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

#+END_SRC

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

* Re: named python session
  2015-08-12 16:34 ` Ken Mankoff
@ 2015-08-13 13:12   ` Andreas Leha
  2015-08-13 14:52     ` Ken Mankoff
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Leha @ 2015-08-13 13:12 UTC (permalink / raw)
  To: emacs-orgmode

Hi Ken,

Ken Mankoff <mankoff@gmail.com> writes:
> Hi Andreas,
>
> On 2015-08-12 at 08:06, Andreas Leha <andreas.leha@med.uni-goettingen.de> wrote:
>> How do I associate that with the python process in *mypy*?
>> I am asked to start python when I run python-shell-send-region.
>
>
> I have set up my system so that Org asks for a buffer name every time =org-edit-special= is called. This may be helpful to you.
>
>   -k.
>  
>
> *** 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)))
>                   )))
>
> #+END_SRC

Thanks a lot for setting me on the right track!  I am quite surprised
(coming from ess as my interface to R) that multiple named sessions
aren't supported out of the box for python.

I use your code now, which in my use-case works great.  Only slight
variation: I set the default python process-name to the session name
using these two functions:

#+BEGIN_SRC emacs-lisp :results none
(defun al-org-babel-get-session (&optional arg info)
  "Return the :session of the current source-code block.
Will return
- nil if :session is given but not named
- \"none\" if no :session argument is specified
- the session name otherwise"
  (interactive)
  (if (org-src-edit-buffer-p)
      (let ((beg org-src--beg-marker))
	(with-current-buffer (org-src--source-buffer)
	  (goto-char beg)
	  (al-org-babel-get-session)))
    (let* ((info (or info (org-babel-get-src-block-info)))
           (params (nth 2 info))
           (session (cdr (assoc :session params))))
      session)))

(defun kdm/my-py-sh-get-proc-name (dedicated)
  (let* ((sessionname (al-org-babel-get-session))
	 (defaultname (or sessionname (if (equalp sessionname "none")
					  (buffer-name)
					sessionname))))
    (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 defaultname nil defaultname))))))
#+END_SRC

Thanks again,
Andreas

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

* Re: named python session
  2015-08-13 13:12   ` Andreas Leha
@ 2015-08-13 14:52     ` Ken Mankoff
  0 siblings, 0 replies; 4+ messages in thread
From: Ken Mankoff @ 2015-08-13 14:52 UTC (permalink / raw)
  To: Andreas Leha; +Cc: emacs-orgmode

Hi Andreas,

Glad I could help, and THANK YOU for your improvement. I had started to try to use the session name, if present, but never figured it out. It was on my TODO list.

  -k.
  

On 2015-08-13 at 06:12, Andreas Leha <andreas.leha@med.uni-goettingen.de> wrote:
> Hi Ken,
>
> Ken Mankoff <mankoff@gmail.com> writes:
>> Hi Andreas,
>>
>> On 2015-08-12 at 08:06, Andreas Leha <andreas.leha@med.uni-goettingen.de> wrote:
>>> How do I associate that with the python process in *mypy*?
>>> I am asked to start python when I run python-shell-send-region.
>>
>>
>> I have set up my system so that Org asks for a buffer name every time =org-edit-special= is called. This may be helpful to you.
>>
>>   -k.
>>  
>>
>> *** 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)))
>>                   )))
>>
>> #+END_SRC
>
> Thanks a lot for setting me on the right track!  I am quite surprised
> (coming from ess as my interface to R) that multiple named sessions
> aren't supported out of the box for python.
>
> I use your code now, which in my use-case works great.  Only slight
> variation: I set the default python process-name to the session name
> using these two functions:
>
> #+BEGIN_SRC emacs-lisp :results none
> (defun al-org-babel-get-session (&optional arg info)
>   "Return the :session of the current source-code block.
> Will return
> - nil if :session is given but not named
> - \"none\" if no :session argument is specified
> - the session name otherwise"
>   (interactive)
>   (if (org-src-edit-buffer-p)
>       (let ((beg org-src--beg-marker))
> 	(with-current-buffer (org-src--source-buffer)
> 	  (goto-char beg)
> 	  (al-org-babel-get-session)))
>     (let* ((info (or info (org-babel-get-src-block-info)))
>            (params (nth 2 info))
>            (session (cdr (assoc :session params))))
>       session)))
>
> (defun kdm/my-py-sh-get-proc-name (dedicated)
>   (let* ((sessionname (al-org-babel-get-session))
> 	 (defaultname (or sessionname (if (equalp sessionname "none")
> 					  (buffer-name)
> 					sessionname))))
>     (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 defaultname nil defaultname))))))
> #+END_SRC
>
> Thanks again,
> Andreas

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

end of thread, other threads:[~2015-08-13 14:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-12 15:06 named python session Andreas Leha
2015-08-12 16:34 ` Ken Mankoff
2015-08-13 13:12   ` Andreas Leha
2015-08-13 14:52     ` Ken Mankoff

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