Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-python.el --- org-babel functions for python evaluation |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2009-2014 Free Software Foundation, Inc. |
86fbb8ca | 4 | |
dfd98937 BG |
5 | ;; Authors: Eric Schulte |
6 | ;; Dan Davison | |
86fbb8ca CD |
7 | ;; Keywords: literate programming, reproducible research |
8 | ;; Homepage: http://orgmode.org | |
86fbb8ca CD |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs 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 of the License, or | |
15 | ;; (at your option) any later version. | |
16 | ||
17 | ;; GNU Emacs 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. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Org-Babel support for evaluating python source code. | |
28 | ||
29 | ;;; Code: | |
30 | (require 'ob) | |
86fbb8ca CD |
31 | (eval-when-compile (require 'cl)) |
32 | ||
33 | (declare-function org-remove-indentation "org" ) | |
34 | (declare-function py-shell "ext:python-mode" (&optional argprompt)) | |
3ab2c837 | 35 | (declare-function py-toggle-shells "ext:python-mode" (arg)) |
271672fa | 36 | (declare-function run-python "ext:python" (cmd &optional dedicated show)) |
86fbb8ca | 37 | |
3ab2c837 | 38 | (defvar org-babel-tangle-lang-exts) |
86fbb8ca CD |
39 | (add-to-list 'org-babel-tangle-lang-exts '("python" . "py")) |
40 | ||
41 | (defvar org-babel-default-header-args:python '()) | |
42 | ||
271672fa BG |
43 | (defcustom org-babel-python-command "python" |
44 | "Name of the command for executing Python code." | |
45 | :version "24.4" | |
46 | :package-version '(Org . "8.0") | |
47 | :group 'org-babel | |
48 | :type 'string) | |
86fbb8ca | 49 | |
271672fa BG |
50 | (defcustom org-babel-python-mode |
51 | (if (or (featurep 'xemacs) (featurep 'python-mode)) 'python-mode 'python) | |
3ab2c837 | 52 | "Preferred python mode for use in running python interactively. |
271672fa BG |
53 | This will typically be either 'python or 'python-mode." |
54 | :group 'org-babel | |
55 | :version "24.4" | |
56 | :package-version '(Org . "8.0") | |
73d3db82 | 57 | :type 'symbol) |
86fbb8ca | 58 | |
afe98dfa | 59 | (defvar org-src-preserve-indentation) |
86fbb8ca | 60 | |
271672fa BG |
61 | (defcustom org-babel-python-hline-to "None" |
62 | "Replace hlines in incoming tables with this when translating to python." | |
63 | :group 'org-babel | |
64 | :version "24.4" | |
65 | :package-version '(Org . "8.0") | |
66 | :type 'string) | |
67 | ||
68 | (defcustom org-babel-python-None-to 'hline | |
69 | "Replace 'None' in python tables with this before returning." | |
70 | :group 'org-babel | |
71 | :version "24.4" | |
72 | :package-version '(Org . "8.0") | |
73d3db82 | 73 | :type 'symbol) |
271672fa | 74 | |
86fbb8ca CD |
75 | (defun org-babel-execute:python (body params) |
76 | "Execute a block of Python code with Babel. | |
77 | This function is called by `org-babel-execute-src-block'." | |
afe98dfa CD |
78 | (let* ((session (org-babel-python-initiate-session |
79 | (cdr (assoc :session params)))) | |
80 | (result-params (cdr (assoc :result-params params))) | |
81 | (result-type (cdr (assoc :result-type params))) | |
acedf35c CD |
82 | (return-val (when (and (eq result-type 'value) (not session)) |
83 | (cdr (assoc :return params)))) | |
84 | (preamble (cdr (assoc :preamble params))) | |
afe98dfa CD |
85 | (full-body |
86 | (org-babel-expand-body:generic | |
2f885dca | 87 | (concat body (if return-val (format "\nreturn %s" return-val) "")) |
acedf35c | 88 | params (org-babel-variable-assignments:python params))) |
86fbb8ca | 89 | (result (org-babel-python-evaluate |
acedf35c | 90 | session full-body result-type result-params preamble))) |
3ab2c837 BG |
91 | (org-babel-reassemble-table |
92 | result | |
93 | (org-babel-pick-name (cdr (assoc :colname-names params)) | |
94 | (cdr (assoc :colnames params))) | |
95 | (org-babel-pick-name (cdr (assoc :rowname-names params)) | |
96 | (cdr (assoc :rownames params)))))) | |
86fbb8ca CD |
97 | |
98 | (defun org-babel-prep-session:python (session params) | |
afe98dfa CD |
99 | "Prepare SESSION according to the header arguments in PARAMS. |
100 | VARS contains resolved variable references" | |
86fbb8ca | 101 | (let* ((session (org-babel-python-initiate-session session)) |
afe98dfa CD |
102 | (var-lines |
103 | (org-babel-variable-assignments:python params))) | |
86fbb8ca CD |
104 | (org-babel-comint-in-buffer session |
105 | (mapc (lambda (var) | |
106 | (end-of-line 1) (insert var) (comint-send-input) | |
107 | (org-babel-comint-wait-for-output session)) var-lines)) | |
108 | session)) | |
109 | ||
110 | (defun org-babel-load-session:python (session body params) | |
111 | "Load BODY into SESSION." | |
112 | (save-window-excursion | |
113 | (let ((buffer (org-babel-prep-session:python session params))) | |
114 | (with-current-buffer buffer | |
115 | (goto-char (process-mark (get-buffer-process (current-buffer)))) | |
116 | (insert (org-babel-chomp body))) | |
117 | buffer))) | |
118 | ||
119 | ;; helper functions | |
120 | ||
afe98dfa | 121 | (defun org-babel-variable-assignments:python (params) |
8223b1d2 | 122 | "Return a list of Python statements assigning the block's variables." |
afe98dfa CD |
123 | (mapcar |
124 | (lambda (pair) | |
125 | (format "%s=%s" | |
126 | (car pair) | |
127 | (org-babel-python-var-to-python (cdr pair)))) | |
128 | (mapcar #'cdr (org-babel-get-header params :var)))) | |
129 | ||
86fbb8ca CD |
130 | (defun org-babel-python-var-to-python (var) |
131 | "Convert an elisp value to a python variable. | |
132 | Convert an elisp value, VAR, into a string of python source code | |
133 | specifying a variable of the same value." | |
134 | (if (listp var) | |
135 | (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]") | |
136 | (if (equal var 'hline) | |
271672fa | 137 | org-babel-python-hline-to |
86fbb8ca CD |
138 | (format |
139 | (if (and (stringp var) (string-match "[\n\r]" var)) "\"\"%S\"\"" "%S") | |
30cb51f1 | 140 | (if (stringp var) (substring-no-properties var) var))))) |
86fbb8ca CD |
141 | |
142 | (defun org-babel-python-table-or-string (results) | |
143 | "Convert RESULTS into an appropriate elisp value. | |
144 | If the results look like a list or tuple, then convert them into an | |
145 | Emacs-lisp table, otherwise return the results as a string." | |
666ffc7e SM |
146 | (let ((res (org-babel-script-escape results))) |
147 | (if (listp res) | |
148 | (mapcar (lambda (el) (if (equal el 'None) | |
149 | org-babel-python-None-to el)) | |
150 | res) | |
151 | res))) | |
86fbb8ca | 152 | |
271672fa | 153 | (defvar org-babel-python-buffers '((:default . "*Python*"))) |
86fbb8ca CD |
154 | |
155 | (defun org-babel-python-session-buffer (session) | |
156 | "Return the buffer associated with SESSION." | |
157 | (cdr (assoc session org-babel-python-buffers))) | |
158 | ||
d1389828 | 159 | (defun org-babel-python-with-earmuffs (session) |
271672fa BG |
160 | (let ((name (if (stringp session) session (format "%s" session)))) |
161 | (if (and (string= "*" (substring name 0 1)) | |
162 | (string= "*" (substring name (- (length name) 1)))) | |
163 | name | |
164 | (format "*%s*" name)))) | |
165 | ||
d1389828 | 166 | (defun org-babel-python-without-earmuffs (session) |
271672fa BG |
167 | (let ((name (if (stringp session) session (format "%s" session)))) |
168 | (if (and (string= "*" (substring name 0 1)) | |
169 | (string= "*" (substring name (- (length name) 1)))) | |
170 | (substring name 1 (- (length name) 1)) | |
171 | name))) | |
172 | ||
3ab2c837 | 173 | (defvar py-default-interpreter) |
666ffc7e SM |
174 | (defvar py-which-bufname) |
175 | (defvar python-shell-buffer-name) | |
86fbb8ca CD |
176 | (defun org-babel-python-initiate-session-by-key (&optional session) |
177 | "Initiate a python session. | |
178 | If there is not a current inferior-process-buffer in SESSION | |
179 | then create. Return the initialized session." | |
180 | (require org-babel-python-mode) | |
181 | (save-window-excursion | |
182 | (let* ((session (if session (intern session) :default)) | |
271672fa BG |
183 | (python-buffer (org-babel-python-session-buffer session)) |
184 | (cmd (if (member system-type '(cygwin windows-nt ms-dos)) | |
185 | (concat org-babel-python-command " -i") | |
186 | org-babel-python-command))) | |
86fbb8ca | 187 | (cond |
afe98dfa | 188 | ((and (eq 'python org-babel-python-mode) |
86fbb8ca | 189 | (fboundp 'run-python)) ; python.el |
271672fa BG |
190 | (if (not (version< "24.1" emacs-version)) |
191 | (run-python cmd) | |
192 | (unless python-buffer | |
d1389828 | 193 | (setq python-buffer (org-babel-python-with-earmuffs session))) |
271672fa | 194 | (let ((python-shell-buffer-name |
d1389828 | 195 | (org-babel-python-without-earmuffs python-buffer))) |
271672fa | 196 | (run-python cmd)))) |
afe98dfa | 197 | ((and (eq 'python-mode org-babel-python-mode) |
86fbb8ca | 198 | (fboundp 'py-shell)) ; python-mode.el |
3ab2c837 BG |
199 | ;; Make sure that py-which-bufname is initialized, as otherwise |
200 | ;; it will be overwritten the first time a Python buffer is | |
201 | ;; created. | |
202 | (py-toggle-shells py-default-interpreter) | |
86fbb8ca CD |
203 | ;; `py-shell' creates a buffer whose name is the value of |
204 | ;; `py-which-bufname' with '*'s at the beginning and end | |
3ab2c837 | 205 | (let* ((bufname (if (and python-buffer (buffer-live-p python-buffer)) |
86fbb8ca CD |
206 | (replace-regexp-in-string ;; zap surrounding * |
207 | "^\\*\\([^*]+\\)\\*$" "\\1" python-buffer) | |
208 | (concat "Python-" (symbol-name session)))) | |
209 | (py-which-bufname bufname)) | |
210 | (py-shell) | |
d1389828 | 211 | (setq python-buffer (org-babel-python-with-earmuffs bufname)))) |
86fbb8ca | 212 | (t |
8223b1d2 | 213 | (error "No function available for running an inferior Python"))) |
86fbb8ca CD |
214 | (setq org-babel-python-buffers |
215 | (cons (cons session python-buffer) | |
216 | (assq-delete-all session org-babel-python-buffers))) | |
217 | session))) | |
218 | ||
219 | (defun org-babel-python-initiate-session (&optional session params) | |
220 | "Create a session named SESSION according to PARAMS." | |
221 | (unless (string= session "none") | |
222 | (org-babel-python-session-buffer | |
223 | (org-babel-python-initiate-session-by-key session)))) | |
224 | ||
225 | (defvar org-babel-python-eoe-indicator "'org_babel_python_eoe'" | |
226 | "A string to indicate that evaluation has completed.") | |
227 | (defvar org-babel-python-wrapper-method | |
228 | " | |
229 | def main(): | |
230 | %s | |
231 | ||
232 | open('%s', 'w').write( str(main()) )") | |
233 | (defvar org-babel-python-pp-wrapper-method | |
234 | " | |
235 | import pprint | |
236 | def main(): | |
237 | %s | |
238 | ||
239 | open('%s', 'w').write( pprint.pformat(main()) )") | |
240 | ||
241 | (defun org-babel-python-evaluate | |
acedf35c | 242 | (session body &optional result-type result-params preamble) |
8223b1d2 | 243 | "Evaluate BODY as Python code." |
afe98dfa CD |
244 | (if session |
245 | (org-babel-python-evaluate-session | |
246 | session body result-type result-params) | |
247 | (org-babel-python-evaluate-external-process | |
acedf35c | 248 | body result-type result-params preamble))) |
afe98dfa CD |
249 | |
250 | (defun org-babel-python-evaluate-external-process | |
acedf35c | 251 | (body &optional result-type result-params preamble) |
afe98dfa CD |
252 | "Evaluate BODY in external python process. |
253 | If RESULT-TYPE equals 'output then return standard output as a | |
8223b1d2 | 254 | string. If RESULT-TYPE equals 'value then return the value of the |
afe98dfa | 255 | last statement in BODY, as elisp." |
666ffc7e SM |
256 | (let ((raw |
257 | (case result-type | |
258 | (output (org-babel-eval org-babel-python-command | |
259 | (concat (if preamble (concat preamble "\n")) | |
260 | body))) | |
261 | (value (let ((tmp-file (org-babel-temp-file "python-"))) | |
262 | (org-babel-eval | |
263 | org-babel-python-command | |
264 | (concat | |
265 | (if preamble (concat preamble "\n") "") | |
266 | (format | |
267 | (if (member "pp" result-params) | |
268 | org-babel-python-pp-wrapper-method | |
269 | org-babel-python-wrapper-method) | |
270 | (mapconcat | |
271 | (lambda (line) (format "\t%s" line)) | |
272 | (split-string | |
273 | (org-remove-indentation | |
274 | (org-babel-trim body)) | |
275 | "[\r\n]") "\n") | |
276 | (org-babel-process-file-name tmp-file 'noquote)))) | |
277 | (org-babel-eval-read-file tmp-file)))))) | |
278 | (org-babel-result-cond result-params | |
279 | raw | |
280 | (org-babel-python-table-or-string (org-babel-trim raw))))) | |
afe98dfa CD |
281 | |
282 | (defun org-babel-python-evaluate-session | |
666ffc7e | 283 | (session body &optional result-type result-params) |
afe98dfa CD |
284 | "Pass BODY to the Python process in SESSION. |
285 | If RESULT-TYPE equals 'output then return standard output as a | |
8223b1d2 | 286 | string. If RESULT-TYPE equals 'value then return the value of the |
afe98dfa | 287 | last statement in BODY, as elisp." |
8223b1d2 | 288 | (let* ((send-wait (lambda () (comint-send-input nil t) (sleep-for 0 5))) |
3ab2c837 | 289 | (dump-last-value |
8223b1d2 BG |
290 | (lambda |
291 | (tmp-file pp) | |
292 | (mapc | |
293 | (lambda (statement) (insert statement) (funcall send-wait)) | |
294 | (if pp | |
295 | (list | |
296 | "import pprint" | |
297 | (format "open('%s', 'w').write(pprint.pformat(_))" | |
298 | (org-babel-process-file-name tmp-file 'noquote))) | |
299 | (list (format "open('%s', 'w').write(str(_))" | |
666ffc7e SM |
300 | (org-babel-process-file-name tmp-file |
301 | 'noquote))))))) | |
8223b1d2 BG |
302 | (input-body (lambda (body) |
303 | (mapc (lambda (line) (insert line) (funcall send-wait)) | |
304 | (split-string body "[\r\n]")) | |
666ffc7e SM |
305 | (funcall send-wait))) |
306 | (results | |
307 | (case result-type | |
308 | (output | |
309 | (mapconcat | |
310 | #'org-babel-trim | |
311 | (butlast | |
312 | (org-babel-comint-with-output | |
313 | (session org-babel-python-eoe-indicator t body) | |
314 | (funcall input-body body) | |
315 | (funcall send-wait) (funcall send-wait) | |
316 | (insert org-babel-python-eoe-indicator) | |
317 | (funcall send-wait)) | |
318 | 2) "\n")) | |
319 | (value | |
320 | (let ((tmp-file (org-babel-temp-file "python-"))) | |
321 | (org-babel-comint-with-output | |
322 | (session org-babel-python-eoe-indicator nil body) | |
323 | (let ((comint-process-echoes nil)) | |
324 | (funcall input-body body) | |
325 | (funcall dump-last-value tmp-file | |
326 | (member "pp" result-params)) | |
327 | (funcall send-wait) (funcall send-wait) | |
328 | (insert org-babel-python-eoe-indicator) | |
329 | (funcall send-wait))) | |
330 | (org-babel-eval-read-file tmp-file)))))) | |
331 | (unless (string= (substring org-babel-python-eoe-indicator 1 -1) results) | |
332 | (org-babel-result-cond result-params | |
333 | results | |
334 | (org-babel-python-table-or-string results))))) | |
86fbb8ca CD |
335 | |
336 | (defun org-babel-python-read-string (string) | |
8223b1d2 | 337 | "Strip 's from around Python string." |
86fbb8ca CD |
338 | (if (string-match "^'\\([^\000]+\\)'$" string) |
339 | (match-string 1 string) | |
340 | string)) | |
341 | ||
342 | (provide 'ob-python) | |
343 | ||
5b409b39 | 344 | |
86fbb8ca CD |
345 | |
346 | ;;; ob-python.el ends here |