org-babel-python.el 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. ;;; org-babel-python.el --- org-babel functions for python evaluation
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating python source code.
  24. ;;; Code:
  25. (require 'org-babel)
  26. (require 'python)
  27. (org-babel-add-interpreter "python")
  28. (add-to-list 'org-babel-tangle-langs '("python" "py" "#!/usr/bin/env python"))
  29. (defun org-babel-execute:python (body params)
  30. "Execute a block of Python code with org-babel. This function is
  31. called by `org-babel-execute-src-block'."
  32. (message "executing Python source code block")
  33. (let* ((processed-params (org-babel-process-params params))
  34. (session (org-babel-python-initiate-session (first processed-params)))
  35. (vars (second processed-params))
  36. (result-params (third processed-params))
  37. (result-type (fourth processed-params))
  38. (full-body (concat
  39. (mapconcat ;; define any variables
  40. (lambda (pair)
  41. (format "%s=%s"
  42. (car pair)
  43. (org-babel-python-var-to-python (cdr pair))))
  44. vars "\n") "\n" (org-babel-trim body) "\n")) ;; then the source block body
  45. (result (org-babel-python-evaluate session full-body result-type)))
  46. (or (cdr (assoc :file params))
  47. (org-babel-reassemble-table
  48. result
  49. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  50. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params)))))))
  51. (defun org-babel-prep-session:python (session params)
  52. "Prepare SESSION according to the header arguments specified in PARAMS."
  53. (let* ((session (org-babel-python-initiate-session session))
  54. (vars (org-babel-ref-variables params))
  55. (var-lines (mapcar ;; define any variables
  56. (lambda (pair)
  57. (format "%s=%s"
  58. (car pair)
  59. (org-babel-python-var-to-python (cdr pair))))
  60. vars)))
  61. (org-babel-comint-in-buffer session
  62. (mapc (lambda (var)
  63. (move-end-of-line 1) (insert var) (comint-send-input nil t)
  64. (org-babel-comint-wait-for-output session)) var-lines))
  65. session))
  66. (defun org-babel-load-session:python (session body params)
  67. "Load BODY into SESSION."
  68. (save-window-excursion
  69. (let ((buffer (org-babel-prep-session:python session params)))
  70. (with-current-buffer buffer
  71. (goto-char (process-mark (get-buffer-process (current-buffer))))
  72. (insert (org-babel-chomp body)))
  73. buffer)))
  74. ;; helper functions
  75. (defun org-babel-python-var-to-python (var)
  76. "Convert an elisp var into a string of python source code
  77. specifying a var of the same value."
  78. (if (listp var)
  79. (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]")
  80. (format "%S" var)))
  81. (defun org-babel-python-table-or-string (results)
  82. "If the results look like a list or tuple, then convert them into an
  83. Emacs-lisp table, otherwise return the results as a string."
  84. (org-babel-read
  85. (if (or (string-match "^\\[.+\\]$" results)
  86. (string-match "^(.+)$" results))
  87. (org-babel-read
  88. (concat "'"
  89. (replace-regexp-in-string
  90. "\\[" "(" (replace-regexp-in-string
  91. "\\]" ")" (replace-regexp-in-string
  92. ", " " " (replace-regexp-in-string
  93. "'" "\"" results))))))
  94. results)))
  95. (defvar org-babel-python-buffers '(:default . nil))
  96. (defun org-babel-python-session-buffer (session)
  97. (cdr (assoc session org-babel-python-buffers)))
  98. (defun org-babel-python-initiate-session-by-key (&optional session)
  99. "If there is not a current inferior-process-buffer in SESSION
  100. then create. Return the initialized session."
  101. (save-window-excursion
  102. (let* ((session (if session (intern session) :default))
  103. (python-buffer (org-babel-python-session-buffer session)))
  104. (run-python)
  105. (setq org-babel-python-buffers (cons (cons session python-buffer)
  106. (assq-delete-all session org-babel-python-buffers)))
  107. session)))
  108. (defun org-babel-python-initiate-session (&optional session params)
  109. (unless (string= session "none")
  110. (org-babel-python-session-buffer (org-babel-python-initiate-session-by-key session))))
  111. (defvar org-babel-python-last-value-eval "_"
  112. "When evaluated by Python this returns the return value of the last statement.")
  113. (defvar org-babel-python-pp-last-value-eval
  114. '("results = _"
  115. "import pprint"
  116. "org_babel_pp = pprint.PrettyPrinter()"
  117. "org_babel_pp.pprint(results)")
  118. "When evaluated by Python this pretty prints the value of the last statement.")
  119. (defvar org-babel-python-eoe-indicator "'org_babel_python_eoe'"
  120. "Used to indicate that evaluation is has completed.")
  121. (defvar org-babel-python-wrapper-method
  122. "
  123. def main():
  124. %s
  125. open('%s', 'w').write( str(main()) )")
  126. (defvar org-babel-python-pp-wrapper-method
  127. "
  128. import pprint
  129. def main():
  130. %s
  131. open('%s', 'w').write( pprint.pformat(main()) )")
  132. (defun org-babel-python-evaluate (buffer body &optional result-type)
  133. "Pass BODY to the Python process in BUFFER. If RESULT-TYPE equals
  134. 'output then return a list of the outputs of the statements in
  135. BODY, if RESULT-TYPE equals 'value then return the value of the
  136. last statement in BODY, as elisp."
  137. (if (not session)
  138. ;; external process evaluation
  139. (save-excursion
  140. (case result-type
  141. (output
  142. (with-temp-buffer
  143. (insert body)
  144. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  145. (org-babel-shell-command-on-region (point-min) (point-max) "python" 'current-buffer 'replace)
  146. (buffer-string)))
  147. (value
  148. (let* ((tmp-file (make-temp-file "org-babel-python-results-")) exit-code
  149. (stderr
  150. (with-temp-buffer
  151. (insert
  152. (format
  153. (if (member "pp" result-params)
  154. org-babel-python-pp-wrapper-method
  155. org-babel-python-wrapper-method)
  156. (mapconcat
  157. (lambda (line) (format "\t%s" line))
  158. (split-string
  159. (org-remove-indentation (org-babel-trim body)) "[\r\n]") "\n")
  160. tmp-file))
  161. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  162. (setq exit-code (org-babel-shell-command-on-region
  163. (point-min) (point-max) "python" nil 'replace (current-buffer)))
  164. (buffer-string))))
  165. (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
  166. (let ((raw (with-temp-buffer
  167. (insert-file-contents (org-babel-maybe-remote-file tmp-file))
  168. (buffer-string))))
  169. (if (or (member "code" result-params) (member "pp" result-params))
  170. raw
  171. (org-babel-python-table-or-string raw)))))))
  172. ;; comint session evaluation
  173. (org-babel-comint-in-buffer buffer
  174. (let* ((raw (org-babel-comint-with-output buffer org-babel-python-eoe-indicator t
  175. ;; for some reason python is fussy, and likes enters after every input
  176. (mapc (lambda (statement) (insert statement) (comint-send-input nil t))
  177. (split-string (org-babel-trim full-body) "[\r\n]+"))
  178. (comint-send-input nil t) (comint-send-input nil t)
  179. (if (member "pp" result-params)
  180. (mapc (lambda (statement) (insert statement) (comint-send-input nil t))
  181. org-babel-python-pp-last-value-eval)
  182. (insert org-babel-python-last-value-eval))
  183. (comint-send-input nil t) (comint-send-input nil t)
  184. (insert org-babel-python-eoe-indicator)
  185. (comint-send-input nil t)))
  186. (results (delete org-babel-python-eoe-indicator
  187. (cdr (member org-babel-python-eoe-indicator
  188. (reverse (mapcar #'org-babel-trim raw)))))))
  189. (unless (or (member "code" result-params) (member "pp" result-params))
  190. (setq results (mapcar #'org-babel-python-read-string results)))
  191. (case result-type
  192. (output (org-babel-trim (mapconcat #'identity (reverse (cdr results)) "\n")))
  193. (value
  194. (if (or (member "code" result-params) (member "pp" result-params))
  195. (car results)
  196. (org-babel-python-table-or-string (org-babel-trim (car results))))))))))
  197. (defun org-babel-python-read-string (string)
  198. "Strip 's from around python string"
  199. (if (string-match "^'\\([^\000]+\\)'$" string)
  200. (match-string 1 string)
  201. string))
  202. (provide 'org-babel-python)
  203. ;;; org-babel-python.el ends here