2015-10-26 00:56:00 +00:00
|
|
|
;;; ob-python.el --- Babel Functions for Python -*- lexical-binding: t; -*-
|
2009-06-12 15:46:00 +00:00
|
|
|
|
2024-01-02 01:47:10 +00:00
|
|
|
;; Copyright (C) 2009-2024 Free Software Foundation, Inc.
|
2009-06-12 15:46:00 +00:00
|
|
|
|
2012-04-01 22:53:28 +00:00
|
|
|
;; Authors: Eric Schulte
|
|
|
|
;; Dan Davison
|
2020-09-05 00:59:18 +00:00
|
|
|
;; Maintainer: Jack Kamm <jackkamm@gmail.com>
|
2009-06-12 15:46:00 +00:00
|
|
|
;; Keywords: literate programming, reproducible research
|
2021-09-26 07:44:29 +00:00
|
|
|
;; URL: https://orgmode.org
|
2009-06-12 15:46:00 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-06-12 15:46:00 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-06-12 15:46:00 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 16:32:35 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2009-06-12 15:46:00 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
2010-06-25 16:32:35 +00:00
|
|
|
|
2009-06-12 15:46:00 +00:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2009-06-12 15:46:00 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Org-Babel support for evaluating python source code.
|
|
|
|
|
|
|
|
;;; Code:
|
2022-08-04 13:53:05 +00:00
|
|
|
|
|
|
|
(require 'org-macs)
|
|
|
|
(org-assert-version)
|
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
(require 'ob)
|
2018-05-10 00:04:12 +00:00
|
|
|
(require 'org-macs)
|
2020-08-25 03:17:57 +00:00
|
|
|
(require 'python)
|
2010-07-03 02:21:31 +00:00
|
|
|
|
Add options to skip extra processing in org-babel-comint-with-output
This patch adds options to org-babel-comint-with-output to skip prompt
removal. Allowing individual languages to handle this cleanup can be
more robust than relying on the generic ob-comint implementation.
This allows ob-python to switch back to using
`org-babel-comint-with-output' rather than its own bespoke
reimplementation, reducing code duplication. Furthermore, this adds a
new implementation of ob-R non-async session output evaluation, that
is similar to the ob-python approach in that it avoids leaking
prompts, rather than relying on the cleanup from
`org-babel-comint-with-output'. A test is added to test-ob-R.el to
demonstrate the improved robustness of the new approach; previously,
this test would fail due to a false positive prompt, but now passes.
* lisp/ob-comint.el (org-babel-comint--remove-prompts-p): New helper
function to parse the prompt-handling argument in
`org-babel-comint-with-output' and `org-babel-comint-async-register'.
(org-babel-comint-with-output): Add a new argument
to prevent extra processing for prompt cleanup. Also, search for the
end-of-execution sentinel within the collected output rather than the
comint buffer, which doesn't depend on the position of point during
evaluation.
(org-babel-comint-async-register): Move parsing of prompt-handling
argument to `org-babel-comint--remove-prompts-p'.
* lisp/ob-python.el: Require subr-x for Emacs 27 compatibility.
(org-babel-python-send-string): Switch to using
`org-babel-comint-with-output', rather than bespoke reimplementation.
* lisp/ob-R.el: Require subr-x for Emacs 27 compatibility.
(ess-send-string): Declare external function.
(org-babel-R-evaluate-session): New implementation of output
evaluation that avoids leaking prompts, by writing the code block to a
tmp file and then sourcing it.
* testing/lisp/test-ob-R.el (test-ob-r/session-output-with->-bol): New
test for robustness against false positive prompts at the beginning of
a line.
2024-10-18 00:33:47 +00:00
|
|
|
(require 'subr-x) ; For `string-trim-right', Emacs < 28
|
|
|
|
|
2011-06-21 22:34:41 +00:00
|
|
|
(defvar org-babel-tangle-lang-exts)
|
2010-06-17 17:21:14 +00:00
|
|
|
(add-to-list 'org-babel-tangle-lang-exts '("python" . "py"))
|
|
|
|
|
2010-06-25 17:12:54 +00:00
|
|
|
(defvar org-babel-default-header-args:python '())
|
|
|
|
|
2023-10-17 08:13:59 +00:00
|
|
|
(defconst org-babel-header-args:python
|
2023-09-17 09:39:40 +00:00
|
|
|
'((return . :any)
|
2024-02-01 15:14:43 +00:00
|
|
|
(python . :any)
|
|
|
|
(async . ((yes no))))
|
2023-09-17 09:39:40 +00:00
|
|
|
"Python-specific header arguments.")
|
|
|
|
|
2023-12-29 21:22:18 +00:00
|
|
|
(defcustom org-babel-python-command 'auto
|
|
|
|
"Command (including arguments) for interactive and non-interactive Python code.
|
|
|
|
When not `auto', it overrides `org-babel-python-command-session'
|
|
|
|
and `org-babel-python-command-nonsession'."
|
|
|
|
:package-version '(Org . "9.7")
|
|
|
|
:group 'org-babel
|
|
|
|
:type '(choice string (const auto)))
|
|
|
|
|
|
|
|
(defcustom org-babel-python-command-session 'auto
|
|
|
|
"Command (including arguments) for starting interactive Python sessions.
|
|
|
|
If `auto' (the default), uses the values from
|
|
|
|
`python-shell-interpreter' and `python-shell-interpreter-args'.
|
|
|
|
If `org-babel-python-command' is set, then it overrides this
|
|
|
|
option."
|
|
|
|
:package-version '(Org . "9.7")
|
|
|
|
:group 'org-babel
|
|
|
|
:type '(choice string (const auto)))
|
|
|
|
|
|
|
|
(defcustom org-babel-python-command-nonsession "python"
|
|
|
|
"Command (including arguments) for executing non-interactive Python code.
|
|
|
|
If `org-babel-python-command' is set, then it overrides this option."
|
|
|
|
:package-version '(Org . "9.7")
|
2013-02-23 09:27:55 +00:00
|
|
|
:group 'org-babel
|
|
|
|
:type 'string)
|
2010-07-04 16:48:12 +00:00
|
|
|
|
2012-10-31 00:04:41 +00:00
|
|
|
(defcustom org-babel-python-hline-to "None"
|
2012-12-07 20:05:52 +00:00
|
|
|
"Replace hlines in incoming tables with this when translating to python."
|
|
|
|
:group 'org-babel
|
Add :version and :package-version
* ox.el (org-export-snippet-translation-alist)
(org-export-coding-system, org-export-in-background)
(org-export-async-init-file, org-export-invisible-backends)
(org-export-dispatch-use-expert-ui):
* ox-texinfo.el (org-texinfo-filename, org-texinfo-classes)
(org-texinfo-format-headline-function)
(org-texinfo-node-description-column)
(org-texinfo-active-timestamp-format)
(org-texinfo-link-with-unknown-path-format)
(org-texinfo-tables-verbatim)
(org-texinfo-table-scientific-notation)
(org-texinfo-text-markup-alist)
(org-texinfo-format-drawer-function)
(org-texinfo-format-inlinetask-function)
(org-texinfo-info-process):
* ox-man.el (org-man-tables-centered)
(org-man-table-scientific-notation)
(org-man-source-highlight, org-man-source-highlight-langs)
(org-man-pdf-process, org-man-logfiles-extensions):
* ox-html.el (org-html-allow-name-attribute-in-anchors)
(org-html-coding-system, org-html-divs):
* ox-ascii.el (org-ascii-text-width)
(org-ascii-headline-spacing, org-ascii-indented-line-width)
(org-ascii-paragraph-spacing, org-ascii-charset)
(org-ascii-underline, org-ascii-bullets)
(org-ascii-links-to-notes)
(org-ascii-table-keep-all-vertical-lines)
(org-ascii-table-widen-columns)
(org-ascii-table-use-ascii-art)
(org-ascii-format-drawer-function)
(org-ascii-format-inlinetask-function):
* org.el (org-modules, org-export-backends)
(org-highlight-latex-and-related, orgstruct-setup-hook):
* org-attach.el (org-attach-git-annex-cutoff):
* org-archive.el (org-archive-file-header-format):
* org-agenda.el (org-agenda-todo-ignore-time-comparison-use-seconds):
* ob-python.el (org-babel-python-hline-to)
(org-babel-python-None-to):
* ob-ditaa.el (org-ditaa-eps-jar-path):
* ob-core.el (org-babel-results-keyword): Add :version and
:package-version.
* ox-ascii.el: Use utf-8-emacs as the file coding system.
2013-03-05 15:34:16 +00:00
|
|
|
:package-version '(Org . "8.0")
|
2012-12-07 20:05:52 +00:00
|
|
|
:type 'string)
|
2012-10-31 00:04:41 +00:00
|
|
|
|
|
|
|
(defcustom org-babel-python-None-to 'hline
|
2015-09-21 04:23:36 +00:00
|
|
|
"Replace `None' in python tables with this before returning."
|
2012-12-07 20:05:52 +00:00
|
|
|
:group 'org-babel
|
Add :version and :package-version
* ox.el (org-export-snippet-translation-alist)
(org-export-coding-system, org-export-in-background)
(org-export-async-init-file, org-export-invisible-backends)
(org-export-dispatch-use-expert-ui):
* ox-texinfo.el (org-texinfo-filename, org-texinfo-classes)
(org-texinfo-format-headline-function)
(org-texinfo-node-description-column)
(org-texinfo-active-timestamp-format)
(org-texinfo-link-with-unknown-path-format)
(org-texinfo-tables-verbatim)
(org-texinfo-table-scientific-notation)
(org-texinfo-text-markup-alist)
(org-texinfo-format-drawer-function)
(org-texinfo-format-inlinetask-function)
(org-texinfo-info-process):
* ox-man.el (org-man-tables-centered)
(org-man-table-scientific-notation)
(org-man-source-highlight, org-man-source-highlight-langs)
(org-man-pdf-process, org-man-logfiles-extensions):
* ox-html.el (org-html-allow-name-attribute-in-anchors)
(org-html-coding-system, org-html-divs):
* ox-ascii.el (org-ascii-text-width)
(org-ascii-headline-spacing, org-ascii-indented-line-width)
(org-ascii-paragraph-spacing, org-ascii-charset)
(org-ascii-underline, org-ascii-bullets)
(org-ascii-links-to-notes)
(org-ascii-table-keep-all-vertical-lines)
(org-ascii-table-widen-columns)
(org-ascii-table-use-ascii-art)
(org-ascii-format-drawer-function)
(org-ascii-format-inlinetask-function):
* org.el (org-modules, org-export-backends)
(org-highlight-latex-and-related, orgstruct-setup-hook):
* org-attach.el (org-attach-git-annex-cutoff):
* org-archive.el (org-archive-file-header-format):
* org-agenda.el (org-agenda-todo-ignore-time-comparison-use-seconds):
* ob-python.el (org-babel-python-hline-to)
(org-babel-python-None-to):
* ob-ditaa.el (org-ditaa-eps-jar-path):
* ob-core.el (org-babel-results-keyword): Add :version and
:package-version.
* ox-ascii.el: Use utf-8-emacs as the file coding system.
2013-03-05 15:34:16 +00:00
|
|
|
:package-version '(Org . "8.0")
|
2013-11-14 13:05:18 +00:00
|
|
|
:type 'symbol)
|
2012-10-31 00:04:41 +00:00
|
|
|
|
2024-01-28 20:29:25 +00:00
|
|
|
(defun org-babel-python-associate-session (session)
|
|
|
|
"Associate Python code buffer with an Python session.
|
|
|
|
Make SESSION without earmuffs be the Python buffer name."
|
|
|
|
(setq-local python-shell-buffer-name
|
|
|
|
(org-babel-python-without-earmuffs session)))
|
|
|
|
|
2009-07-08 04:13:07 +00:00
|
|
|
(defun org-babel-execute:python (body params)
|
2023-10-01 10:41:59 +00:00
|
|
|
"Execute Python BODY according to PARAMS.
|
2010-07-13 23:20:08 +00:00
|
|
|
This function is called by `org-babel-execute-src-block'."
|
2017-04-29 14:26:33 +00:00
|
|
|
(let* ((org-babel-python-command
|
|
|
|
(or (cdr (assq :python params))
|
|
|
|
org-babel-python-command))
|
|
|
|
(session (org-babel-python-initiate-session
|
2016-09-22 17:45:15 +00:00
|
|
|
(cdr (assq :session params))))
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(graphics-file (and (member "graphics" (assq :result-params params))
|
|
|
|
(org-babel-graphical-output-file params)))
|
2016-09-22 17:45:15 +00:00
|
|
|
(result-params (cdr (assq :result-params params)))
|
|
|
|
(result-type (cdr (assq :result-type params)))
|
2020-09-27 15:41:26 +00:00
|
|
|
(return-val (when (eq result-type 'value)
|
2016-09-22 17:45:15 +00:00
|
|
|
(cdr (assq :return params))))
|
|
|
|
(preamble (cdr (assq :preamble params)))
|
2021-05-18 15:45:05 +00:00
|
|
|
(async (org-babel-comint-use-async params))
|
2010-10-21 11:28:19 +00:00
|
|
|
(full-body
|
2020-09-27 15:41:26 +00:00
|
|
|
(concat
|
|
|
|
(org-babel-expand-body:generic
|
|
|
|
body params
|
|
|
|
(org-babel-variable-assignments:python params))
|
|
|
|
(when return-val
|
|
|
|
(format (if session "\n%s" "\nreturn %s") return-val))))
|
2010-07-03 02:21:31 +00:00
|
|
|
(result (org-babel-python-evaluate
|
2021-05-18 15:45:05 +00:00
|
|
|
session full-body result-type
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
result-params preamble async graphics-file)))
|
2010-12-21 10:21:28 +00:00
|
|
|
(org-babel-reassemble-table
|
|
|
|
result
|
2016-09-22 17:45:15 +00:00
|
|
|
(org-babel-pick-name (cdr (assq :colname-names params))
|
|
|
|
(cdr (assq :colnames params)))
|
|
|
|
(org-babel-pick-name (cdr (assq :rowname-names params))
|
|
|
|
(cdr (assq :rownames params))))))
|
2009-06-12 15:46:00 +00:00
|
|
|
|
2009-06-14 18:34:06 +00:00
|
|
|
(defun org-babel-prep-session:python (session params)
|
2010-10-21 11:28:19 +00:00
|
|
|
"Prepare SESSION according to the header arguments in PARAMS.
|
2019-09-20 22:27:53 +00:00
|
|
|
VARS contains resolved variable references."
|
2009-06-14 18:34:06 +00:00
|
|
|
(let* ((session (org-babel-python-initiate-session session))
|
2010-10-21 11:28:19 +00:00
|
|
|
(var-lines
|
|
|
|
(org-babel-variable-assignments:python params)))
|
2009-06-14 18:34:06 +00:00
|
|
|
(org-babel-comint-in-buffer session
|
|
|
|
(mapc (lambda (var)
|
Xemacs incompatibilities
Carsten Dominik <carsten.dominik@gmail.com> writes:
> On May 17, 2010, at 4:39 PM, Michael Sperber wrote:
>
>> In particular, fixing the require won't be enough: org-babel-python.el
>> uses `run-python' and interacts with the inferior Python, whereas
>> python-mode.el defines `py-shell'.
>>
>> Should I try to abstract over the differences?
>
> Yes, this would be much appreciated. (I think, Eric or Dan?)
OK, I've attached a patch that makes `org-babel-python' work on XEmacs.
Most of the issues are pure XEmacs issues. Notes:
- XEmacs doesn't have [[:digit:]] - I hope to rectify this in the
future, but it seems there's no downside in this particular case to
replacing by [0-9].
- XEmacs doesn't have `move-end-of-line', but does have `end-of-line'.
I don't understand the intent of having both of these, but the code
seems fine with `end-of-line'.
- It seems there are way too few `require's throughout org-babel. I
don't know if it's OK to add the ones I needed.
- `org-babel-python-evaluate' looked broken as-is: It doesn't use the
`body' argument properly, the result is (I think) processed in the
wrong order and not properly split into lines. I've fixed all these,
but a review is probably in order.
2010-05-24 19:22:50 +00:00
|
|
|
(end-of-line 1) (insert var) (comint-send-input)
|
2020-02-18 21:57:37 +00:00
|
|
|
(org-babel-comint-wait-for-output session))
|
|
|
|
var-lines))
|
2010-01-11 17:14:30 +00:00
|
|
|
session))
|
|
|
|
|
|
|
|
(defun org-babel-load-session:python (session body params)
|
|
|
|
"Load BODY into SESSION."
|
|
|
|
(save-window-excursion
|
|
|
|
(let ((buffer (org-babel-prep-session:python session params)))
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(goto-char (process-mark (get-buffer-process (current-buffer))))
|
|
|
|
(insert (org-babel-chomp body)))
|
|
|
|
buffer)))
|
2009-06-14 18:34:06 +00:00
|
|
|
|
|
|
|
;; helper functions
|
|
|
|
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(defconst org-babel-python--output-graphics-wrapper "\
|
|
|
|
import matplotlib.pyplot
|
|
|
|
matplotlib.pyplot.gcf().clear()
|
|
|
|
%s
|
|
|
|
matplotlib.pyplot.savefig('%s')"
|
|
|
|
"Format string for saving Python graphical output.
|
|
|
|
Has two %s escapes, for the Python code to be evaluated, and the
|
|
|
|
file to save the graphics to.")
|
|
|
|
|
|
|
|
(defconst org-babel-python--def-format-value "\
|
|
|
|
def __org_babel_python_format_value(result, result_file, result_params):
|
|
|
|
with open(result_file, 'w') as f:
|
|
|
|
if 'graphics' in result_params:
|
|
|
|
result.savefig(result_file)
|
|
|
|
elif 'pp' in result_params:
|
|
|
|
import pprint
|
|
|
|
f.write(pprint.pformat(result))
|
|
|
|
elif 'list' in result_params and isinstance(result, dict):
|
|
|
|
f.write(str(['{} :: {}'.format(k, v) for k, v in result.items()]))
|
|
|
|
else:
|
|
|
|
if not set(result_params).intersection(\
|
|
|
|
['scalar', 'verbatim', 'raw']):
|
|
|
|
def dict2table(res):
|
|
|
|
if isinstance(res, dict):
|
|
|
|
return [(k, dict2table(v)) for k, v in res.items()]
|
|
|
|
elif isinstance(res, list) or isinstance(res, tuple):
|
|
|
|
return [dict2table(x) for x in res]
|
|
|
|
else:
|
|
|
|
return res
|
|
|
|
if 'table' in result_params:
|
|
|
|
result = dict2table(result)
|
|
|
|
try:
|
|
|
|
import pandas
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if isinstance(result, pandas.DataFrame) and 'table' in result_params:
|
|
|
|
result = [[result.index.name or ''] + list(result.columns)] + \
|
|
|
|
[None] + [[i] + list(row) for i, row in result.iterrows()]
|
|
|
|
elif isinstance(result, pandas.Series) and 'table' in result_params:
|
|
|
|
result = list(result.items())
|
|
|
|
try:
|
|
|
|
import numpy
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if isinstance(result, numpy.ndarray):
|
|
|
|
if 'table' in result_params:
|
|
|
|
result = result.tolist()
|
|
|
|
else:
|
|
|
|
result = repr(result)
|
|
|
|
f.write(str(result))"
|
|
|
|
"Python function to format value result and save it to file.")
|
|
|
|
|
2010-10-21 11:28:19 +00:00
|
|
|
(defun org-babel-variable-assignments:python (params)
|
2023-10-01 10:41:59 +00:00
|
|
|
"Return a list of Python statements assigning the block's variables.
|
|
|
|
The assignments are defined in PARAMS."
|
2010-10-14 20:05:02 +00:00
|
|
|
(mapcar
|
|
|
|
(lambda (pair)
|
|
|
|
(format "%s=%s"
|
|
|
|
(car pair)
|
|
|
|
(org-babel-python-var-to-python (cdr pair))))
|
2015-10-29 19:26:11 +00:00
|
|
|
(org-babel--get-vars params)))
|
2010-10-14 20:05:02 +00:00
|
|
|
|
2009-06-12 15:46:00 +00:00
|
|
|
(defun org-babel-python-var-to-python (var)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Convert an elisp value to a python variable.
|
|
|
|
Convert an elisp value, VAR, into a string of python source code
|
|
|
|
specifying a variable of the same value."
|
2009-06-12 15:46:00 +00:00
|
|
|
(if (listp var)
|
|
|
|
(concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]")
|
2016-09-25 15:29:06 +00:00
|
|
|
(if (eq var 'hline)
|
2012-10-31 00:04:41 +00:00
|
|
|
org-babel-python-hline-to
|
2010-07-14 18:31:38 +00:00
|
|
|
(format
|
2014-01-08 17:31:50 +00:00
|
|
|
(if (and (stringp var) (string-match "[\n\r]" var)) "\"\"%S\"\"" "%S")
|
2014-01-22 16:10:55 +00:00
|
|
|
(if (stringp var) (substring-no-properties var) var)))))
|
2009-06-12 15:46:00 +00:00
|
|
|
|
2009-07-05 23:55:54 +00:00
|
|
|
(defun org-babel-python-table-or-string (results)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Convert RESULTS into an appropriate elisp value.
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
If the results look like a list or tuple (but not a dict), then
|
|
|
|
convert them into an Emacs-lisp table. Otherwise return the
|
|
|
|
results as a string."
|
2023-12-23 20:11:05 +00:00
|
|
|
(let ((res (if (and (> (length results) 0)
|
|
|
|
(string-equal "{" (substring results 0 1)))
|
2024-06-09 22:38:13 +00:00
|
|
|
results ;don't convert dicts to elisp
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(org-babel-script-escape results))))
|
Backport changes from Emacs revs 115081 and 115082
2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca>
Address some byte-compiler warnings.
* ob-abc.el (org-babel-expand-body:abc): Use dolist.
(org-babel-execute:abc): Fix regexp quoting.
* ob-calc.el (org--var-syms): Rename from `var-syms'.
* ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding.
* ob-table.el (sbe): Move debug declaration.
* org-clock.el (org--msg-extra): Rename from `msg-extra'.
* org.el (org-version): Avoid var name starting with _.
(org-inhibit-startup, org-called-with-limited-levels)
(org-link-search-inhibit-query, org-time-was-given)
(org-end-time-was-given, org-def, org-defdecode, org-with-time):
* org-colview.el (org-agenda-overriding-columns-format):
* org-agenda.el (org-agenda-multi, org-depend-tag-blocked)
(org-agenda-show-log-scoped):
* ob-python.el (py-which-bufname, python-shell-buffer-name):
* ob-haskell.el (org-export-copy-to-kill-ring):
* ob-exp.el (org-link-search-inhibit-query):
* ob-R.el (ess-eval-visibly-p):
* ob-core.el (org-src-window-setup): Declare before use.
(org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'.
* ox-odt.el (org-odt-hfy-face-to-css):
* org-src.el (org-src-associate-babel-session, org-src-get-lang-mode):
* org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex)
(org-bibtex-check):
* ob-tangle.el (org-babel-tangle, org-babel-spec-to-string)
(org-babel-tangle-single-block, org-babel-tangle-comment-links):
* ob-table.el (sbe):
* ob-sqlite.el (org-babel-sqlite-expand-vars):
* ob-sql.el (org-babel-sql-expand-vars):
* ob-shen.el (org-babel-execute:shen):
* ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate):
* ob-scala.el (org-babel-scala-evaluate):
* ob-ruby.el (org-babel-ruby-table-or-string)
(org-babel-ruby-evaluate):
* ob-python.el (org-babel-python-table-or-string)
(org-babel-python-evaluate-external-process)
(org-babel-python-evaluate-session):
* ob-picolisp.el (org-babel-execute:picolisp):
* ob-perl.el (org-babel-perl-evaluate):
* ob-maxima.el (org-babel-execute:maxima):
* ob-lisp.el (org-babel-execute:lisp):
* ob-java.el (org-babel-execute:java):
* ob-io.el (org-babel-io-evaluate):
* ob-haskell.el (org-babel-execute:haskell):
* ob-fortran.el (org-babel-execute:fortran):
* ob-exp.el (org-babel-exp-code):
* ob-emacs-lisp.el (org-babel-execute:emacs-lisp):
* ob-ditaa.el (org-babel-execute:ditaa):
* ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash)
(org-babel-parse-header-arguments, org-babel-reassemble-table)
(org-babel-goto-src-block-head, org-babel-mark-block)
(org-babel-expand-noweb-references, org-babel-script-escape)
(org-babel-process-file-name):
* ob-clojure.el (org-babel-execute:clojure):
* ob-calc.el (org-babel-execute:calc):
* ob-awk.el (org-babel-execute:awk):
* ob-abc.el (org-babel-execute:abc):
* ob-R.el (org-babel-expand-body:R):
* ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...).
2013-11-12 Glenn Morris <rgm@gnu.org>
* ox-html.el (org-html-scripts): Add 2013 to copyright years.
(org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
|
|
|
(if (listp res)
|
2016-09-25 15:29:06 +00:00
|
|
|
(mapcar (lambda (el) (if (eq el 'None)
|
2021-09-29 07:22:47 +00:00
|
|
|
org-babel-python-None-to el))
|
Backport changes from Emacs revs 115081 and 115082
2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca>
Address some byte-compiler warnings.
* ob-abc.el (org-babel-expand-body:abc): Use dolist.
(org-babel-execute:abc): Fix regexp quoting.
* ob-calc.el (org--var-syms): Rename from `var-syms'.
* ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding.
* ob-table.el (sbe): Move debug declaration.
* org-clock.el (org--msg-extra): Rename from `msg-extra'.
* org.el (org-version): Avoid var name starting with _.
(org-inhibit-startup, org-called-with-limited-levels)
(org-link-search-inhibit-query, org-time-was-given)
(org-end-time-was-given, org-def, org-defdecode, org-with-time):
* org-colview.el (org-agenda-overriding-columns-format):
* org-agenda.el (org-agenda-multi, org-depend-tag-blocked)
(org-agenda-show-log-scoped):
* ob-python.el (py-which-bufname, python-shell-buffer-name):
* ob-haskell.el (org-export-copy-to-kill-ring):
* ob-exp.el (org-link-search-inhibit-query):
* ob-R.el (ess-eval-visibly-p):
* ob-core.el (org-src-window-setup): Declare before use.
(org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'.
* ox-odt.el (org-odt-hfy-face-to-css):
* org-src.el (org-src-associate-babel-session, org-src-get-lang-mode):
* org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex)
(org-bibtex-check):
* ob-tangle.el (org-babel-tangle, org-babel-spec-to-string)
(org-babel-tangle-single-block, org-babel-tangle-comment-links):
* ob-table.el (sbe):
* ob-sqlite.el (org-babel-sqlite-expand-vars):
* ob-sql.el (org-babel-sql-expand-vars):
* ob-shen.el (org-babel-execute:shen):
* ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate):
* ob-scala.el (org-babel-scala-evaluate):
* ob-ruby.el (org-babel-ruby-table-or-string)
(org-babel-ruby-evaluate):
* ob-python.el (org-babel-python-table-or-string)
(org-babel-python-evaluate-external-process)
(org-babel-python-evaluate-session):
* ob-picolisp.el (org-babel-execute:picolisp):
* ob-perl.el (org-babel-perl-evaluate):
* ob-maxima.el (org-babel-execute:maxima):
* ob-lisp.el (org-babel-execute:lisp):
* ob-java.el (org-babel-execute:java):
* ob-io.el (org-babel-io-evaluate):
* ob-haskell.el (org-babel-execute:haskell):
* ob-fortran.el (org-babel-execute:fortran):
* ob-exp.el (org-babel-exp-code):
* ob-emacs-lisp.el (org-babel-execute:emacs-lisp):
* ob-ditaa.el (org-babel-execute:ditaa):
* ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash)
(org-babel-parse-header-arguments, org-babel-reassemble-table)
(org-babel-goto-src-block-head, org-babel-mark-block)
(org-babel-expand-noweb-references, org-babel-script-escape)
(org-babel-process-file-name):
* ob-clojure.el (org-babel-execute:clojure):
* ob-calc.el (org-babel-execute:calc):
* ob-awk.el (org-babel-execute:awk):
* ob-abc.el (org-babel-execute:abc):
* ob-R.el (org-babel-expand-body:R):
* ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...).
2013-11-12 Glenn Morris <rgm@gnu.org>
* ox-html.el (org-html-scripts): Add 2013 to copyright years.
(org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
|
|
|
res)
|
|
|
|
res)))
|
2009-06-12 15:46:00 +00:00
|
|
|
|
2013-03-29 16:31:37 +00:00
|
|
|
(defvar org-babel-python-buffers '((:default . "*Python*")))
|
2009-06-12 21:48:40 +00:00
|
|
|
|
|
|
|
(defun org-babel-python-session-buffer (session)
|
2010-06-12 00:12:15 +00:00
|
|
|
"Return the buffer associated with SESSION."
|
2009-06-12 21:48:40 +00:00
|
|
|
(cdr (assoc session org-babel-python-buffers)))
|
|
|
|
|
2013-11-17 08:12:41 +00:00
|
|
|
(defun org-babel-python-with-earmuffs (session)
|
2023-10-01 10:41:59 +00:00
|
|
|
"Return SESSION name as string, ensuring *...* around."
|
2013-03-29 16:45:01 +00:00
|
|
|
(let ((name (if (stringp session) session (format "%s" session))))
|
|
|
|
(if (and (string= "*" (substring name 0 1))
|
|
|
|
(string= "*" (substring name (- (length name) 1))))
|
|
|
|
name
|
|
|
|
(format "*%s*" name))))
|
|
|
|
|
2013-11-17 08:12:41 +00:00
|
|
|
(defun org-babel-python-without-earmuffs (session)
|
2023-10-01 10:41:59 +00:00
|
|
|
"Return SESSION name as string, without *...* around."
|
2013-03-29 16:45:01 +00:00
|
|
|
(let ((name (if (stringp session) session (format "%s" session))))
|
|
|
|
(if (and (string= "*" (substring name 0 1))
|
|
|
|
(string= "*" (substring name (- (length name) 1))))
|
|
|
|
(substring name 1 (- (length name) 1))
|
|
|
|
name)))
|
|
|
|
|
2024-02-06 14:02:48 +00:00
|
|
|
(defun org-babel-session-buffer:python (session &optional _)
|
|
|
|
"Return session buffer name for SESSION."
|
|
|
|
(or (org-babel-python-session-buffer session)
|
|
|
|
(org-babel-python-with-earmuffs session)))
|
|
|
|
|
2023-12-29 16:43:24 +00:00
|
|
|
(defun org-babel-python--python-util-comint-end-of-output-p ()
|
|
|
|
"Return non-nil if the last prompt matches input prompt.
|
|
|
|
Backport of `python-util-comint-end-of-output-p' to emacs28. To
|
|
|
|
be removed after minimum supported version reaches emacs29."
|
2024-10-24 09:04:40 +00:00
|
|
|
(when-let* ((prompt (python-util-comint-last-prompt)))
|
2023-12-29 16:43:24 +00:00
|
|
|
(python-shell-comint-end-of-output-p
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
(car prompt) (cdr prompt)))))
|
|
|
|
|
2023-12-29 21:22:18 +00:00
|
|
|
(defun org-babel-python--command (is-session)
|
|
|
|
"Helper function to return the Python command.
|
|
|
|
This checks `org-babel-python-command', and then
|
|
|
|
`org-babel-python-command-session' (if IS-SESSION) or
|
|
|
|
`org-babel-python-command-nonsession' (if not IS-SESSION). If
|
2024-07-07 12:27:14 +00:00
|
|
|
IS-SESSION, this might return nil, which means to use
|
2023-12-29 21:22:18 +00:00
|
|
|
`python-shell-calculate-command'."
|
|
|
|
(or (unless (eq org-babel-python-command 'auto)
|
|
|
|
org-babel-python-command)
|
|
|
|
(if is-session
|
|
|
|
(unless (eq org-babel-python-command-session 'auto)
|
|
|
|
org-babel-python-command-session)
|
|
|
|
org-babel-python-command-nonsession)))
|
|
|
|
|
2022-11-15 03:39:04 +00:00
|
|
|
(defvar-local org-babel-python--initialized nil
|
|
|
|
"Flag used to mark that python session has been initialized.")
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(defun org-babel-python--setup-session ()
|
|
|
|
"Babel Python session setup code, to be run once per session.
|
|
|
|
Function should be run from within the Python session buffer.
|
|
|
|
This is often run as a part of `python-shell-first-prompt-hook',
|
|
|
|
unless the Python session was created outside Org."
|
|
|
|
(python-shell-send-string-no-output org-babel-python--def-format-value)
|
|
|
|
(setq-local org-babel-python--initialized t))
|
2009-06-14 18:34:06 +00:00
|
|
|
(defun org-babel-python-initiate-session-by-key (&optional session)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Initiate a python session.
|
2023-12-29 15:39:59 +00:00
|
|
|
If there is not a current inferior-process-buffer matching
|
2024-07-07 12:27:14 +00:00
|
|
|
SESSION then create it. If inferior process already
|
2023-12-29 15:39:59 +00:00
|
|
|
exists (e.g. if it was manually started with `run-python'), make
|
|
|
|
sure it's configured to work with ob-python. If session has
|
|
|
|
already been configured as such, do nothing. Return the
|
|
|
|
initialized session."
|
2009-06-12 21:48:40 +00:00
|
|
|
(save-window-excursion
|
|
|
|
(let* ((session (if session (intern session) :default))
|
2024-02-06 14:02:48 +00:00
|
|
|
(py-buffer (org-babel-session-buffer:python session))
|
2023-03-25 14:53:28 +00:00
|
|
|
(python-shell-buffer-name
|
|
|
|
(org-babel-python-without-earmuffs py-buffer))
|
2023-12-29 21:22:18 +00:00
|
|
|
(existing-session-p (comint-check-proc py-buffer))
|
|
|
|
(cmd (org-babel-python--command t)))
|
|
|
|
(if cmd
|
|
|
|
(let* ((cmd-split (split-string-and-unquote cmd))
|
|
|
|
(python-shell-interpreter (car cmd-split))
|
|
|
|
(python-shell-interpreter-args
|
|
|
|
(combine-and-quote-strings
|
|
|
|
(append (cdr cmd-split)
|
|
|
|
(when (member system-type
|
|
|
|
'(cygwin windows-nt ms-dos))
|
|
|
|
(list "-i"))))))
|
|
|
|
(run-python))
|
|
|
|
(run-python))
|
2023-01-27 15:47:05 +00:00
|
|
|
(with-current-buffer py-buffer
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(if existing-session-p
|
|
|
|
;; Session was created outside Org. Assume first prompt
|
|
|
|
;; already happened; run session setup code directly
|
|
|
|
(unless org-babel-python--initialized
|
2023-12-29 15:39:59 +00:00
|
|
|
;; Ensure first prompt. Based on python-tests.el
|
|
|
|
;; (`python-tests-shell-wait-for-prompt')
|
2023-12-29 16:43:24 +00:00
|
|
|
(while (not (org-babel-python--python-util-comint-end-of-output-p))
|
2023-12-29 15:39:59 +00:00
|
|
|
(sit-for 0.1))
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(org-babel-python--setup-session))
|
|
|
|
;; Adding to `python-shell-first-prompt-hook' immediately
|
|
|
|
;; after `run-python' should be safe from race conditions,
|
|
|
|
;; because subprocess output only arrives when Emacs is
|
|
|
|
;; waiting (see elisp manual, "Output from Processes")
|
|
|
|
(add-hook
|
|
|
|
'python-shell-first-prompt-hook
|
|
|
|
#'org-babel-python--setup-session
|
|
|
|
nil 'local)))
|
|
|
|
;; Wait until Python initializes
|
|
|
|
;; This is more reliable compared to
|
|
|
|
;; `org-babel-comint-wait-for-output' as python may emit
|
|
|
|
;; multiple prompts during initialization.
|
|
|
|
(with-current-buffer py-buffer
|
|
|
|
(while (not org-babel-python--initialized)
|
2023-10-21 09:50:22 +00:00
|
|
|
(sleep-for 0.010)))
|
2010-07-04 16:48:12 +00:00
|
|
|
(setq org-babel-python-buffers
|
2020-08-25 03:17:57 +00:00
|
|
|
(cons (cons session py-buffer)
|
2010-07-04 16:48:12 +00:00
|
|
|
(assq-delete-all session org-babel-python-buffers)))
|
2009-06-12 21:48:40 +00:00
|
|
|
session)))
|
2009-06-12 15:46:00 +00:00
|
|
|
|
2015-10-26 00:56:00 +00:00
|
|
|
(defun org-babel-python-initiate-session (&optional session _params)
|
2023-12-29 15:39:59 +00:00
|
|
|
"Initiate Python session named SESSION according to PARAMS.
|
|
|
|
If there is not a current inferior-process-buffer matching
|
2024-07-07 12:27:14 +00:00
|
|
|
SESSION then create it. If inferior process already
|
2023-12-29 15:39:59 +00:00
|
|
|
exists (e.g. if it was manually started with `run-python'), make
|
|
|
|
sure it's configured to work with ob-python. If session has
|
|
|
|
already been configured as such, do nothing."
|
2009-06-15 18:14:32 +00:00
|
|
|
(unless (string= session "none")
|
2010-07-04 16:48:12 +00:00
|
|
|
(org-babel-python-session-buffer
|
|
|
|
(org-babel-python-initiate-session-by-key session))))
|
|
|
|
|
2020-09-09 13:24:07 +00:00
|
|
|
(defvar org-babel-python-eoe-indicator "org_babel_python_eoe"
|
|
|
|
"A string to indicate that evaluation has completed.")
|
|
|
|
|
2020-09-07 16:39:21 +00:00
|
|
|
(defun org-babel-python-format-session-value
|
|
|
|
(src-file result-file result-params)
|
2023-10-01 10:41:59 +00:00
|
|
|
"Return Python code to evaluate SRC-FILE and write result to RESULT-FILE.
|
|
|
|
RESULT-PARAMS defines the result type."
|
2020-09-07 16:39:21 +00:00
|
|
|
(format "\
|
ob-python: Fix several issues with :session :results value
* lisp/ob-python.el (org-babel-python-evaluate-session): Fix a few
related issues with :session :results value blocks, including broken
if-else statements, indented blocks with blank lines, and returning
the wrong value when underscore has been used.
(org-babel-python--eval-ast): New constant variable, a string
consisting of Python code to execute a source block using ast.
Previously, python blocks with parameters ":session :results value"
were entered line-by-line into the Python session, which could cause
issues around indentation and new lines. Now, such python blocks are
written to temp files, then the built-in ast python module is used to
parse and execute them, and to extract the last line separately to
return as a result. Introduces a change in behavior, requiring that
the last line must be a top-level expression statement if its result
is to be saved (otherwise, the result is None).
2020-01-21 01:40:22 +00:00
|
|
|
import ast
|
2020-10-06 21:06:08 +00:00
|
|
|
with open('%s') as __org_babel_python_tmpfile:
|
|
|
|
__org_babel_python_ast = ast.parse(__org_babel_python_tmpfile.read())
|
2020-08-25 03:17:57 +00:00
|
|
|
__org_babel_python_final = __org_babel_python_ast.body[-1]
|
|
|
|
if isinstance(__org_babel_python_final, ast.Expr):
|
|
|
|
__org_babel_python_ast.body = __org_babel_python_ast.body[:-1]
|
|
|
|
exec(compile(__org_babel_python_ast, '<string>', 'exec'))
|
|
|
|
__org_babel_python_final = eval(compile(ast.Expression(
|
|
|
|
__org_babel_python_final.value), '<string>', 'eval'))
|
|
|
|
else:
|
|
|
|
exec(compile(__org_babel_python_ast, '<string>', 'exec'))
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
__org_babel_python_final = None
|
|
|
|
__org_babel_python_format_value(__org_babel_python_final, '%s', %s)"
|
2020-09-07 16:39:21 +00:00
|
|
|
(org-babel-process-file-name src-file 'noquote)
|
|
|
|
(org-babel-process-file-name result-file 'noquote)
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(org-babel-python-var-to-python result-params)))
|
ob-python: Fix several issues with :session :results value
* lisp/ob-python.el (org-babel-python-evaluate-session): Fix a few
related issues with :session :results value blocks, including broken
if-else statements, indented blocks with blank lines, and returning
the wrong value when underscore has been used.
(org-babel-python--eval-ast): New constant variable, a string
consisting of Python code to execute a source block using ast.
Previously, python blocks with parameters ":session :results value"
were entered line-by-line into the Python session, which could cause
issues around indentation and new lines. Now, such python blocks are
written to temp files, then the built-in ast python module is used to
parse and execute them, and to extract the last line separately to
return as a result. Introduces a change in behavior, requiring that
the last line must be a top-level expression statement if its result
is to be saved (otherwise, the result is None).
2020-01-21 01:40:22 +00:00
|
|
|
|
2010-07-03 02:21:31 +00:00
|
|
|
(defun org-babel-python-evaluate
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(session body &optional result-type result-params preamble async graphics-file)
|
2012-09-28 15:47:48 +00:00
|
|
|
"Evaluate BODY as Python code."
|
2010-09-08 01:58:58 +00:00
|
|
|
(if session
|
2021-05-18 15:45:05 +00:00
|
|
|
(if async
|
|
|
|
(org-babel-python-async-evaluate-session
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
session body result-type result-params graphics-file)
|
2021-05-18 15:45:05 +00:00
|
|
|
(org-babel-python-evaluate-session
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
session body result-type result-params graphics-file))
|
2010-09-08 01:58:58 +00:00
|
|
|
(org-babel-python-evaluate-external-process
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
body result-type result-params preamble graphics-file)))
|
2010-09-08 01:58:58 +00:00
|
|
|
|
2020-09-09 13:24:07 +00:00
|
|
|
(defun org-babel-python--shift-right (body &optional count)
|
|
|
|
(with-temp-buffer
|
|
|
|
(python-mode)
|
|
|
|
(insert body)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (not (eobp))
|
|
|
|
(unless (python-syntax-context 'string)
|
|
|
|
(python-indent-shift-right (line-beginning-position)
|
|
|
|
(line-end-position)
|
|
|
|
count))
|
|
|
|
(forward-line 1))
|
|
|
|
(buffer-string)))
|
|
|
|
|
2010-09-08 01:58:58 +00:00
|
|
|
(defun org-babel-python-evaluate-external-process
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(body &optional result-type result-params preamble graphics-file)
|
2010-09-08 01:58:58 +00:00
|
|
|
"Evaluate BODY in external python process.
|
2015-09-21 04:24:12 +00:00
|
|
|
If RESULT-TYPE equals `output' then return standard output as a
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
string. If RESULT-TYPE equals `value' then return the value of
|
|
|
|
the last statement in BODY, as elisp. If GRAPHICS-FILE is
|
|
|
|
non-nil, then save graphical results to that file instead."
|
Backport changes from Emacs revs 115081 and 115082
2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca>
Address some byte-compiler warnings.
* ob-abc.el (org-babel-expand-body:abc): Use dolist.
(org-babel-execute:abc): Fix regexp quoting.
* ob-calc.el (org--var-syms): Rename from `var-syms'.
* ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding.
* ob-table.el (sbe): Move debug declaration.
* org-clock.el (org--msg-extra): Rename from `msg-extra'.
* org.el (org-version): Avoid var name starting with _.
(org-inhibit-startup, org-called-with-limited-levels)
(org-link-search-inhibit-query, org-time-was-given)
(org-end-time-was-given, org-def, org-defdecode, org-with-time):
* org-colview.el (org-agenda-overriding-columns-format):
* org-agenda.el (org-agenda-multi, org-depend-tag-blocked)
(org-agenda-show-log-scoped):
* ob-python.el (py-which-bufname, python-shell-buffer-name):
* ob-haskell.el (org-export-copy-to-kill-ring):
* ob-exp.el (org-link-search-inhibit-query):
* ob-R.el (ess-eval-visibly-p):
* ob-core.el (org-src-window-setup): Declare before use.
(org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'.
* ox-odt.el (org-odt-hfy-face-to-css):
* org-src.el (org-src-associate-babel-session, org-src-get-lang-mode):
* org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex)
(org-bibtex-check):
* ob-tangle.el (org-babel-tangle, org-babel-spec-to-string)
(org-babel-tangle-single-block, org-babel-tangle-comment-links):
* ob-table.el (sbe):
* ob-sqlite.el (org-babel-sqlite-expand-vars):
* ob-sql.el (org-babel-sql-expand-vars):
* ob-shen.el (org-babel-execute:shen):
* ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate):
* ob-scala.el (org-babel-scala-evaluate):
* ob-ruby.el (org-babel-ruby-table-or-string)
(org-babel-ruby-evaluate):
* ob-python.el (org-babel-python-table-or-string)
(org-babel-python-evaluate-external-process)
(org-babel-python-evaluate-session):
* ob-picolisp.el (org-babel-execute:picolisp):
* ob-perl.el (org-babel-perl-evaluate):
* ob-maxima.el (org-babel-execute:maxima):
* ob-lisp.el (org-babel-execute:lisp):
* ob-java.el (org-babel-execute:java):
* ob-io.el (org-babel-io-evaluate):
* ob-haskell.el (org-babel-execute:haskell):
* ob-fortran.el (org-babel-execute:fortran):
* ob-exp.el (org-babel-exp-code):
* ob-emacs-lisp.el (org-babel-execute:emacs-lisp):
* ob-ditaa.el (org-babel-execute:ditaa):
* ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash)
(org-babel-parse-header-arguments, org-babel-reassemble-table)
(org-babel-goto-src-block-head, org-babel-mark-block)
(org-babel-expand-noweb-references, org-babel-script-escape)
(org-babel-process-file-name):
* ob-clojure.el (org-babel-execute:clojure):
* ob-calc.el (org-babel-execute:calc):
* ob-awk.el (org-babel-execute:awk):
* ob-abc.el (org-babel-execute:abc):
* ob-R.el (org-babel-expand-body:R):
* ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...).
2013-11-12 Glenn Morris <rgm@gnu.org>
* ox-html.el (org-html-scripts): Add 2013 to copyright years.
(org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
|
|
|
(let ((raw
|
2016-07-25 14:34:48 +00:00
|
|
|
(pcase result-type
|
2023-12-29 21:22:18 +00:00
|
|
|
(`output (org-babel-eval (org-babel-python--command nil)
|
2018-05-10 00:14:24 +00:00
|
|
|
(concat preamble (and preamble "\n")
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(if graphics-file
|
|
|
|
(format org-babel-python--output-graphics-wrapper
|
|
|
|
body graphics-file)
|
|
|
|
body))))
|
|
|
|
(`value (let ((results-file (or graphics-file
|
|
|
|
(org-babel-temp-file "python-"))))
|
2023-12-29 21:22:18 +00:00
|
|
|
(org-babel-eval (org-babel-python--command nil)
|
2016-07-25 14:34:48 +00:00
|
|
|
(concat
|
2018-05-10 00:14:24 +00:00
|
|
|
preamble (and preamble "\n")
|
2016-07-25 14:34:48 +00:00
|
|
|
(format
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(concat org-babel-python--def-format-value "
|
|
|
|
def main():
|
|
|
|
%s
|
|
|
|
|
|
|
|
__org_babel_python_format_value(main(), '%s', %s)")
|
|
|
|
(org-babel-python--shift-right body)
|
|
|
|
(org-babel-process-file-name results-file 'noquote)
|
|
|
|
(org-babel-python-var-to-python result-params))))
|
|
|
|
(org-babel-eval-read-file results-file))))))
|
Backport changes from Emacs revs 115081 and 115082
2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca>
Address some byte-compiler warnings.
* ob-abc.el (org-babel-expand-body:abc): Use dolist.
(org-babel-execute:abc): Fix regexp quoting.
* ob-calc.el (org--var-syms): Rename from `var-syms'.
* ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding.
* ob-table.el (sbe): Move debug declaration.
* org-clock.el (org--msg-extra): Rename from `msg-extra'.
* org.el (org-version): Avoid var name starting with _.
(org-inhibit-startup, org-called-with-limited-levels)
(org-link-search-inhibit-query, org-time-was-given)
(org-end-time-was-given, org-def, org-defdecode, org-with-time):
* org-colview.el (org-agenda-overriding-columns-format):
* org-agenda.el (org-agenda-multi, org-depend-tag-blocked)
(org-agenda-show-log-scoped):
* ob-python.el (py-which-bufname, python-shell-buffer-name):
* ob-haskell.el (org-export-copy-to-kill-ring):
* ob-exp.el (org-link-search-inhibit-query):
* ob-R.el (ess-eval-visibly-p):
* ob-core.el (org-src-window-setup): Declare before use.
(org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'.
* ox-odt.el (org-odt-hfy-face-to-css):
* org-src.el (org-src-associate-babel-session, org-src-get-lang-mode):
* org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex)
(org-bibtex-check):
* ob-tangle.el (org-babel-tangle, org-babel-spec-to-string)
(org-babel-tangle-single-block, org-babel-tangle-comment-links):
* ob-table.el (sbe):
* ob-sqlite.el (org-babel-sqlite-expand-vars):
* ob-sql.el (org-babel-sql-expand-vars):
* ob-shen.el (org-babel-execute:shen):
* ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate):
* ob-scala.el (org-babel-scala-evaluate):
* ob-ruby.el (org-babel-ruby-table-or-string)
(org-babel-ruby-evaluate):
* ob-python.el (org-babel-python-table-or-string)
(org-babel-python-evaluate-external-process)
(org-babel-python-evaluate-session):
* ob-picolisp.el (org-babel-execute:picolisp):
* ob-perl.el (org-babel-perl-evaluate):
* ob-maxima.el (org-babel-execute:maxima):
* ob-lisp.el (org-babel-execute:lisp):
* ob-java.el (org-babel-execute:java):
* ob-io.el (org-babel-io-evaluate):
* ob-haskell.el (org-babel-execute:haskell):
* ob-fortran.el (org-babel-execute:fortran):
* ob-exp.el (org-babel-exp-code):
* ob-emacs-lisp.el (org-babel-execute:emacs-lisp):
* ob-ditaa.el (org-babel-execute:ditaa):
* ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash)
(org-babel-parse-header-arguments, org-babel-reassemble-table)
(org-babel-goto-src-block-head, org-babel-mark-block)
(org-babel-expand-noweb-references, org-babel-script-escape)
(org-babel-process-file-name):
* ob-clojure.el (org-babel-execute:clojure):
* ob-calc.el (org-babel-execute:calc):
* ob-awk.el (org-babel-execute:awk):
* ob-abc.el (org-babel-execute:abc):
* ob-R.el (org-babel-expand-body:R):
* ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...).
2013-11-12 Glenn Morris <rgm@gnu.org>
* ox-html.el (org-html-scripts): Add 2013 to copyright years.
(org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
|
|
|
(org-babel-result-cond result-params
|
|
|
|
raw
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(org-babel-python-table-or-string raw))))
|
2010-09-08 01:58:58 +00:00
|
|
|
|
2023-01-27 15:47:05 +00:00
|
|
|
(defun org-babel-python-send-string (session body)
|
2020-09-07 00:45:08 +00:00
|
|
|
"Pass BODY to the Python process in SESSION.
|
|
|
|
Return output."
|
Add options to skip extra processing in org-babel-comint-with-output
This patch adds options to org-babel-comint-with-output to skip prompt
removal. Allowing individual languages to handle this cleanup can be
more robust than relying on the generic ob-comint implementation.
This allows ob-python to switch back to using
`org-babel-comint-with-output' rather than its own bespoke
reimplementation, reducing code duplication. Furthermore, this adds a
new implementation of ob-R non-async session output evaluation, that
is similar to the ob-python approach in that it avoids leaking
prompts, rather than relying on the cleanup from
`org-babel-comint-with-output'. A test is added to test-ob-R.el to
demonstrate the improved robustness of the new approach; previously,
this test would fail due to a false positive prompt, but now passes.
* lisp/ob-comint.el (org-babel-comint--remove-prompts-p): New helper
function to parse the prompt-handling argument in
`org-babel-comint-with-output' and `org-babel-comint-async-register'.
(org-babel-comint-with-output): Add a new argument
to prevent extra processing for prompt cleanup. Also, search for the
end-of-execution sentinel within the collected output rather than the
comint buffer, which doesn't depend on the position of point during
evaluation.
(org-babel-comint-async-register): Move parsing of prompt-handling
argument to `org-babel-comint--remove-prompts-p'.
* lisp/ob-python.el: Require subr-x for Emacs 27 compatibility.
(org-babel-python-send-string): Switch to using
`org-babel-comint-with-output', rather than bespoke reimplementation.
* lisp/ob-R.el: Require subr-x for Emacs 27 compatibility.
(ess-send-string): Declare external function.
(org-babel-R-evaluate-session): New implementation of output
evaluation that avoids leaking prompts, by writing the code block to a
tmp file and then sourcing it.
* testing/lisp/test-ob-R.el (test-ob-r/session-output-with->-bol): New
test for robustness against false positive prompts at the beginning of
a line.
2024-10-18 00:33:47 +00:00
|
|
|
(org-babel-chomp
|
|
|
|
(string-trim-right
|
|
|
|
(org-babel-comint-with-output
|
|
|
|
((org-babel-session-buffer:python session)
|
|
|
|
org-babel-python-eoe-indicator
|
|
|
|
nil nil 'disable-prompt-filtering)
|
|
|
|
(python-shell-send-string (format "\
|
2020-09-09 13:24:07 +00:00
|
|
|
try:
|
|
|
|
%s
|
|
|
|
except:
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
print('%s')"
|
Add options to skip extra processing in org-babel-comint-with-output
This patch adds options to org-babel-comint-with-output to skip prompt
removal. Allowing individual languages to handle this cleanup can be
more robust than relying on the generic ob-comint implementation.
This allows ob-python to switch back to using
`org-babel-comint-with-output' rather than its own bespoke
reimplementation, reducing code duplication. Furthermore, this adds a
new implementation of ob-R non-async session output evaluation, that
is similar to the ob-python approach in that it avoids leaking
prompts, rather than relying on the cleanup from
`org-babel-comint-with-output'. A test is added to test-ob-R.el to
demonstrate the improved robustness of the new approach; previously,
this test would fail due to a false positive prompt, but now passes.
* lisp/ob-comint.el (org-babel-comint--remove-prompts-p): New helper
function to parse the prompt-handling argument in
`org-babel-comint-with-output' and `org-babel-comint-async-register'.
(org-babel-comint-with-output): Add a new argument
to prevent extra processing for prompt cleanup. Also, search for the
end-of-execution sentinel within the collected output rather than the
comint buffer, which doesn't depend on the position of point during
evaluation.
(org-babel-comint-async-register): Move parsing of prompt-handling
argument to `org-babel-comint--remove-prompts-p'.
* lisp/ob-python.el: Require subr-x for Emacs 27 compatibility.
(org-babel-python-send-string): Switch to using
`org-babel-comint-with-output', rather than bespoke reimplementation.
* lisp/ob-R.el: Require subr-x for Emacs 27 compatibility.
(ess-send-string): Declare external function.
(org-babel-R-evaluate-session): New implementation of output
evaluation that avoids leaking prompts, by writing the code block to a
tmp file and then sourcing it.
* testing/lisp/test-ob-R.el (test-ob-r/session-output-with->-bol): New
test for robustness against false positive prompts at the beginning of
a line.
2024-10-18 00:33:47 +00:00
|
|
|
(org-babel-python--shift-right body 4)
|
|
|
|
org-babel-python-eoe-indicator)))
|
|
|
|
(rx (literal org-babel-python-eoe-indicator) (zero-or-more anychar)))))
|
2020-09-07 00:45:08 +00:00
|
|
|
|
2010-09-08 01:58:58 +00:00
|
|
|
(defun org-babel-python-evaluate-session
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(session body &optional result-type result-params graphics-file)
|
2010-09-08 01:58:58 +00:00
|
|
|
"Pass BODY to the Python process in SESSION.
|
2015-09-21 04:24:12 +00:00
|
|
|
If RESULT-TYPE equals `output' then return standard output as a
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
string. If RESULT-TYPE equals `value' then return the value of
|
|
|
|
the last statement in BODY, as elisp. If GRAPHICS-FILE is
|
|
|
|
non-nil, then save graphical results to that file instead."
|
2020-09-07 00:45:08 +00:00
|
|
|
(let* ((tmp-src-file (org-babel-temp-file "python-"))
|
2020-09-05 00:37:05 +00:00
|
|
|
(results
|
2020-08-25 03:17:57 +00:00
|
|
|
(progn
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(with-temp-file tmp-src-file
|
|
|
|
(insert (if (and graphics-file (eq result-type 'output))
|
|
|
|
(format org-babel-python--output-graphics-wrapper
|
|
|
|
body graphics-file)
|
|
|
|
body)))
|
2020-09-05 00:37:05 +00:00
|
|
|
(pcase result-type
|
2020-08-25 03:17:57 +00:00
|
|
|
(`output
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(let ((body (format "\
|
|
|
|
with open('%s') as f:
|
|
|
|
exec(compile(f.read(), f.name, 'exec'))"
|
2020-09-07 00:45:08 +00:00
|
|
|
(org-babel-process-file-name
|
|
|
|
tmp-src-file 'noquote))))
|
2023-01-27 15:47:05 +00:00
|
|
|
(org-babel-python-send-string session body)))
|
2020-09-05 00:37:05 +00:00
|
|
|
(`value
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(let* ((results-file (or graphics-file
|
|
|
|
(org-babel-temp-file "python-")))
|
2020-09-07 16:39:21 +00:00
|
|
|
(body (org-babel-python-format-session-value
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
tmp-src-file results-file result-params)))
|
2023-01-27 15:47:05 +00:00
|
|
|
(org-babel-python-send-string session body)
|
2023-10-21 09:50:22 +00:00
|
|
|
(sleep-for 0.010)
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(org-babel-eval-read-file results-file)))))))
|
2020-09-07 00:45:08 +00:00
|
|
|
(org-babel-result-cond result-params
|
|
|
|
results
|
|
|
|
(org-babel-python-table-or-string results))))
|
2009-06-12 15:46:00 +00:00
|
|
|
|
2009-06-12 23:23:28 +00:00
|
|
|
(defun org-babel-python-read-string (string)
|
2023-10-07 10:34:42 +00:00
|
|
|
"Strip \\='s from around Python STRING."
|
2016-08-24 15:05:54 +00:00
|
|
|
(if (and (string-prefix-p "'" string)
|
|
|
|
(string-suffix-p "'" string))
|
|
|
|
(substring string 1 -1)
|
2009-06-12 23:23:28 +00:00
|
|
|
string))
|
|
|
|
|
2021-05-18 15:45:05 +00:00
|
|
|
;; Async session eval
|
|
|
|
|
|
|
|
(defconst org-babel-python-async-indicator "print ('ob_comint_async_python_%s_%s')")
|
|
|
|
|
|
|
|
(defun org-babel-python-async-value-callback (params tmp-file)
|
|
|
|
(let ((result-params (cdr (assq :result-params params)))
|
|
|
|
(results (org-babel-eval-read-file tmp-file)))
|
|
|
|
(org-babel-result-cond result-params
|
|
|
|
results
|
|
|
|
(org-babel-python-table-or-string results))))
|
|
|
|
|
|
|
|
(defun org-babel-python-async-evaluate-session
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(session body &optional result-type result-params graphics-file)
|
2021-05-18 15:45:05 +00:00
|
|
|
"Asynchronously evaluate BODY in SESSION.
|
|
|
|
Returns a placeholder string for insertion, to later be replaced
|
|
|
|
by `org-babel-comint-async-filter'."
|
|
|
|
(org-babel-comint-async-register
|
|
|
|
session (current-buffer)
|
2024-06-26 12:55:58 +00:00
|
|
|
"ob_comint_async_python_\\(start\\|end\\|file\\)_\\(.+\\)"
|
2024-09-22 20:48:45 +00:00
|
|
|
'org-babel-chomp 'org-babel-python-async-value-callback
|
|
|
|
'disable-prompt-filtering)
|
2023-07-12 10:07:06 +00:00
|
|
|
(pcase result-type
|
|
|
|
(`output
|
|
|
|
(let ((uuid (org-id-uuid)))
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert (format org-babel-python-async-indicator "start" uuid))
|
|
|
|
(insert "\n")
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(insert (if graphics-file
|
|
|
|
(format org-babel-python--output-graphics-wrapper
|
|
|
|
body graphics-file)
|
|
|
|
body))
|
2023-07-12 10:07:06 +00:00
|
|
|
(insert "\n")
|
|
|
|
(insert (format org-babel-python-async-indicator "end" uuid))
|
|
|
|
(let ((python-shell-buffer-name
|
|
|
|
(org-babel-python-without-earmuffs session)))
|
|
|
|
(python-shell-send-buffer)))
|
|
|
|
uuid))
|
|
|
|
(`value
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(let ((results-file (or graphics-file
|
|
|
|
(org-babel-temp-file "python-")))
|
2023-07-12 10:07:06 +00:00
|
|
|
(tmp-src-file (org-babel-temp-file "python-")))
|
|
|
|
(with-temp-file tmp-src-file (insert body))
|
|
|
|
(with-temp-buffer
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(insert (org-babel-python-format-session-value
|
|
|
|
tmp-src-file results-file result-params))
|
2023-07-12 10:07:06 +00:00
|
|
|
(insert "\n")
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
(unless graphics-file
|
|
|
|
(insert (format org-babel-python-async-indicator "file" results-file)))
|
2023-07-12 10:07:06 +00:00
|
|
|
(let ((python-shell-buffer-name
|
|
|
|
(org-babel-python-without-earmuffs session)))
|
|
|
|
(python-shell-send-buffer)))
|
ob-python: Results handling for dicts, dataframes, arrays, plots
Thanks to Ihor Radchenko and Liu Hui for valuable feedback on this
patch.
* lisp/ob-python.el (org-babel-execute:python): Parse graphics-file
from params, and pass it to `org-babel-python-evaluate'.
(org-babel-python--output-graphics-wrapper): New constant. Python
code to save graphical output.
(org-babel-python--def-format-value): New constant. Python function
to format and save value results to file. Includes handling for
graphics, dataframes, and arrays.
(org-babel-python-table-or-string): Prevent `org-babel-script-escape'
from mangling dict results.
(org-babel-python--setup-session): New function to
run setup code within Python session.
(org-babel-python-initiate-session-by-key): Replace lambda with
`org-babel-python--setup-session' in `python-shell-first-prompt-hook',
or run it directly if the session was started outside Org.
(org-babel-python-wrapper-method): Removed. Instead use part of the
string directly in `org-babel-python-evaluate-external-process'.
(org-babel-python-pp-wrapper-method): Removed. Pretty printing is now
handled by `org-babel-python--def-format-value'.
(org-babel-python--exec-tmpfile): Removed. Instead use the raw string
directly in `org-babel-python-evaluate-session'.
(org-babel-python-format-session-value): Updated to use
`org-babel-python--def-format-value' for formatting value result.
(org-babel-python-evaluate): New parameter graphics-file. Pass
graphics-file onto downstream helper functions.
(org-babel-python-evaluate-external-process): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. For value result, use
`org-babel-python--def-format-value'. Don't call `org-trim' on
results, to prevent misalignment of pandas DataFrames.
(org-babel-python-evaluate-session): New parameter graphics-file. Use
`org-babel-python--output-graphics-wrapper' for graphical output.
Replace the removed constant `org-babel-python--exec-tmpfile' with the
string directly. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
(org-babel-python-async-evaluate-session): New parameter
graphics-file. Use `org-babel-python--output-graphics-wrapper' for
graphical output. Rename local variable tmp-results-file to
results-file, which may take the value of graphics-file when provided.
2020-09-07 16:58:30 +00:00
|
|
|
results-file))))
|
2021-05-18 15:45:05 +00:00
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
(provide 'ob-python)
|
2010-06-25 16:32:35 +00:00
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
;;; ob-python.el ends here
|