1 ;;; ob-python.el --- org-babel functions for python evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
27 ;; Org-Babel support for evaluating python source code.
34 (eval-when-compile (require 'cl
))
36 (declare-function org-remove-indentation
"org" )
37 (declare-function py-shell
"ext:python-mode" (&optional argprompt
))
38 (declare-function run-python
"ext:python" (&optional cmd noshow new
))
40 (add-to-list 'org-babel-tangle-lang-exts
'("python" .
"py"))
42 (defvar org-babel-default-header-args
:python
'())
44 (defvar org-babel-python-command
"python"
45 "Name of command for executing python code.")
47 (defvar org-babel-python-mode
(if (featurep 'xemacs
) 'python-mode
'python
)
48 "Preferred python mode for use in running python interactively.")
50 (defvar org-src-preserve-indentation
)
51 (defun org-babel-expand-body:python
(body params
&optional processed-params
)
52 "Expand BODY according to PARAMS, return the expanded body."
54 (org-babel-python-variable-assignments params processed-params
)))
58 (org-babel-python-variable-assignments params processed-params
)
60 (org-babel-trim body
(if org-src-preserve-indentation
"[\f\n\r\v]"))))
63 (defun org-babel-execute:python
(body params
)
64 "Execute a block of Python code with Babel.
65 This function is called by `org-babel-execute-src-block'."
66 (let* ((processed-params (org-babel-process-params params
))
67 (session (org-babel-python-initiate-session (first processed-params
)))
68 (result-params (nth 2 processed-params
))
69 (result-type (nth 3 processed-params
))
70 (full-body (org-babel-expand-body:python
71 body params processed-params
))
72 (result (org-babel-python-evaluate
73 session full-body result-type result-params
)))
74 (or (cdr (assoc :file params
))
75 (org-babel-reassemble-table
77 (org-babel-pick-name (nth 4 processed-params
)
78 (cdr (assoc :colnames params
)))
79 (org-babel-pick-name (nth 5 processed-params
)
80 (cdr (assoc :rownames params
)))))))
82 (defun org-babel-prep-session:python
(session params
)
83 "Prepare SESSION according to the header arguments in PARAMS."
84 (let* ((session (org-babel-python-initiate-session session
))
85 (var-lines (org-babel-python-variable-assignments params
)))
86 (org-babel-comint-in-buffer session
88 (end-of-line 1) (insert var
) (comint-send-input)
89 (org-babel-comint-wait-for-output session
)) var-lines
))
92 (defun org-babel-load-session:python
(session body params
)
93 "Load BODY into SESSION."
94 (save-window-excursion
95 (let ((buffer (org-babel-prep-session:python session params
)))
96 (with-current-buffer buffer
97 (goto-char (process-mark (get-buffer-process (current-buffer))))
98 (insert (org-babel-chomp body
)))
103 (defun org-babel-python-variable-assignments (params &optional processed-params
)
104 "Return list of python statements assigning the block's variables"
109 (org-babel-python-var-to-python (cdr pair
))))
110 (nth 1 (or processed-params
(org-babel-process-params params
)))))
112 (defun org-babel-python-var-to-python (var)
113 "Convert an elisp value to a python variable.
114 Convert an elisp value, VAR, into a string of python source code
115 specifying a variable of the same value."
117 (concat "[" (mapconcat #'org-babel-python-var-to-python var
", ") "]")
118 (if (equal var
'hline
)
121 (if (and (stringp var
) (string-match "[\n\r]" var
)) "\"\"%S\"\"" "%S")
124 (defun org-babel-python-table-or-string (results)
125 "Convert RESULTS into an appropriate elisp value.
126 If the results look like a list or tuple, then convert them into an
127 Emacs-lisp table, otherwise return the results as a string."
130 (mapcar (lambda (el) (if (equal el
'None
) 'hline el
)) res
)
133 (if (and (stringp results
) (string-match "^[([].+[])]$" results
))
136 (replace-regexp-in-string
137 "\\[" "(" (replace-regexp-in-string
138 "\\]" ")" (replace-regexp-in-string
139 ", " " " (replace-regexp-in-string
140 "'" "\"" results t
))))))
143 (defvar org-babel-python-buffers
'((:default . nil
)))
145 (defun org-babel-python-session-buffer (session)
146 "Return the buffer associated with SESSION."
147 (cdr (assoc session org-babel-python-buffers
)))
149 (defun org-babel-python-initiate-session-by-key (&optional session
)
150 "Initiate a python session.
151 If there is not a current inferior-process-buffer in SESSION
152 then create. Return the initialized session."
153 (require org-babel-python-mode
)
154 (save-window-excursion
155 (let* ((session (if session
(intern session
) :default
))
156 (python-buffer (org-babel-python-session-buffer session
)))
158 ((and (eq 'python org-babel-python-mode
)
159 (fboundp 'run-python
)) ; python.el
161 ((and (eq 'python-mode org-babel-python-mode
)
162 (fboundp 'py-shell
)) ; python-mode.el
163 ;; `py-shell' creates a buffer whose name is the value of
164 ;; `py-which-bufname' with '*'s at the beginning and end
165 (let* ((bufname (if python-buffer
166 (replace-regexp-in-string ;; zap surrounding *
167 "^\\*\\([^*]+\\)\\*$" "\\1" python-buffer
)
168 (concat "Python-" (symbol-name session
))))
169 (py-which-bufname bufname
))
171 (setq python-buffer
(concat "*" bufname
"*"))))
173 (error "No function available for running an inferior python.")))
174 (setq org-babel-python-buffers
175 (cons (cons session python-buffer
)
176 (assq-delete-all session org-babel-python-buffers
)))
179 (defun org-babel-python-initiate-session (&optional session params
)
180 "Create a session named SESSION according to PARAMS."
181 (unless (string= session
"none")
182 (org-babel-python-session-buffer
183 (org-babel-python-initiate-session-by-key session
))))
185 (defvar org-babel-python-eoe-indicator
"'org_babel_python_eoe'"
186 "A string to indicate that evaluation has completed.")
187 (defvar org-babel-python-wrapper-method
192 open('%s', 'w').write( str(main()) )")
193 (defvar org-babel-python-pp-wrapper-method
199 open('%s', 'w').write( pprint.pformat(main()) )")
201 (defun org-babel-python-evaluate
202 (session body
&optional result-type result-params
)
203 "Evaluate BODY as python code."
205 (org-babel-python-evaluate-session
206 session body result-type result-params
)
207 (org-babel-python-evaluate-external-process
208 body result-type result-params
)))
210 (defun org-babel-python-evaluate-external-process
211 (body &optional result-type result-params
)
212 "Evaluate BODY in external python process.
213 If RESULT-TYPE equals 'output then return standard output as a
214 string. If RESULT-TYPE equals 'value then return the value of the
215 last statement in BODY, as elisp."
217 (output (org-babel-eval org-babel-python-command body
))
218 (value (let ((tmp-file (org-babel-temp-file "python-")))
219 (org-babel-eval org-babel-python-command
221 (if (member "pp" result-params
)
222 org-babel-python-pp-wrapper-method
223 org-babel-python-wrapper-method
)
225 (lambda (line) (format "\t%s" line
))
227 (org-remove-indentation
228 (org-babel-trim body
))
230 (org-babel-process-file-name tmp-file
'noquote
)))
232 (if (or (member "code" result-params
)
233 (member "pp" result-params
))
235 (org-babel-python-table-or-string raw
)))
236 (org-babel-eval-read-file tmp-file
))))))
238 (defun org-babel-python-evaluate-session
239 (session body
&optional result-type result-params
)
240 "Pass BODY to the Python process in SESSION.
241 If RESULT-TYPE equals 'output then return standard output as a
242 string. If RESULT-TYPE equals 'value then return the value of the
243 last statement in BODY, as elisp."
244 (flet ((dump-last-value
247 (lambda (statement) (insert statement
) (comint-send-input))
251 (format "open('%s', 'w').write(pprint.pformat(_))"
252 (org-babel-process-file-name tmp-file
'noquote
)))
253 (list (format "open('%s', 'w').write(str(_))"
254 (org-babel-process-file-name tmp-file
'noquote
))))))
256 (mapc (lambda (statement) (insert statement
) (comint-send-input))
257 (split-string (org-babel-trim body
) "[\r\n]+"))
258 (comint-send-input) (comint-send-input)))
264 (org-babel-comint-with-output
265 (session org-babel-python-eoe-indicator t body
)
266 (let ((comint-process-echoes nil
))
268 (insert org-babel-python-eoe-indicator
)
269 (comint-send-input))) 2) "\n"))
272 (if (or (member "code" result-params
) (member "pp" result-params
))
274 (org-babel-python-table-or-string results
)))
275 (let ((tmp-file (org-babel-temp-file "python-")))
276 (org-babel-comint-with-output
277 (session org-babel-python-eoe-indicator t body
)
278 (let ((comint-process-echoes nil
))
280 (dump-last-value tmp-file
(member "pp" result-params
))
281 (comint-send-input) (comint-send-input)
282 (insert org-babel-python-eoe-indicator
)
283 (comint-send-input)))
284 (org-babel-eval-read-file tmp-file
)))))))
286 (defun org-babel-python-read-string (string)
287 "Strip 's from around python string"
288 (if (string-match "^'\\([^\000]+\\)'$" string
)
289 (match-string 1 string
)
294 ;; arch-tag: f19b6c3d-dfcb-4a1a-9ce0-45ade1ebc212
296 ;;; ob-python.el ends here