org-babel-python.el 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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-expand-body:python (body params &optional processed-params)
  30. (concat
  31. (mapconcat ;; define any variables
  32. (lambda (pair)
  33. (format "%s=%s"
  34. (car pair)
  35. (org-babel-python-var-to-python (cdr pair))))
  36. (second (or processed-params (org-babel-process-params params))) "\n")
  37. "\n" (org-babel-trim body) "\n"))
  38. (defun org-babel-execute:python (body params)
  39. "Execute a block of Python code with org-babel. This function is
  40. called by `org-babel-execute-src-block'."
  41. (message "executing Python source code block")
  42. (let* ((processed-params (org-babel-process-params params))
  43. (session (org-babel-python-initiate-session (first processed-params)))
  44. (result-params (third processed-params))
  45. (result-type (fourth processed-params))
  46. (full-body (org-babel-expand-body:python
  47. body params processed-params)) ;; then the source block body
  48. (result (org-babel-python-evaluate session full-body result-type)))
  49. (or (cdr (assoc :file params))
  50. (org-babel-reassemble-table
  51. result
  52. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  53. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params)))))))
  54. (defun org-babel-prep-session:python (session params)
  55. "Prepare SESSION according to the header arguments specified in PARAMS."
  56. (let* ((session (org-babel-python-initiate-session session))
  57. (vars (org-babel-ref-variables params))
  58. (var-lines (mapcar ;; define any variables
  59. (lambda (pair)
  60. (format "%s=%s"
  61. (car pair)
  62. (org-babel-python-var-to-python (cdr pair))))
  63. vars)))
  64. (org-babel-comint-in-buffer session
  65. (mapc (lambda (var)
  66. (move-end-of-line 1) (insert var) (comint-send-input nil t)
  67. (org-babel-comint-wait-for-output session)) var-lines))
  68. session))
  69. (defun org-babel-load-session:python (session body params)
  70. "Load BODY into SESSION."
  71. (save-window-excursion
  72. (let ((buffer (org-babel-prep-session:python session params)))
  73. (with-current-buffer buffer
  74. (goto-char (process-mark (get-buffer-process (current-buffer))))
  75. (insert (org-babel-chomp body)))
  76. buffer)))
  77. ;; helper functions
  78. (defun org-babel-python-var-to-python (var)
  79. "Convert an elisp var into a string of python source code
  80. specifying a var of the same value."
  81. (if (listp var)
  82. (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]")
  83. (format "%S" var)))
  84. (defun org-babel-python-table-or-string (results)
  85. "If the results look like a list or tuple, then convert them into an
  86. Emacs-lisp table, otherwise return the results as a string."
  87. (org-babel-read
  88. (if (or (string-match "^\\[.+\\]$" results)
  89. (string-match "^(.+)$" results))
  90. (org-babel-read
  91. (concat "'"
  92. (replace-regexp-in-string
  93. "\\[" "(" (replace-regexp-in-string
  94. "\\]" ")" (replace-regexp-in-string
  95. ", " " " (replace-regexp-in-string
  96. "'" "\"" results))))))
  97. results)))
  98. (defvar org-babel-python-buffers '(:default . nil))
  99. (defun org-babel-python-session-buffer (session)
  100. (cdr (assoc session org-babel-python-buffers)))
  101. (defun org-babel-python-initiate-session-by-key (&optional session)
  102. "If there is not a current inferior-process-buffer in SESSION
  103. then create. Return the initialized session."
  104. (save-window-excursion
  105. (let* ((session (if session (intern session) :default))
  106. (python-buffer (org-babel-python-session-buffer session)))
  107. (run-python)
  108. (setq org-babel-python-buffers (cons (cons session python-buffer)
  109. (assq-delete-all session org-babel-python-buffers)))
  110. session)))
  111. (defun org-babel-python-initiate-session (&optional session params)
  112. (unless (string= session "none")
  113. (org-babel-python-session-buffer (org-babel-python-initiate-session-by-key session))))
  114. (defvar org-babel-python-last-value-eval "_"
  115. "When evaluated by Python this returns the return value of the last statement.")
  116. (defvar org-babel-python-pp-last-value-eval
  117. '("results = _"
  118. "import pprint"
  119. "org_babel_pp = pprint.PrettyPrinter()"
  120. "org_babel_pp.pprint(results)")
  121. "When evaluated by Python this pretty prints the value of the last statement.")
  122. (defvar org-babel-python-eoe-indicator "'org_babel_python_eoe'"
  123. "Used to indicate that evaluation is has completed.")
  124. (defvar org-babel-python-wrapper-method
  125. "
  126. def main():
  127. %s
  128. open('%s', 'w').write( str(main()) )")
  129. (defvar org-babel-python-pp-wrapper-method
  130. "
  131. import pprint
  132. def main():
  133. %s
  134. open('%s', 'w').write( pprint.pformat(main()) )")
  135. (defun org-babel-python-evaluate (buffer body &optional result-type)
  136. "Pass BODY to the Python process in BUFFER. If RESULT-TYPE equals
  137. 'output then return a list of the outputs of the statements in
  138. BODY, if RESULT-TYPE equals 'value then return the value of the
  139. last statement in BODY, as elisp."
  140. (if (not session)
  141. ;; external process evaluation
  142. (save-excursion
  143. (case result-type
  144. (output
  145. (with-temp-buffer
  146. (insert body)
  147. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  148. (org-babel-shell-command-on-region (point-min) (point-max) "python" 'current-buffer 'replace)
  149. (buffer-string)))
  150. (value
  151. (let* ((tmp-file (make-temp-file "org-babel-python-results-")) exit-code
  152. (stderr
  153. (with-temp-buffer
  154. (insert
  155. (format
  156. (if (member "pp" result-params)
  157. org-babel-python-pp-wrapper-method
  158. org-babel-python-wrapper-method)
  159. (mapconcat
  160. (lambda (line) (format "\t%s" line))
  161. (split-string
  162. (org-remove-indentation (org-babel-trim body)) "[\r\n]") "\n")
  163. tmp-file))
  164. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  165. (setq exit-code (org-babel-shell-command-on-region
  166. (point-min) (point-max) "python" nil 'replace (current-buffer)))
  167. (buffer-string))))
  168. (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
  169. (let ((raw (with-temp-buffer
  170. (insert-file-contents (org-babel-maybe-remote-file tmp-file))
  171. (buffer-string))))
  172. (if (or (member "code" result-params) (member "pp" result-params))
  173. raw
  174. (org-babel-python-table-or-string raw)))))))
  175. ;; comint session evaluation
  176. (org-babel-comint-in-buffer buffer
  177. (let* ((raw (org-babel-comint-with-output buffer org-babel-python-eoe-indicator t
  178. ;; for some reason python is fussy, and likes enters after every input
  179. (mapc (lambda (statement) (insert statement) (comint-send-input nil t))
  180. (split-string (org-babel-trim full-body) "[\r\n]+"))
  181. (comint-send-input nil t) (comint-send-input nil t)
  182. (if (member "pp" result-params)
  183. (mapc (lambda (statement) (insert statement) (comint-send-input nil t))
  184. org-babel-python-pp-last-value-eval)
  185. (insert org-babel-python-last-value-eval))
  186. (comint-send-input nil t) (comint-send-input nil t)
  187. (insert org-babel-python-eoe-indicator)
  188. (comint-send-input nil t)))
  189. (results (delete org-babel-python-eoe-indicator
  190. (cdr (member org-babel-python-eoe-indicator
  191. (reverse (mapcar #'org-babel-trim raw)))))))
  192. (unless (or (member "code" result-params) (member "pp" result-params))
  193. (setq results (mapcar #'org-babel-python-read-string results)))
  194. (case result-type
  195. (output (org-babel-trim (mapconcat #'identity (reverse (cdr results)) "\n")))
  196. (value
  197. (if (or (member "code" result-params) (member "pp" result-params))
  198. (car results)
  199. (org-babel-python-table-or-string (org-babel-trim (car results))))))))))
  200. (defun org-babel-python-read-string (string)
  201. "Strip 's from around python string"
  202. (if (string-match "^'\\([^\000]+\\)'$" string)
  203. (match-string 1 string)
  204. string))
  205. (provide 'org-babel-python)
  206. ;;; org-babel-python.el ends here