org-babel-python.el 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. ;;; org-babel-python.el --- org-babel functions for python
  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. (defun org-babel-execute:python (body params)
  29. "Execute a block of Python code with org-babel. This function is
  30. called by `org-babel-execute-src-block'."
  31. (message "executing Python source code block")
  32. (let* ((vars (org-babel-ref-variables params))
  33. (result-params (split-string (or (cdr (assoc :results params)) "")))
  34. (result-type (cond ((member "output" result-params) 'output)
  35. ((member "value" result-params) 'value)
  36. (t 'value)))
  37. (full-body (concat
  38. (mapconcat ;; define any variables
  39. (lambda (pair)
  40. (format "%s=%s"
  41. (car pair)
  42. (org-babel-python-var-to-python (cdr pair))))
  43. vars "\n") "\n" body "\n")) ;; then the source block body
  44. (session (org-babel-python-initiate-session (cdr (assoc :session params))))
  45. (results (org-babel-python-evaluate session full-body result-type)))
  46. (if (member "scalar" result-params)
  47. results
  48. (setq results (case result-type ;; process results based on the result-type
  49. ('output (let ((tmp-file (make-temp-file "org-babel-python")))
  50. (with-temp-file tmp-file (insert results))
  51. (org-babel-import-elisp-from-file tmp-file)))
  52. ('value (org-babel-python-table-or-results results))))
  53. (if (and (member "vector" results) (not (listp results)))
  54. (list (list results))
  55. results))))
  56. (defun org-babel-python-var-to-python (var)
  57. "Convert an elisp var into a string of python or python source
  58. code specifying a var of the same value."
  59. (if (listp var)
  60. (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]")
  61. (format "%S" var)))
  62. (defun org-babel-python-table-or-results (results)
  63. "If the results look like a table, then convert them into an
  64. Emacs-lisp table, otherwise return the results as a string."
  65. (org-babel-read
  66. (if (string-match "^\\[.+\\]$" results)
  67. (org-babel-read
  68. (replace-regexp-in-string
  69. "\\[" "(" (replace-regexp-in-string
  70. "\\]" ")" (replace-regexp-in-string
  71. ", " " " (replace-regexp-in-string
  72. "'" "\"" results)))))
  73. results)))
  74. ;; functions for comint evaluation
  75. (defun org-babel-python-initiate-session (&optional session)
  76. "If there is not a current inferior-process-buffer in SESSION
  77. then create. Return the initialized session."
  78. (let ((python-buffer (or session python-buffer)))
  79. (save-window-excursion (run-python nil session) (current-buffer))))
  80. (defvar org-babel-python-last-value-eval "_"
  81. "When evaluated by Python this returns the return value of the last statement.")
  82. (defvar org-babel-python-eoe-indicator "'org_babel_python_eoe'"
  83. "Used to indicate that evaluation is has completed.")
  84. (defun org-babel-python-evaluate (buffer body &optional result-type)
  85. "Pass BODY to the Python process in BUFFER. If RESULT-TYPE equals
  86. 'output then return a list of the outputs of the statements in
  87. BODY, if RESULT-TYPE equals 'value then return the value of the
  88. last statement in BODY."
  89. (org-babel-comint-in-buffer buffer
  90. (let ((string-buffer "")
  91. (full-body (mapconcat #'org-babel-chomp
  92. (list body org-babel-python-last-value-eval org-babel-python-eoe-indicator) "\n"))
  93. results)
  94. (flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
  95. ;; setup filter
  96. (add-hook 'comint-output-filter-functions 'my-filt)
  97. ;; pass FULL-BODY to process
  98. (goto-char (process-mark (get-buffer-process buffer)))
  99. (insert full-body)
  100. (comint-send-input)
  101. ;; wait for end-of-evaluation indicator
  102. (while (progn
  103. (goto-char comint-last-input-end)
  104. (not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
  105. (re-search-forward (regexp-quote org-babel-python-eoe-indicator) nil t)))))
  106. (accept-process-output (get-buffer-process buffer)))
  107. ;; remove filter
  108. (remove-hook 'comint-output-filter-functions 'my-filt))
  109. ;; remove echo'd FULL-BODY from input
  110. (if (string-match (replace-regexp-in-string "\n" "\r\n" (regexp-quote full-body)) string-buffer)
  111. (setq string-buffer (substring string-buffer (match-end 0))))
  112. ;; split results with `comint-prompt-regexp'
  113. (setq results (cdr (member org-babel-python-eoe-indicator
  114. (reverse (mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp))))))
  115. (org-babel-trim (case result-type
  116. (output (mapconcat #'identity (reverse (cdr results)) "\n"))
  117. (value (car results))
  118. (t (reverse results)))))))
  119. (provide 'org-babel-python)
  120. ;;; org-babel-python.el ends here