1 ;;; org-babel-python.el --- org-babel functions for python evaluation
3 ;; Copyright (C) 2009 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Org-Babel support for evaluating python source code.
35 (org-babel-add-interpreter "python")
37 (add-to-list 'org-babel-tangle-langs
'("python" "py" "#!/usr/bin/env python"))
39 (defun org-babel-execute:python
(body params
)
40 "Execute a block of Python code with org-babel. This function is
41 called by `org-babel-execute-src-block' via multiple-value-bind."
42 (message "executing Python source code block")
43 (let ((full-body (concat
44 (mapconcat ;; define any variables
48 (org-babel-python-var-to-python (cdr pair
))))
49 vars
"\n") "\n" (org-babel-trim body
) "\n")) ;; then the source block body
50 (session (org-babel-python-initiate-session session
)))
51 (org-babel-python-evaluate session full-body result-type
)))
53 (defun org-babel-prep-session:python
(session params
)
54 "Prepare SESSION according to the header arguments specified in PARAMS."
55 (let* ((session (org-babel-python-initiate-session session
))
56 (vars (org-babel-ref-variables params
))
57 (var-lines (mapcar ;; define any variables
61 (org-babel-python-var-to-python (cdr pair
))))
63 (org-babel-comint-in-buffer session
65 (move-end-of-line 1) (insert var
) (comint-send-input nil t
)
66 (org-babel-comint-wait-for-output session
)) var-lines
))))
70 (defun org-babel-python-var-to-python (var)
71 "Convert an elisp var into a string of python source code
72 specifying a var of the same value."
74 (concat "[" (mapconcat #'org-babel-python-var-to-python var
", ") "]")
77 (defun org-babel-python-table-or-string (results)
78 "If the results look like a table, then convert them into an
79 Emacs-lisp table, otherwise return the results as a string."
81 (if (string-match "^\\[.+\\]$" results
)
83 (replace-regexp-in-string
84 "\\[" "(" (replace-regexp-in-string
85 "\\]" ")" (replace-regexp-in-string
86 ", " " " (replace-regexp-in-string
90 (defvar org-babel-python-buffers
'(:default . nil
))
92 (defun org-babel-python-session-buffer (session)
93 (cdr (assoc session org-babel-python-buffers
)))
95 (defun org-babel-python-initiate-session-by-key (&optional session
)
96 "If there is not a current inferior-process-buffer in SESSION
97 then create. Return the initialized session."
98 (save-window-excursion
99 (let* ((session (if session
(intern session
) :default
))
100 (python-buffer (org-babel-python-session-buffer session
)))
102 (setq org-babel-python-buffers
(cons (cons session python-buffer
)
103 (assq-delete-all session org-babel-python-buffers
)))
106 (defun org-babel-python-initiate-session (&optional session
)
107 (unless (string= session
"none")
108 (org-babel-python-session-buffer (org-babel-python-initiate-session-by-key session
))))
110 (defvar org-babel-python-last-value-eval
"_"
111 "When evaluated by Python this returns the return value of the last statement.")
112 (defvar org-babel-python-eoe-indicator
"'org_babel_python_eoe'"
113 "Used to indicate that evaluation is has completed.")
114 (defvar org-babel-python-wrapper-method
119 open('%s', 'w').write( str(main()) )")
121 (defun org-babel-python-evaluate (buffer body
&optional result-type
)
122 "Pass BODY to the Python process in BUFFER. If RESULT-TYPE equals
123 'output then return a list of the outputs of the statements in
124 BODY, if RESULT-TYPE equals 'value then return the value of the
125 last statement in BODY, as elisp."
127 ;; external process evaluation
128 (save-window-excursion
133 ;; (message "buffer=%s" (buffer-string)) ;; debugging
134 (shell-command-on-region (point-min) (point-max) "python" 'replace
)
137 (let ((tmp-file (make-temp-file "python-functional-results")))
141 org-babel-python-wrapper-method
142 (let ((lines (split-string
143 (org-remove-indentation (org-babel-trim body
)) "[\r\n]")))
146 (lambda (line) (format "\t%s" line
))
147 (butlast lines
) "\n")
148 (format "\n\treturn %s" (last lines
))))
150 ;; (message "buffer=%s" (buffer-string)) ;; debugging
151 (shell-command-on-region (point-min) (point-max) "python"))
152 (org-babel-python-table-or-string
153 (with-temp-buffer (insert-file-contents tmp-file
) (buffer-string)))))))
154 ;; comint session evaluation
155 (org-babel-comint-in-buffer buffer
156 (let* ((raw (org-babel-comint-with-output buffer org-babel-python-eoe-indicator t
157 ;; for some reason python is fussy, and likes enters after every input
158 (mapc (lambda (statement) (insert statement
) (comint-send-input nil t
))
159 (split-string (org-babel-trim full-body
) "[\r\n]+"))
160 (comint-send-input nil t
) (comint-send-input nil t
)
161 (insert org-babel-python-last-value-eval
)
162 (comint-send-input nil t
)
163 (insert org-babel-python-eoe-indicator
)
164 (comint-send-input nil t
)))
165 (results (delete org-babel-python-eoe-indicator
166 (cdr (member org-babel-python-eoe-indicator
167 (reverse (mapcar #'org-babel-trim raw
)))))))
168 (setq results
(mapcar #'org-babel-python-read-string results
))
170 (output (org-babel-trim (mapconcat #'identity
(reverse (cdr results
)) "\n")))
171 (value (org-babel-python-table-or-string (org-babel-trim (car results
)))))))))
173 (defun org-babel-python-read-string (string)
174 "Strip 's from around ruby string"
175 (if (string-match "'\\([^\000]+\\)'" string
)
176 (match-string 1 string
)
179 (provide 'org-babel-python
)
180 ;;; org-babel-python.el ends here