org-mode/lisp/ob-python.el

520 lines
20 KiB
EmacsLisp
Raw Normal View History

Activate lexical binding in some libraries * lisp/ob-C.el (org-babel-prep-session:C): (org-babel-load-session:C): * lisp/ob-J.el: (org-babel-expand-body:J): (org-babel-execute:J): * lisp/ob-R.el: (org-babel-expand-body:R): * lisp/ob-abc.el: (org-babel-execute:abc): (org-babel-prep-session:abc): * lisp/ob-asymptote.el: (org-babel-execute:asymptote): (org-babel-prep-session:asymptote): * lisp/ob-awk.el: (org-babel-expand-body:awk): * lisp/ob-calc.el: (org-babel-expand-body:calc): * lisp/ob-clojure.el: * lisp/ob-comint.el: (org-babel-comint-in-buffer): (org-babel-comint-with-output): (org-babel-comint-eval-invisibly-and-wait-for-file): * lisp/ob-coq.el: * lisp/ob-css.el: (org-babel-execute:css): (org-babel-prep-session:css): * lisp/ob-ditaa.el: (org-babel-execute:ditaa): (org-babel-prep-session:ditaa): * lisp/ob-dot.el: (org-babel-execute:dot): (org-babel-prep-session:dot): * lisp/ob-ebnf.el: * lisp/ob-emacs-lisp.el: * lisp/ob-eval.el: * lisp/ob-forth.el: * lisp/ob-fortran.el: (org-babel-execute:fortran): (org-babel-prep-session:fortran): (org-babel-load-session:fortran): * lisp/ob-gnuplot.el: (org-babel-expand-body:gnuplot): (org-babel-prep-session:gnuplot): (org-babel-gnuplot-initiate-session): * lisp/ob-groovy.el: (org-babel-prep-session:groovy): (org-babel-groovy-initiate-session): * lisp/ob-haskell.el: (org-babel-haskell-initiate-session): * lisp/ob-io.el: (org-babel-prep-session:io): (org-babel-io-initiate-session): * lisp/ob-java.el: (org-babel-execute:java): * lisp/ob-js.el: * lisp/ob-keys.el: * lisp/ob-latex.el: (org-babel-prep-session:latex): * lisp/ob-ledger.el: (org-babel-execute:ledger): (org-babel-prep-session:ledger): * lisp/ob-lilypond.el: (org-babel-lilypond-commands): (org-babel-lilypond-process-basic): (org-babel-prep-session:lilypond): (org-babel-lilypond-parse-line-num): * lisp/ob-lisp.el: * lisp/ob-makefile.el: (org-babel-execute:makefile): (org-babel-prep-session:makefile): * lisp/ob-matlab.el: * lisp/ob-maxima.el: (org-babel-prep-session:maxima): * lisp/ob-mscgen.el: (org-babel-prep-session:mscgen): * lisp/ob-ocaml.el: (org-babel-execute:ocaml): (org-babel-prep-session:ocaml): * lisp/ob-octave.el: (org-babel-execute:octave): (org-babel-octave-initiate-session): * lisp/ob-org.el: (org-babel-prep-session:org): * lisp/ob-perl.el: (org-babel-prep-session:perl): (org-babel-perl--var-to-perl): (org-babel-perl-initiate-session): * lisp/ob-picolisp.el: (org-babel-expand-body:picolisp): (org-babel-execute:picolisp): * lisp/ob-plantuml.el: (org-babel-execute:plantuml): (org-babel-prep-session:plantuml): * lisp/ob-processing.el: (org-babel-prep-session:processing): * lisp/ob-python.el: (org-babel-python-initiate-session): * lisp/ob-ref.el: (org-babel-ref-resolve): * lisp/ob-ruby.el: (org-babel-ruby-initiate-session): * lisp/ob-sass.el: (org-babel-execute:sass): (org-babel-prep-session:sass): * lisp/ob-scala.el: (org-babel-execute:scala): (org-babel-prep-session:scala): (org-babel-scala-initiate-session): * lisp/ob-scheme.el: * lisp/ob-screen.el: (org-babel-prep-session:screen): (org-babel-screen-session-write-temp-file): (org-babel-screen-test): * lisp/ob.el: * lisp/org-colview.el: (org-columns-todo): (org-columns-set-tags-or-toggle): (org-columns-new): (org-columns-uncompile-format): (org-agenda-colview-summarize): * lisp/org-footnote.el: (electric-indent-mode): * lisp/org-indent.el: (org-indent-refresh-maybe): * lisp/org-list.el: * lisp/org-macro.el: (org-macro--collect-macros): * lisp/org-src.el: * lisp/org-table.el: (sort-fold-case): (org-table-create): (org-table-field-info): (org-table-transpose-table-at-point): (org-table-remove-rectangle-highlight): (orgtbl-create-or-convert-from-region): (org-define-lookup-function): * lisp/ox-ascii.el: (org-ascii-format-drawer-function): (org-ascii--has-caption-p): (org-ascii-bold): (org-ascii-center-block): (org-ascii-clock): (org-ascii-code): (org-ascii-dynamic-block): (org-ascii-entity): (org-ascii-example-block): (org-ascii-export-snippet): (org-ascii-export-block): (org-ascii-fixed-width): (org-ascii-footnote-reference): (org-ascii-horizontal-rule): (org-ascii-inline-src-block): (org-ascii-format-inlinetask-default): (org-ascii-italic): (org-ascii-keyword): (org-ascii-latex-environment): (org-ascii-latex-fragment): (org-ascii-line-break): (org-ascii-node-property): (org-ascii-planning): (org-ascii-quote-block): (org-ascii-radio-target): (org-ascii-special-block): (org-ascii-src-block): (org-ascii-statistics-cookie): (org-ascii-subscript): (org-ascii-superscript): (org-ascii-strike-through): (org-ascii-timestamp): (org-ascii-underline): (org-ascii-verbatim): (org-ascii-verse-block): (org-ascii-filter-headline-blank-lines): (org-ascii-filter-paragraph-spacing): (org-ascii-filter-comment-spacing): Use lexical binding.
2015-10-25 20:56:00 -04:00
;;; ob-python.el --- Babel Functions for Python -*- lexical-binding: t; -*-
;; Copyright (C) 2009-2023 Free Software Foundation, Inc.
2012-04-01 18:53:28 -04:00
;; Authors: Eric Schulte
;; Dan Davison
2020-09-04 20:59:18 -04:00
;; Maintainer: Jack Kamm <jackkamm@gmail.com>
;; Keywords: literate programming, reproducible research
;; URL: https://orgmode.org
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; 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,
;; 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.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Org-Babel support for evaluating python source code.
;;; Code:
(require 'org-macs)
(org-assert-version)
(require 'ob)
(require 'org-macs)
(require 'python)
(defvar org-babel-tangle-lang-exts)
(add-to-list 'org-babel-tangle-lang-exts '("python" . "py"))
(defvar org-babel-default-header-args:python '())
(defconst org-babel-header-args:python
'((return . :any)
(python . :any))
"Python-specific header arguments.")
(defcustom org-babel-python-command "python"
"Name of the command for executing Python code."
:version "24.4"
:package-version '(Org . "8.0")
:group 'org-babel
:type 'string)
(defcustom org-babel-python-hline-to "None"
"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 10:34:16 -05:00
:version "24.4"
:package-version '(Org . "8.0")
:type 'string)
(defcustom org-babel-python-None-to 'hline
"Replace `None' in python tables with this before returning."
: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 10:34:16 -05:00
:version "24.4"
:package-version '(Org . "8.0")
Fix errors reported by cus-test.el * ox.el (org-export-async-init-file): Use :set to set the default value. * ox-texinfo.el (org-texinfo-filename): Fix default value. (org-texinfo-format-headline-function): Use 'ignore as the default value. (org-texinfo-format-drawer-function): Use a function as the default value. Update docstring. (org-texinfo-drawer): Always use `org-texinfo-format-drawer-function' as it is now a function by default. (org-texinfo-headline): Compare `org-texinfo-format-headline-function' against 'ignore. (org-texinfo-inlinetask): Compare `org-texinfo-format-inlinetask-function' against 'ignore. * ox-odt.el (org-odt-format-drawer-function): Use a function as the default value. Update docstring. (org-odt-format-headline-function) (org-odt-format-inlinetask-function): Fix default value. (org-odt-with-latex): Use :set to set the default value. (org-odt-drawer): Always use `org-odt-format-drawer-function' as it is now a function by default. (org-odt-format-headline--wrap): Compare `org-odt-format-headline-function' against 'ignore. * ox-latex.el (org-latex-format-drawer-function): Use a function as the default value. Update docstring. (org-latex-format-inlinetask-function): Fix default value. (org-latex-drawer): Always use `org-latex-format-drawer-function' as it is now a function by default. (org-latex-inlinetask): Compare `org-latex-format-inlinetask-function' against 'ignore. * ox-html.el (org-html-format-drawer-function): Use a function as the default value. Update docstring. (org-html-format-headline-function) (org-html-format-inlinetask-function): Fix default value. (org-html-with-latex): Use :set to set the default value. (org-html--format-toc-headline) (org-html-format-headline--wrap): Compare `org-html-format-headline-function' against 'ignore. (org-html-inlinetask): Compare `org-html-format-inlinetask-function' against 'ignore. * ox-ascii.el (org-ascii-format-drawer-function): Use a function as the default value. Update docstring. (org-ascii-drawer): Always use `org-ascii-format-drawer-function' as it is now a function by default. (org-ascii-format-inlinetask-default): New function. (org-ascii-format-inlinetask-function): Use `org-ascii-format-inlinetask-default' as the default. * org.el (org-mouse-1-follows-link): Use :set to set the default value. Update custom type. (org-log-note-headings): Fix order or list items in the custom type. (orgstruct-heading-prefix-regexp): Use an empty string as the default value. Use 'regexp as the custom type. (orgstruct-make-binding): Tiny docstring enhancement. Assume `orgstruct-heading-prefix-regexp' is a string. * org-agenda.el (org-agenda-search-view-max-outline-level): Set default value to 0. Update docstring. (org-agenda-deadline-leaders): Fix custom type. (org-search-view): Assume `org-agenda-search-view-max-outline-level' is a number. * ob-ruby.el (org-babel-ruby-nil-to): Fix custom type. * ob-python.el (org-babel-python-mode): Use :set to set the default value. (org-babel-python-None-to): Fix custom type. * ob-plantuml.el (org-plantuml-jar-path): Fix default value. (org-babel-execute:plantuml): Assume `org-plantuml-jar-path' is a string. * ob-latex.el (org-babel-latex-htlatex): Fix default value. (org-babel-latex-htlatex-packages): Fix custom type. (org-babel-execute:latex): Assume `org-babel-latex-htlatex' is a string. Thanks to Glenn Morris for reporting this.
2013-11-14 08:05:18 -05:00
:type 'symbol)
(defun org-babel-execute:python (body params)
"Execute Python BODY according to PARAMS.
2010-07-13 19:20:08 -04:00
This function is called by `org-babel-execute-src-block'."
(let* ((org-babel-python-command
(or (cdr (assq :python params))
org-babel-python-command))
(session (org-babel-python-initiate-session
Use assq instead of assoc for :keywords * lisp/ob-C.el (org-babel-C-execute): * lisp/ob-J.el (org-babel-execute:J): * lisp/ob-R.el (org-babel-edit-prep:R): (org-babel-expand-body:R): (org-babel-execute:R): (org-babel-variable-assignments:R): (org-babel-R-initiate-session): * lisp/ob-abc.el (org-babel-execute:abc): * lisp/ob-asymptote.el (org-babel-execute:asymptote): * lisp/ob-awk.el (org-babel-execute:awk): * lisp/ob-clojure.el (org-babel-expand-body:clojure): (org-babel-execute:clojure): * lisp/ob-core.el (org-babel-expand-body:generic): (org-babel-load-in-session): (org-babel-initiate-session): (org-babel-open-src-block-result): (org-babel-process-params): (org-babel-insert-result): (org-babel-expand-noweb-references): * lisp/ob-ditaa.el (org-babel-execute:ditaa): * lisp/ob-dot.el (org-babel-execute:dot): * lisp/ob-ebnf.el (org-babel-execute:ebnf): * lisp/ob-emacs-lisp.el (org-babel-expand-body:emacs-lisp): (org-babel-execute:emacs-lisp): * lisp/ob-exp.el (org-babel-exp-process-buffer): (org-babel-exp-do-export): (org-babel-exp-code): * lisp/ob-forth.el (org-babel-execute:forth): * lisp/ob-fortran.el (org-babel-execute:fortran): * lisp/ob-gnuplot.el (org-babel-gnuplot-process-vars): (org-babel-expand-body:gnuplot): (org-babel-execute:gnuplot): * lisp/ob-groovy.el (org-babel-execute:groovy): * lisp/ob-haskell.el (org-babel-execute:haskell): * lisp/ob-io.el (org-babel-execute:io): * lisp/ob-java.el (org-babel-execute:java): * lisp/ob-js.el (org-babel-execute:js): * lisp/ob-latex.el (org-babel-execute:latex): * lisp/ob-ledger.el (org-babel-execute:ledger): * lisp/ob-lilypond.el (org-babel-lilypond-process-basic): * lisp/ob-lisp.el (org-babel-expand-body:lisp): (org-babel-execute:lisp): * lisp/ob-lua.el (org-babel-execute:lua): * lisp/ob-maxima.el (org-babel-execute:maxima): * lisp/ob-mscgen.el (org-babel-execute:mscgen): * lisp/ob-ocaml.el (org-babel-execute:ocaml): * lisp/ob-octave.el (org-babel-execute:octave): * lisp/ob-org.el (org-babel-execute:org): * lisp/ob-perl.el (org-babel-execute:perl): * lisp/ob-picolisp.el (org-babel-execute:picolisp): * lisp/ob-plantuml.el (org-babel-execute:plantuml): * lisp/ob-python.el (org-babel-execute:python): * lisp/ob-ruby.el (org-babel-execute:ruby): * lisp/ob-sass.el (org-babel-execute:sass): * lisp/ob-scala.el (org-babel-execute:scala): * lisp/ob-scheme.el (org-babel-execute:scheme): * lisp/ob-screen.el (org-babel-execute:screen): (org-babel-prep-session:screen): * lisp/ob-shell.el (org-babel-execute:shell): (org-babel-variable-assignments:shell): (org-babel-sh-evaluate): * lisp/ob-shen.el (org-babel-execute:shen): * lisp/ob-sql.el (org-babel-execute:sql): * lisp/ob-sqlite.el (org-babel-execute:sqlite): * lisp/ob-tangle.el (org-babel-tangle): (org-babel-tangle-single-block): * lisp/org-src.el (org-src-associate-babel-session): * lisp/org.el (org-next-link): Use `assq' instead of `assoc' with :keyword keys.
2016-09-22 13:45:15 -04: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 12:58:30 -04:00
(graphics-file (and (member "graphics" (assq :result-params params))
(org-babel-graphical-output-file params)))
Use assq instead of assoc for :keywords * lisp/ob-C.el (org-babel-C-execute): * lisp/ob-J.el (org-babel-execute:J): * lisp/ob-R.el (org-babel-edit-prep:R): (org-babel-expand-body:R): (org-babel-execute:R): (org-babel-variable-assignments:R): (org-babel-R-initiate-session): * lisp/ob-abc.el (org-babel-execute:abc): * lisp/ob-asymptote.el (org-babel-execute:asymptote): * lisp/ob-awk.el (org-babel-execute:awk): * lisp/ob-clojure.el (org-babel-expand-body:clojure): (org-babel-execute:clojure): * lisp/ob-core.el (org-babel-expand-body:generic): (org-babel-load-in-session): (org-babel-initiate-session): (org-babel-open-src-block-result): (org-babel-process-params): (org-babel-insert-result): (org-babel-expand-noweb-references): * lisp/ob-ditaa.el (org-babel-execute:ditaa): * lisp/ob-dot.el (org-babel-execute:dot): * lisp/ob-ebnf.el (org-babel-execute:ebnf): * lisp/ob-emacs-lisp.el (org-babel-expand-body:emacs-lisp): (org-babel-execute:emacs-lisp): * lisp/ob-exp.el (org-babel-exp-process-buffer): (org-babel-exp-do-export): (org-babel-exp-code): * lisp/ob-forth.el (org-babel-execute:forth): * lisp/ob-fortran.el (org-babel-execute:fortran): * lisp/ob-gnuplot.el (org-babel-gnuplot-process-vars): (org-babel-expand-body:gnuplot): (org-babel-execute:gnuplot): * lisp/ob-groovy.el (org-babel-execute:groovy): * lisp/ob-haskell.el (org-babel-execute:haskell): * lisp/ob-io.el (org-babel-execute:io): * lisp/ob-java.el (org-babel-execute:java): * lisp/ob-js.el (org-babel-execute:js): * lisp/ob-latex.el (org-babel-execute:latex): * lisp/ob-ledger.el (org-babel-execute:ledger): * lisp/ob-lilypond.el (org-babel-lilypond-process-basic): * lisp/ob-lisp.el (org-babel-expand-body:lisp): (org-babel-execute:lisp): * lisp/ob-lua.el (org-babel-execute:lua): * lisp/ob-maxima.el (org-babel-execute:maxima): * lisp/ob-mscgen.el (org-babel-execute:mscgen): * lisp/ob-ocaml.el (org-babel-execute:ocaml): * lisp/ob-octave.el (org-babel-execute:octave): * lisp/ob-org.el (org-babel-execute:org): * lisp/ob-perl.el (org-babel-execute:perl): * lisp/ob-picolisp.el (org-babel-execute:picolisp): * lisp/ob-plantuml.el (org-babel-execute:plantuml): * lisp/ob-python.el (org-babel-execute:python): * lisp/ob-ruby.el (org-babel-execute:ruby): * lisp/ob-sass.el (org-babel-execute:sass): * lisp/ob-scala.el (org-babel-execute:scala): * lisp/ob-scheme.el (org-babel-execute:scheme): * lisp/ob-screen.el (org-babel-execute:screen): (org-babel-prep-session:screen): * lisp/ob-shell.el (org-babel-execute:shell): (org-babel-variable-assignments:shell): (org-babel-sh-evaluate): * lisp/ob-shen.el (org-babel-execute:shen): * lisp/ob-sql.el (org-babel-execute:sql): * lisp/ob-sqlite.el (org-babel-execute:sqlite): * lisp/ob-tangle.el (org-babel-tangle): (org-babel-tangle-single-block): * lisp/org-src.el (org-src-associate-babel-session): * lisp/org.el (org-next-link): Use `assq' instead of `assoc' with :keyword keys.
2016-09-22 13:45:15 -04:00
(result-params (cdr (assq :result-params params)))
(result-type (cdr (assq :result-type params)))
(return-val (when (eq result-type 'value)
Use assq instead of assoc for :keywords * lisp/ob-C.el (org-babel-C-execute): * lisp/ob-J.el (org-babel-execute:J): * lisp/ob-R.el (org-babel-edit-prep:R): (org-babel-expand-body:R): (org-babel-execute:R): (org-babel-variable-assignments:R): (org-babel-R-initiate-session): * lisp/ob-abc.el (org-babel-execute:abc): * lisp/ob-asymptote.el (org-babel-execute:asymptote): * lisp/ob-awk.el (org-babel-execute:awk): * lisp/ob-clojure.el (org-babel-expand-body:clojure): (org-babel-execute:clojure): * lisp/ob-core.el (org-babel-expand-body:generic): (org-babel-load-in-session): (org-babel-initiate-session): (org-babel-open-src-block-result): (org-babel-process-params): (org-babel-insert-result): (org-babel-expand-noweb-references): * lisp/ob-ditaa.el (org-babel-execute:ditaa): * lisp/ob-dot.el (org-babel-execute:dot): * lisp/ob-ebnf.el (org-babel-execute:ebnf): * lisp/ob-emacs-lisp.el (org-babel-expand-body:emacs-lisp): (org-babel-execute:emacs-lisp): * lisp/ob-exp.el (org-babel-exp-process-buffer): (org-babel-exp-do-export): (org-babel-exp-code): * lisp/ob-forth.el (org-babel-execute:forth): * lisp/ob-fortran.el (org-babel-execute:fortran): * lisp/ob-gnuplot.el (org-babel-gnuplot-process-vars): (org-babel-expand-body:gnuplot): (org-babel-execute:gnuplot): * lisp/ob-groovy.el (org-babel-execute:groovy): * lisp/ob-haskell.el (org-babel-execute:haskell): * lisp/ob-io.el (org-babel-execute:io): * lisp/ob-java.el (org-babel-execute:java): * lisp/ob-js.el (org-babel-execute:js): * lisp/ob-latex.el (org-babel-execute:latex): * lisp/ob-ledger.el (org-babel-execute:ledger): * lisp/ob-lilypond.el (org-babel-lilypond-process-basic): * lisp/ob-lisp.el (org-babel-expand-body:lisp): (org-babel-execute:lisp): * lisp/ob-lua.el (org-babel-execute:lua): * lisp/ob-maxima.el (org-babel-execute:maxima): * lisp/ob-mscgen.el (org-babel-execute:mscgen): * lisp/ob-ocaml.el (org-babel-execute:ocaml): * lisp/ob-octave.el (org-babel-execute:octave): * lisp/ob-org.el (org-babel-execute:org): * lisp/ob-perl.el (org-babel-execute:perl): * lisp/ob-picolisp.el (org-babel-execute:picolisp): * lisp/ob-plantuml.el (org-babel-execute:plantuml): * lisp/ob-python.el (org-babel-execute:python): * lisp/ob-ruby.el (org-babel-execute:ruby): * lisp/ob-sass.el (org-babel-execute:sass): * lisp/ob-scala.el (org-babel-execute:scala): * lisp/ob-scheme.el (org-babel-execute:scheme): * lisp/ob-screen.el (org-babel-execute:screen): (org-babel-prep-session:screen): * lisp/ob-shell.el (org-babel-execute:shell): (org-babel-variable-assignments:shell): (org-babel-sh-evaluate): * lisp/ob-shen.el (org-babel-execute:shen): * lisp/ob-sql.el (org-babel-execute:sql): * lisp/ob-sqlite.el (org-babel-execute:sqlite): * lisp/ob-tangle.el (org-babel-tangle): (org-babel-tangle-single-block): * lisp/org-src.el (org-src-associate-babel-session): * lisp/org.el (org-next-link): Use `assq' instead of `assoc' with :keyword keys.
2016-09-22 13:45:15 -04:00
(cdr (assq :return params))))
(preamble (cdr (assq :preamble params)))
ob-comint.el, ob-python.el: Async session evaluation Adds functionality to ob-comint.el to implement async session eval on a per-language basis. Adds a reference implementation for ob-python. * lisp/ob-comint.el (org-babel-comint-with-output): Remove comment. (org-babel-comint-async-indicator, org-babel-comint-async-buffers, org-babel-comint-async-file-callback, org-babel-comint-async-chunk-callback, org-babel-comint-async-dangling): Add buffer-local variables used for async comint evaluation. (org-babel-comint-use-async): Add function to determine whether block should be evaluated asynchronously. (org-babel-comint-async-filter): Add filter function to attach to comint-output-filter-functions for babel async eval. (org-babel-comint-async-register): Add function to setup buffer variables and hooks for session eval. (org-babel-comint-async-delete-dangling-and-eval): Add helper function for async session eval. * lisp/ob-python.el (org-babel-execute:python): Check for async header argument. (org-babel-python-evaluate): Check whether to use async evaluation. (org-babel-python-async-indicator): Add constant for indicating the start/end of async evaluations. (org-babel-python-async-evaluate-session): Add function for Python async eval. * testing/lisp/test-ob-python.el (test-ob-python/async-simple-session-output): Unit test for Python async session eval. (test-ob-python/async-named-output): Unit test that Python async eval can replace named output. (test-ob-python/async-output-drawer): Unit test that Python async eval works with drawer results.
2021-05-18 11:45:05 -04:00
(async (org-babel-comint-use-async params))
(full-body
(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))))
(result (org-babel-python-evaluate
ob-comint.el, ob-python.el: Async session evaluation Adds functionality to ob-comint.el to implement async session eval on a per-language basis. Adds a reference implementation for ob-python. * lisp/ob-comint.el (org-babel-comint-with-output): Remove comment. (org-babel-comint-async-indicator, org-babel-comint-async-buffers, org-babel-comint-async-file-callback, org-babel-comint-async-chunk-callback, org-babel-comint-async-dangling): Add buffer-local variables used for async comint evaluation. (org-babel-comint-use-async): Add function to determine whether block should be evaluated asynchronously. (org-babel-comint-async-filter): Add filter function to attach to comint-output-filter-functions for babel async eval. (org-babel-comint-async-register): Add function to setup buffer variables and hooks for session eval. (org-babel-comint-async-delete-dangling-and-eval): Add helper function for async session eval. * lisp/ob-python.el (org-babel-execute:python): Check for async header argument. (org-babel-python-evaluate): Check whether to use async evaluation. (org-babel-python-async-indicator): Add constant for indicating the start/end of async evaluations. (org-babel-python-async-evaluate-session): Add function for Python async eval. * testing/lisp/test-ob-python.el (test-ob-python/async-simple-session-output): Unit test for Python async session eval. (test-ob-python/async-named-output): Unit test that Python async eval can replace named output. (test-ob-python/async-output-drawer): Unit test that Python async eval works with drawer results.
2021-05-18 11:45:05 -04: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 12:58:30 -04:00
result-params preamble async graphics-file)))
ob: language-specific changes in response to :file arg changes These languages are capable of writing results to file; for several of them this is their only mode of operation. These changes cause the languages to return to ob.el either the computed result, or nil, when they have written results to file themselves. This is in place of the previous method of returning the output file name as a string to ob.el. * lisp/ob-asymptote.el (org-babel-execute:asymptote): Return nil to signal that the intended content has been written to file. * lisp/ob-ditaa.el (org-babel-execute:ditaa): Return nil to signal that the intended content has been written to file. * lisp/ob-dot.el (org-babel-execute:dot): Return nil to signal that the intended content has been written to file. * lisp/ob-gnuplot.el (org-babel-execute:gnuplot): Return nil to signal that the intended content has been written to file. * lisp/ob-latex.el (org-babel-execute:latex): Return nil to signal that the intended content has been written to file. * lisp/ob-mscgen.el (org-babel-execute:mscgen): Return nil to signal that the intended content has been written to file. * lisp/ob-octave.el (org-babel-execute:octave): Return result; not name of output file. * lisp/ob-plantuml.el (org-babel-execute:plantuml): Return nil to signal that the intended content has been written to file. * lisp/ob-python.el (org-babel-execute:python): Return result; not name of output file. * lisp/ob-ruby.el (org-babel-execute:ruby): Return result; not name of output file. * lisp/ob-sass.el (org-babel-execute:sass): Return nil if result has been written to file
2010-12-21 05:21:28 -05:00
(org-babel-reassemble-table
result
Use assq instead of assoc for :keywords * lisp/ob-C.el (org-babel-C-execute): * lisp/ob-J.el (org-babel-execute:J): * lisp/ob-R.el (org-babel-edit-prep:R): (org-babel-expand-body:R): (org-babel-execute:R): (org-babel-variable-assignments:R): (org-babel-R-initiate-session): * lisp/ob-abc.el (org-babel-execute:abc): * lisp/ob-asymptote.el (org-babel-execute:asymptote): * lisp/ob-awk.el (org-babel-execute:awk): * lisp/ob-clojure.el (org-babel-expand-body:clojure): (org-babel-execute:clojure): * lisp/ob-core.el (org-babel-expand-body:generic): (org-babel-load-in-session): (org-babel-initiate-session): (org-babel-open-src-block-result): (org-babel-process-params): (org-babel-insert-result): (org-babel-expand-noweb-references): * lisp/ob-ditaa.el (org-babel-execute:ditaa): * lisp/ob-dot.el (org-babel-execute:dot): * lisp/ob-ebnf.el (org-babel-execute:ebnf): * lisp/ob-emacs-lisp.el (org-babel-expand-body:emacs-lisp): (org-babel-execute:emacs-lisp): * lisp/ob-exp.el (org-babel-exp-process-buffer): (org-babel-exp-do-export): (org-babel-exp-code): * lisp/ob-forth.el (org-babel-execute:forth): * lisp/ob-fortran.el (org-babel-execute:fortran): * lisp/ob-gnuplot.el (org-babel-gnuplot-process-vars): (org-babel-expand-body:gnuplot): (org-babel-execute:gnuplot): * lisp/ob-groovy.el (org-babel-execute:groovy): * lisp/ob-haskell.el (org-babel-execute:haskell): * lisp/ob-io.el (org-babel-execute:io): * lisp/ob-java.el (org-babel-execute:java): * lisp/ob-js.el (org-babel-execute:js): * lisp/ob-latex.el (org-babel-execute:latex): * lisp/ob-ledger.el (org-babel-execute:ledger): * lisp/ob-lilypond.el (org-babel-lilypond-process-basic): * lisp/ob-lisp.el (org-babel-expand-body:lisp): (org-babel-execute:lisp): * lisp/ob-lua.el (org-babel-execute:lua): * lisp/ob-maxima.el (org-babel-execute:maxima): * lisp/ob-mscgen.el (org-babel-execute:mscgen): * lisp/ob-ocaml.el (org-babel-execute:ocaml): * lisp/ob-octave.el (org-babel-execute:octave): * lisp/ob-org.el (org-babel-execute:org): * lisp/ob-perl.el (org-babel-execute:perl): * lisp/ob-picolisp.el (org-babel-execute:picolisp): * lisp/ob-plantuml.el (org-babel-execute:plantuml): * lisp/ob-python.el (org-babel-execute:python): * lisp/ob-ruby.el (org-babel-execute:ruby): * lisp/ob-sass.el (org-babel-execute:sass): * lisp/ob-scala.el (org-babel-execute:scala): * lisp/ob-scheme.el (org-babel-execute:scheme): * lisp/ob-screen.el (org-babel-execute:screen): (org-babel-prep-session:screen): * lisp/ob-shell.el (org-babel-execute:shell): (org-babel-variable-assignments:shell): (org-babel-sh-evaluate): * lisp/ob-shen.el (org-babel-execute:shen): * lisp/ob-sql.el (org-babel-execute:sql): * lisp/ob-sqlite.el (org-babel-execute:sqlite): * lisp/ob-tangle.el (org-babel-tangle): (org-babel-tangle-single-block): * lisp/org-src.el (org-src-associate-babel-session): * lisp/org.el (org-next-link): Use `assq' instead of `assoc' with :keyword keys.
2016-09-22 13:45:15 -04: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-14 14:34:06 -04:00
(defun org-babel-prep-session:python (session params)
"Prepare SESSION according to the header arguments in PARAMS.
Backport commit 114323226 from Emacs * lisp/ob-C.el (org-babel-prep-session:C) (org-babel-load-session:C): * lisp/ob-J.el (org-babel-execute:J): * lisp/ob-asymptote.el (org-babel-prep-session:asymptote): * lisp/ob-awk.el (org-babel-execute:awk): * lisp/ob-core.el (org-babel-process-file-name): * lisp/ob-ebnf.el (org-babel-execute:ebnf): * lisp/ob-forth.el (org-babel-execute:forth): * lisp/ob-fortran.el (org-babel-execute:fortran) (org-babel-prep-session:fortran, org-babel-load-session:fortran): * lisp/ob-groovy.el (org-babel-execute:groovy): * lisp/ob-io.el (org-babel-execute:io): * lisp/ob-js.el (org-babel-execute:js): * lisp/ob-lilypond.el (org-babel-default-header-args:lilypond) (org-babel-lilypond-compile-post-tangle) (org-babel-lilypond-display-pdf-post-tangle) (org-babel-lilypond-tangle) (org-babel-lilypond-execute-tangled-ly) (org-babel-lilypond-compile-lilyfile) (org-babel-lilypond-check-for-compile-error) (org-babel-lilypond-process-compile-error) (org-babel-lilypond-mark-error-line) (org-babel-lilypond-parse-error-line) (org-babel-lilypond-attempt-to-open-pdf) (org-babel-lilypond-attempt-to-play-midi) (org-babel-lilypond-switch-extension) (org-babel-lilypond-set-header-args): * lisp/ob-lua.el (org-babel-prep-session:lua): * lisp/ob-picolisp.el (org-babel-execute:picolisp): * lisp/ob-processing.el (org-babel-prep-session:processing): * lisp/ob-python.el (org-babel-prep-session:python): * lisp/ob-scheme.el (org-babel-scheme-capture-current-message) (org-babel-scheme-execute-with-geiser, org-babel-execute:scheme): * lisp/ob-shen.el (org-babel-execute:shen): * lisp/org-agenda.el (org-agenda-entry-types) (org-agenda-move-date-from-past-immediately-to-today) (org-agenda-time-grid, org-agenda-sorting-strategy) (org-agenda-filter-by-category, org-agenda-forward-block): * lisp/org-faces.el (org-verbatim, org-cycle-level-faces): * lisp/org-indent.el (org-indent-set-line-properties): * lisp/org-macs.el (org-get-limited-outline-regexp): * lisp/org-mobile.el (org-mobile-files): * lisp/org.el (org-use-fast-todo-selection) (org-extend-today-until, org-use-property-inheritance) (org-refresh-effort-properties) (org-track-ordered-property-with-tag, org-shiftright): * lisp/ox-html.el (org-html-checkbox-type): * lisp/ox-man.el (org-man-source-highlight) (org-man-verse-block): * lisp/ox-publish.el (org-publish-sitemap-default): Assorted docfixes. lisp/*.el: Fix typos and other trivial doc fixes 11432322650830fe9ae365f4113733a79226056d Juanma Barranquero Sat Sep 21 00:27:53 2019 +0200
2019-09-20 18:27:53 -04:00
VARS contains resolved variable references."
2009-06-14 14:34:06 -04:00
(let* ((session (org-babel-python-initiate-session session))
(var-lines
(org-babel-variable-assignments:python params)))
2009-06-14 14:34:06 -04:00
(org-babel-comint-in-buffer session
(mapc (lambda (var)
(end-of-line 1) (insert var) (comint-send-input)
Tiny formatting fixes * lisp/ox.el (org-export-table-dimensions): * lisp/ox-texinfo.el (org-texinfo-template): * lisp/ox-md.el (org-md-link): * lisp/ox-icalendar.el (org-icalendar-use-UTC-date-time-p): * lisp/ox-ascii.el (org-ascii-fixed-width): * lisp/org.el (org-context): * lisp/org-table.el (org-table-eval-formula) (org-table-export): * lisp/org-refile.el: * lisp/org-plot.el (org-plot/gnuplot-to-grid-data): * lisp/org-num.el (org-num): * lisp/org-mouse.el (org-mouse-popup-global-menu) (org-mouse-context-menu): * lisp/org-macro.el (org-macro): * lisp/org-lint.el (org-lint): * lisp/org-keys.el (org-keys): * lisp/org-duration.el: * lisp/org-clock.el (org-clock-get-last-clock-out-time) (org-clock-update-mode-line, org-find-open-clocks): * lisp/org-agenda.el (org-diary) (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item) (org-agenda-highlight-todo, org-cmp-alpha) (org-agenda-filter-by-category): * lisp/ol.el (org-link-expand-abbrev, ol): * lisp/ol-docview.el (ol-docview): * lisp/ol-bibtex.el (org-execute-file-search-in-bibtex) (org-bibtex, org-bibtex-read): * lisp/ol-bbdb.el (org-bbdb-anniversary-description): * lisp/ob-tangle.el (org-babel-tangle-jump-to-org): * lisp/ob-table.el (org-babel-table-truncate-at-newline): * lisp/ob-stan.el: * lisp/ob-sqlite.el (org-babel-sqlite-table-or-scalar): * lisp/ob-sql.el: * lisp/ob-shen.el: * lisp/ob-shell.el (org-babel-prep-session:shell) (org-babel-prep-session:shell): * lisp/ob-sed.el (org-babel-execute:sed) (org-babel-execute:sed): * lisp/ob-screen.el: * lisp/ob-sass.el: * lisp/ob-ruby.el (org-babel-prep-session:ruby) (org-babel-prep-session:ruby): * lisp/ob-ref.el (org-babel-ref-resolve, ob-ref): * lisp/ob-python.el (org-babel-prep-session:python) (org-babel-prep-session:python): * lisp/ob-plantuml.el: * lisp/ob-picolisp.el: * lisp/ob-perl.el: * lisp/ob-org.el: * lisp/ob-octave.el (org-babel-prep-session:octave) (org-babel-prep-session:octave) (org-babel-octave-evaluate-session): * lisp/ob-ocaml.el: * lisp/ob-mscgen.el (org-babel-execute:mscgen) (org-babel-execute:mscgen): * lisp/ob-maxima.el: (ob-maxima): * lisp/ob-matlab.el: * lisp/ob-makefile.el: * lisp/ob-lua.el (org-babel-prep-session:lua) (org-babel-prep-session:lua): * lisp/ob-lisp.el: * lisp/ob-ledger.el: * lisp/ob-latex.el (org-babel-expand-body:latex) (org-babel-expand-body:latex, ob-latex): * lisp/ob-js.el: * lisp/ob-java.el: * lisp/ob-io.el (org-babel-prep-session:io) (org-babel-prep-session:io): * lisp/ob-hledger.el: * lisp/ob-haskell.el: * lisp/ob-groovy.el (org-babel-groovy-wrapper-method) (org-babel-groovy-evaluate): * lisp/ob-gnuplot.el: * lisp/ob-fortran.el (org-babel-expand-body:fortran) (org-babel-expand-body:fortran): * lisp/ob-forth.el (org-babel-forth-session-execute): * lisp/ob-exp.el (ob-exp): * lisp/ob-eval.el: * lisp/ob-emacs-lisp.el: * lisp/ob-ebnf.el: * lisp/ob-dot.el: * lisp/ob-ditaa.el: * lisp/ob-css.el: * lisp/ob-core.el (org-babel-put-rownames): * lisp/ob-coq.el: * lisp/ob-comint.el: * lisp/ob-calc.el: * lisp/ob-awk.el: * lisp/ob-asymptote.el: * lisp/ob-abc.el: * lisp/ob-R.el (org-babel-prep-session:R): Formatting fixes.
2020-02-18 16:57:37 -05:00
(org-babel-comint-wait-for-output session))
var-lines))
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 14:34:06 -04: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 12:58:30 -04: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.")
(defun org-babel-variable-assignments:python (params)
"Return a list of Python statements assigning the block's variables.
The assignments are defined in PARAMS."
(mapcar
(lambda (pair)
(format "%s=%s"
(car pair)
(org-babel-python-var-to-python (cdr pair))))
babel: small change in API. * lisp/ob-core.el (org-babel--get-vars): New function. (org-babel-get-header): Delete. * lisp/ob-C.el (org-babel-C-expand-C): (org-babel-C-expand-D): * lisp/ob-R.el (org-babel-variable-assignments:R): * lisp/ob-abc.el (org-babel-expand-body:abc): * lisp/ob-asymptote.el (org-babel-variable-assignments:asymptote): * lisp/ob-awk.el (org-babel-execute:awk): * lisp/ob-calc.el (org-babel-execute:calc): * lisp/ob-clojure.el (org-babel-expand-body:clojure): * lisp/ob-dot.el (org-babel-expand-body:dot): * lisp/ob-emacs-lisp.el (org-babel-expand-body:emacs-lisp): * lisp/ob-fortran.el (org-babel-expand-body:fortran): (org-babel-fortran-ensure-main-wrap): * lisp/ob-gnuplot.el (org-babel-gnuplot-process-vars): * lisp/ob-haskell.el (org-babel-variable-assignments:haskell): * lisp/ob-js.el (org-babel-variable-assignments:js): * lisp/ob-latex.el (org-babel-expand-body:latex): * lisp/ob-lilypond.el (org-babel-expand-body:lilypond): * lisp/ob-lisp.el (org-babel-expand-body:lisp): * lisp/ob-maxima.el (org-babel-maxima-expand): * lisp/ob-ocaml.el (org-babel-variable-assignments:ocaml): * lisp/ob-octave.el (org-babel-variable-assignments:octave): * lisp/ob-org.el (org-babel-expand-body:org): * lisp/ob-perl.el (org-babel-variable-assignments:perl): * lisp/ob-picolisp.el (org-babel-expand-body:picolisp): * lisp/ob-processing.el (org-babel-variable-assignments:processing): * lisp/ob-python.el (org-babel-variable-assignments:python): * lisp/ob-ruby.el (org-babel-variable-assignments:ruby): * lisp/ob-scheme.el (org-babel-expand-body:scheme): * lisp/ob-shell.el (org-babel-variable-assignments:shell): * lisp/ob-shen.el (org-babel-expand-body:shen): * lisp/ob-sql.el (org-babel-expand-body:sql): * lisp/ob-sqlite.el (org-babel-expand-body:sqlite): Adapt to change. * etc/ORG-NEWS: Document change.
2015-10-29 15:26:11 -04:00
(org-babel--get-vars params)))
(defun org-babel-python-var-to-python (var)
2010-07-13 19:20:08 -04: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."
(if (listp var)
(concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]")
(if (eq var 'hline)
org-babel-python-hline-to
(format
(if (and (stringp var) (string-match "[\n\r]" var)) "\"\"%S\"\"" "%S")
2014-01-22 11:10:55 -05:00
(if (stringp var) (substring-no-properties var) var)))))
(defun org-babel-python-table-or-string (results)
2010-07-13 19:20:08 -04: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 12:58:30 -04: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."
(let ((res (if (and (> (length results) 0)
(string-equal "{" (substring results 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 12:58:30 -04:00
results ;don't covert dicts to elisp
(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 14:57:31 -05:00
(if (listp res)
(mapcar (lambda (el) (if (eq el 'None)
2021-09-29 03:22:47 -04: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 14:57:31 -05:00
res)
res)))
(defvar org-babel-python-buffers '((:default . "*Python*")))
(defun org-babel-python-session-buffer (session)
"Return the buffer associated with SESSION."
(cdr (assoc session org-babel-python-buffers)))
(defun org-babel-python-with-earmuffs (session)
"Return SESSION name as string, ensuring *...* around."
(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))))
(defun org-babel-python-without-earmuffs (session)
"Return SESSION name as string, without *...* around."
(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)))
(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 12:58:30 -04: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 14:34:06 -04:00
(defun org-babel-python-initiate-session-by-key (&optional session)
2010-07-13 19:20:08 -04:00
"Initiate a python session.
If there is not a current inferior-process-buffer matching
SESSION then create it. If inferior process already
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."
(save-window-excursion
(let* ((session (if session (intern session) :default))
(py-buffer (or (org-babel-python-session-buffer session)
(org-babel-python-with-earmuffs session)))
(cmd (if (member system-type '(cygwin windows-nt ms-dos))
(concat org-babel-python-command " -i")
org-babel-python-command))
(python-shell-buffer-name
(org-babel-python-without-earmuffs py-buffer))
(existing-session-p (comint-check-proc py-buffer)))
(run-python cmd)
(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 12:58:30 -04: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
;; Ensure first prompt. Based on python-tests.el
;; (`python-tests-shell-wait-for-prompt')
(while (not (python-util-comint-end-of-output-p))
(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 12:58:30 -04: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)
(sleep-for 0.010)))
(setq org-babel-python-buffers
(cons (cons session py-buffer)
(assq-delete-all session org-babel-python-buffers)))
session)))
Activate lexical binding in some libraries * lisp/ob-C.el (org-babel-prep-session:C): (org-babel-load-session:C): * lisp/ob-J.el: (org-babel-expand-body:J): (org-babel-execute:J): * lisp/ob-R.el: (org-babel-expand-body:R): * lisp/ob-abc.el: (org-babel-execute:abc): (org-babel-prep-session:abc): * lisp/ob-asymptote.el: (org-babel-execute:asymptote): (org-babel-prep-session:asymptote): * lisp/ob-awk.el: (org-babel-expand-body:awk): * lisp/ob-calc.el: (org-babel-expand-body:calc): * lisp/ob-clojure.el: * lisp/ob-comint.el: (org-babel-comint-in-buffer): (org-babel-comint-with-output): (org-babel-comint-eval-invisibly-and-wait-for-file): * lisp/ob-coq.el: * lisp/ob-css.el: (org-babel-execute:css): (org-babel-prep-session:css): * lisp/ob-ditaa.el: (org-babel-execute:ditaa): (org-babel-prep-session:ditaa): * lisp/ob-dot.el: (org-babel-execute:dot): (org-babel-prep-session:dot): * lisp/ob-ebnf.el: * lisp/ob-emacs-lisp.el: * lisp/ob-eval.el: * lisp/ob-forth.el: * lisp/ob-fortran.el: (org-babel-execute:fortran): (org-babel-prep-session:fortran): (org-babel-load-session:fortran): * lisp/ob-gnuplot.el: (org-babel-expand-body:gnuplot): (org-babel-prep-session:gnuplot): (org-babel-gnuplot-initiate-session): * lisp/ob-groovy.el: (org-babel-prep-session:groovy): (org-babel-groovy-initiate-session): * lisp/ob-haskell.el: (org-babel-haskell-initiate-session): * lisp/ob-io.el: (org-babel-prep-session:io): (org-babel-io-initiate-session): * lisp/ob-java.el: (org-babel-execute:java): * lisp/ob-js.el: * lisp/ob-keys.el: * lisp/ob-latex.el: (org-babel-prep-session:latex): * lisp/ob-ledger.el: (org-babel-execute:ledger): (org-babel-prep-session:ledger): * lisp/ob-lilypond.el: (org-babel-lilypond-commands): (org-babel-lilypond-process-basic): (org-babel-prep-session:lilypond): (org-babel-lilypond-parse-line-num): * lisp/ob-lisp.el: * lisp/ob-makefile.el: (org-babel-execute:makefile): (org-babel-prep-session:makefile): * lisp/ob-matlab.el: * lisp/ob-maxima.el: (org-babel-prep-session:maxima): * lisp/ob-mscgen.el: (org-babel-prep-session:mscgen): * lisp/ob-ocaml.el: (org-babel-execute:ocaml): (org-babel-prep-session:ocaml): * lisp/ob-octave.el: (org-babel-execute:octave): (org-babel-octave-initiate-session): * lisp/ob-org.el: (org-babel-prep-session:org): * lisp/ob-perl.el: (org-babel-prep-session:perl): (org-babel-perl--var-to-perl): (org-babel-perl-initiate-session): * lisp/ob-picolisp.el: (org-babel-expand-body:picolisp): (org-babel-execute:picolisp): * lisp/ob-plantuml.el: (org-babel-execute:plantuml): (org-babel-prep-session:plantuml): * lisp/ob-processing.el: (org-babel-prep-session:processing): * lisp/ob-python.el: (org-babel-python-initiate-session): * lisp/ob-ref.el: (org-babel-ref-resolve): * lisp/ob-ruby.el: (org-babel-ruby-initiate-session): * lisp/ob-sass.el: (org-babel-execute:sass): (org-babel-prep-session:sass): * lisp/ob-scala.el: (org-babel-execute:scala): (org-babel-prep-session:scala): (org-babel-scala-initiate-session): * lisp/ob-scheme.el: * lisp/ob-screen.el: (org-babel-prep-session:screen): (org-babel-screen-session-write-temp-file): (org-babel-screen-test): * lisp/ob.el: * lisp/org-colview.el: (org-columns-todo): (org-columns-set-tags-or-toggle): (org-columns-new): (org-columns-uncompile-format): (org-agenda-colview-summarize): * lisp/org-footnote.el: (electric-indent-mode): * lisp/org-indent.el: (org-indent-refresh-maybe): * lisp/org-list.el: * lisp/org-macro.el: (org-macro--collect-macros): * lisp/org-src.el: * lisp/org-table.el: (sort-fold-case): (org-table-create): (org-table-field-info): (org-table-transpose-table-at-point): (org-table-remove-rectangle-highlight): (orgtbl-create-or-convert-from-region): (org-define-lookup-function): * lisp/ox-ascii.el: (org-ascii-format-drawer-function): (org-ascii--has-caption-p): (org-ascii-bold): (org-ascii-center-block): (org-ascii-clock): (org-ascii-code): (org-ascii-dynamic-block): (org-ascii-entity): (org-ascii-example-block): (org-ascii-export-snippet): (org-ascii-export-block): (org-ascii-fixed-width): (org-ascii-footnote-reference): (org-ascii-horizontal-rule): (org-ascii-inline-src-block): (org-ascii-format-inlinetask-default): (org-ascii-italic): (org-ascii-keyword): (org-ascii-latex-environment): (org-ascii-latex-fragment): (org-ascii-line-break): (org-ascii-node-property): (org-ascii-planning): (org-ascii-quote-block): (org-ascii-radio-target): (org-ascii-special-block): (org-ascii-src-block): (org-ascii-statistics-cookie): (org-ascii-subscript): (org-ascii-superscript): (org-ascii-strike-through): (org-ascii-timestamp): (org-ascii-underline): (org-ascii-verbatim): (org-ascii-verse-block): (org-ascii-filter-headline-blank-lines): (org-ascii-filter-paragraph-spacing): (org-ascii-filter-comment-spacing): Use lexical binding.
2015-10-25 20:56:00 -04:00
(defun org-babel-python-initiate-session (&optional session _params)
"Initiate Python session named SESSION according to PARAMS.
If there is not a current inferior-process-buffer matching
SESSION then create it. If inferior process already
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."
(unless (string= session "none")
(org-babel-python-session-buffer
(org-babel-python-initiate-session-by-key session))))
(defvar org-babel-python-eoe-indicator "org_babel_python_eoe"
"A string to indicate that evaluation has completed.")
(defun org-babel-python-format-session-value
(src-file result-file result-params)
"Return Python code to evaluate SRC-FILE and write result to RESULT-FILE.
RESULT-PARAMS defines the result type."
(format "\
import ast
with open('%s') as __org_babel_python_tmpfile:
__org_babel_python_ast = ast.parse(__org_babel_python_tmpfile.read())
__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 12:58:30 -04:00
__org_babel_python_final = None
__org_babel_python_format_value(__org_babel_python_final, '%s', %s)"
(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 12:58:30 -04:00
(org-babel-python-var-to-python result-params)))
(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 12:58:30 -04:00
(session body &optional result-type result-params preamble async graphics-file)
"Evaluate BODY as Python code."
(if session
ob-comint.el, ob-python.el: Async session evaluation Adds functionality to ob-comint.el to implement async session eval on a per-language basis. Adds a reference implementation for ob-python. * lisp/ob-comint.el (org-babel-comint-with-output): Remove comment. (org-babel-comint-async-indicator, org-babel-comint-async-buffers, org-babel-comint-async-file-callback, org-babel-comint-async-chunk-callback, org-babel-comint-async-dangling): Add buffer-local variables used for async comint evaluation. (org-babel-comint-use-async): Add function to determine whether block should be evaluated asynchronously. (org-babel-comint-async-filter): Add filter function to attach to comint-output-filter-functions for babel async eval. (org-babel-comint-async-register): Add function to setup buffer variables and hooks for session eval. (org-babel-comint-async-delete-dangling-and-eval): Add helper function for async session eval. * lisp/ob-python.el (org-babel-execute:python): Check for async header argument. (org-babel-python-evaluate): Check whether to use async evaluation. (org-babel-python-async-indicator): Add constant for indicating the start/end of async evaluations. (org-babel-python-async-evaluate-session): Add function for Python async eval. * testing/lisp/test-ob-python.el (test-ob-python/async-simple-session-output): Unit test for Python async session eval. (test-ob-python/async-named-output): Unit test that Python async eval can replace named output. (test-ob-python/async-output-drawer): Unit test that Python async eval works with drawer results.
2021-05-18 11:45:05 -04: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 12:58:30 -04:00
session body result-type result-params graphics-file)
ob-comint.el, ob-python.el: Async session evaluation Adds functionality to ob-comint.el to implement async session eval on a per-language basis. Adds a reference implementation for ob-python. * lisp/ob-comint.el (org-babel-comint-with-output): Remove comment. (org-babel-comint-async-indicator, org-babel-comint-async-buffers, org-babel-comint-async-file-callback, org-babel-comint-async-chunk-callback, org-babel-comint-async-dangling): Add buffer-local variables used for async comint evaluation. (org-babel-comint-use-async): Add function to determine whether block should be evaluated asynchronously. (org-babel-comint-async-filter): Add filter function to attach to comint-output-filter-functions for babel async eval. (org-babel-comint-async-register): Add function to setup buffer variables and hooks for session eval. (org-babel-comint-async-delete-dangling-and-eval): Add helper function for async session eval. * lisp/ob-python.el (org-babel-execute:python): Check for async header argument. (org-babel-python-evaluate): Check whether to use async evaluation. (org-babel-python-async-indicator): Add constant for indicating the start/end of async evaluations. (org-babel-python-async-evaluate-session): Add function for Python async eval. * testing/lisp/test-ob-python.el (test-ob-python/async-simple-session-output): Unit test for Python async session eval. (test-ob-python/async-named-output): Unit test that Python async eval can replace named output. (test-ob-python/async-output-drawer): Unit test that Python async eval works with drawer results.
2021-05-18 11:45:05 -04: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 12:58:30 -04:00
session body result-type result-params graphics-file))
(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 12:58:30 -04:00
body result-type result-params preamble graphics-file)))
(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)))
(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 12:58:30 -04:00
(body &optional result-type result-params preamble graphics-file)
"Evaluate BODY in external python process.
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 12:58:30 -04: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 14:57:31 -05:00
(let ((raw
Silence byte-compiler * lisp/ob-core.el (org-babel-process-params): Silence byte-compiler. * lisp/ob-groovy.el (org-babel-groovy-evaluate): * lisp/ob-haskell.el (org-babel-execute:haskell): * lisp/ob-io.el (org-babel-io-evaluate): * lisp/ob-octave.el (org-babel-octave-evaluate-external-process): (org-babel-octave-evaluate-session): * lisp/ob-perl.el (org-babel-perl-evaluate): * lisp/ob-python.el (org-babel-python-evaluate-external-process): (org-babel-python-evaluate-session): * lisp/ob-ruby.el (org-babel-ruby-pp-wrapper-method): (org-babel-ruby-evaluate): * lisp/ob-scala.el: * lisp/ob-tangle.el: (org-babel-tangle-collect-blocks): * lisp/org-agenda.el (org-agenda-get-category-icon): (org-agenda-todo-yesterday): * lisp/org-bbdb.el (org-bbdb-anniv-extract-date): (org-bbdb-make-anniv-hash): (org-bbdb-anniversaries-future): * lisp/org-bibtex.el (org-bibtex-fleshout): (org-bibtex-read): (org-bibtex-write): * lisp/org-capture.el (org-capture-set-target-location): (org-capture-get-indirect-buffer): (org-mks): * lisp/org-clock.el (org-clock--oldest-date): (org-clock-resolve): (org-clock-sum): (org-clock-special-range): (org-clocktable-steps): * lisp/org-ctags.el (org-ctags-create-tags): * lisp/org-element.el (org-element--interpret-affiliated-keywords): (org-element--cache-shift-positions): (org-element--cache-sync): (org-element--cache-submit-request): * lisp/org-habit.el (org-habit-parse-todo): * lisp/org-inlinetask.el (org-inlinetask-hide-tasks): * lisp/org-lint.el (org-lint--generate-reports): * lisp/org-mouse.el (org-mouse-get-context): * lisp/org-plot.el (org-plot/gnuplot-to-grid-data): (org-plot/gnuplot): * lisp/ox-ascii.el (org-ascii--current-text-width): (org-ascii--current-justification): (org-ascii--build-caption): (org-ascii--checkbox): (org-ascii-item): * lisp/ox-html.el (org-html-footnote-section): * lisp/ox-latex.el (org-latex--make-option-string): * lisp/ox-odt.el (org-odt-toc): (org-odt-add-automatic-style): (org-odt-format-label): (org-odt-link--inline-image): (org-odt--render-image/formula): (org-odt--enumerable-image-p): (org-odt--enumerable-latex-image-p): (org-odt--enumerable-formula-p): (org-odt-do-format-code): (org-odt-table-cell): Silence byte-compiler.
2016-07-25 10:34:48 -04:00
(pcase result-type
(`output (org-babel-eval org-babel-python-command
(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 12:58:30 -04: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-"))))
Silence byte-compiler * lisp/ob-core.el (org-babel-process-params): Silence byte-compiler. * lisp/ob-groovy.el (org-babel-groovy-evaluate): * lisp/ob-haskell.el (org-babel-execute:haskell): * lisp/ob-io.el (org-babel-io-evaluate): * lisp/ob-octave.el (org-babel-octave-evaluate-external-process): (org-babel-octave-evaluate-session): * lisp/ob-perl.el (org-babel-perl-evaluate): * lisp/ob-python.el (org-babel-python-evaluate-external-process): (org-babel-python-evaluate-session): * lisp/ob-ruby.el (org-babel-ruby-pp-wrapper-method): (org-babel-ruby-evaluate): * lisp/ob-scala.el: * lisp/ob-tangle.el: (org-babel-tangle-collect-blocks): * lisp/org-agenda.el (org-agenda-get-category-icon): (org-agenda-todo-yesterday): * lisp/org-bbdb.el (org-bbdb-anniv-extract-date): (org-bbdb-make-anniv-hash): (org-bbdb-anniversaries-future): * lisp/org-bibtex.el (org-bibtex-fleshout): (org-bibtex-read): (org-bibtex-write): * lisp/org-capture.el (org-capture-set-target-location): (org-capture-get-indirect-buffer): (org-mks): * lisp/org-clock.el (org-clock--oldest-date): (org-clock-resolve): (org-clock-sum): (org-clock-special-range): (org-clocktable-steps): * lisp/org-ctags.el (org-ctags-create-tags): * lisp/org-element.el (org-element--interpret-affiliated-keywords): (org-element--cache-shift-positions): (org-element--cache-sync): (org-element--cache-submit-request): * lisp/org-habit.el (org-habit-parse-todo): * lisp/org-inlinetask.el (org-inlinetask-hide-tasks): * lisp/org-lint.el (org-lint--generate-reports): * lisp/org-mouse.el (org-mouse-get-context): * lisp/org-plot.el (org-plot/gnuplot-to-grid-data): (org-plot/gnuplot): * lisp/ox-ascii.el (org-ascii--current-text-width): (org-ascii--current-justification): (org-ascii--build-caption): (org-ascii--checkbox): (org-ascii-item): * lisp/ox-html.el (org-html-footnote-section): * lisp/ox-latex.el (org-latex--make-option-string): * lisp/ox-odt.el (org-odt-toc): (org-odt-add-automatic-style): (org-odt-format-label): (org-odt-link--inline-image): (org-odt--render-image/formula): (org-odt--enumerable-image-p): (org-odt--enumerable-latex-image-p): (org-odt--enumerable-formula-p): (org-odt-do-format-code): (org-odt-table-cell): Silence byte-compiler.
2016-07-25 10:34:48 -04:00
(org-babel-eval
org-babel-python-command
(concat
preamble (and preamble "\n")
Silence byte-compiler * lisp/ob-core.el (org-babel-process-params): Silence byte-compiler. * lisp/ob-groovy.el (org-babel-groovy-evaluate): * lisp/ob-haskell.el (org-babel-execute:haskell): * lisp/ob-io.el (org-babel-io-evaluate): * lisp/ob-octave.el (org-babel-octave-evaluate-external-process): (org-babel-octave-evaluate-session): * lisp/ob-perl.el (org-babel-perl-evaluate): * lisp/ob-python.el (org-babel-python-evaluate-external-process): (org-babel-python-evaluate-session): * lisp/ob-ruby.el (org-babel-ruby-pp-wrapper-method): (org-babel-ruby-evaluate): * lisp/ob-scala.el: * lisp/ob-tangle.el: (org-babel-tangle-collect-blocks): * lisp/org-agenda.el (org-agenda-get-category-icon): (org-agenda-todo-yesterday): * lisp/org-bbdb.el (org-bbdb-anniv-extract-date): (org-bbdb-make-anniv-hash): (org-bbdb-anniversaries-future): * lisp/org-bibtex.el (org-bibtex-fleshout): (org-bibtex-read): (org-bibtex-write): * lisp/org-capture.el (org-capture-set-target-location): (org-capture-get-indirect-buffer): (org-mks): * lisp/org-clock.el (org-clock--oldest-date): (org-clock-resolve): (org-clock-sum): (org-clock-special-range): (org-clocktable-steps): * lisp/org-ctags.el (org-ctags-create-tags): * lisp/org-element.el (org-element--interpret-affiliated-keywords): (org-element--cache-shift-positions): (org-element--cache-sync): (org-element--cache-submit-request): * lisp/org-habit.el (org-habit-parse-todo): * lisp/org-inlinetask.el (org-inlinetask-hide-tasks): * lisp/org-lint.el (org-lint--generate-reports): * lisp/org-mouse.el (org-mouse-get-context): * lisp/org-plot.el (org-plot/gnuplot-to-grid-data): (org-plot/gnuplot): * lisp/ox-ascii.el (org-ascii--current-text-width): (org-ascii--current-justification): (org-ascii--build-caption): (org-ascii--checkbox): (org-ascii-item): * lisp/ox-html.el (org-html-footnote-section): * lisp/ox-latex.el (org-latex--make-option-string): * lisp/ox-odt.el (org-odt-toc): (org-odt-add-automatic-style): (org-odt-format-label): (org-odt-link--inline-image): (org-odt--render-image/formula): (org-odt--enumerable-image-p): (org-odt--enumerable-latex-image-p): (org-odt--enumerable-formula-p): (org-odt-do-format-code): (org-odt-table-cell): Silence byte-compiler.
2016-07-25 10:34:48 -04: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 12:58:30 -04: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 14:57:31 -05: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 12:58:30 -04:00
(org-babel-python-table-or-string raw))))
(defun org-babel-python-send-string (session body)
"Pass BODY to the Python process in SESSION.
Return output."
(with-current-buffer session
(let* ((string-buffer "")
(comint-output-filter-functions
(cons (lambda (text) (setq string-buffer
(concat string-buffer text)))
comint-output-filter-functions))
(body (format "\
try:
%s
except:
raise
finally:
print('%s')"
(org-babel-python--shift-right body 4)
org-babel-python-eoe-indicator)))
(let ((python-shell-buffer-name
(org-babel-python-without-earmuffs session)))
(python-shell-send-string body))
;; same as `python-shell-comint-end-of-output-p' in emacs-25.1+
(while (not (and (python-shell-comint-end-of-output-p string-buffer)
(string-match
org-babel-python-eoe-indicator
string-buffer)))
(accept-process-output (get-buffer-process (current-buffer))))
(org-babel-chomp (substring string-buffer 0 (match-beginning 0))))))
(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 12:58:30 -04:00
(session body &optional result-type result-params graphics-file)
"Pass BODY to the Python process in SESSION.
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 12:58:30 -04: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."
(let* ((tmp-src-file (org-babel-temp-file "python-"))
(results
(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 12:58:30 -04: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)))
(pcase result-type
(`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 12:58:30 -04:00
(let ((body (format "\
with open('%s') as f:
exec(compile(f.read(), f.name, 'exec'))"
(org-babel-process-file-name
tmp-src-file 'noquote))))
(org-babel-python-send-string session body)))
(`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 12:58:30 -04:00
(let* ((results-file (or graphics-file
(org-babel-temp-file "python-")))
(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 12:58:30 -04:00
tmp-src-file results-file result-params)))
(org-babel-python-send-string session body)
(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 12:58:30 -04:00
(org-babel-eval-read-file results-file)))))))
(org-babel-result-cond result-params
results
(org-babel-python-table-or-string results))))
2009-06-12 19:23:28 -04:00
(defun org-babel-python-read-string (string)
"Strip \\='s from around Python STRING."
(if (and (string-prefix-p "'" string)
(string-suffix-p "'" string))
(substring string 1 -1)
2009-06-12 19:23:28 -04:00
string))
ob-comint.el, ob-python.el: Async session evaluation Adds functionality to ob-comint.el to implement async session eval on a per-language basis. Adds a reference implementation for ob-python. * lisp/ob-comint.el (org-babel-comint-with-output): Remove comment. (org-babel-comint-async-indicator, org-babel-comint-async-buffers, org-babel-comint-async-file-callback, org-babel-comint-async-chunk-callback, org-babel-comint-async-dangling): Add buffer-local variables used for async comint evaluation. (org-babel-comint-use-async): Add function to determine whether block should be evaluated asynchronously. (org-babel-comint-async-filter): Add filter function to attach to comint-output-filter-functions for babel async eval. (org-babel-comint-async-register): Add function to setup buffer variables and hooks for session eval. (org-babel-comint-async-delete-dangling-and-eval): Add helper function for async session eval. * lisp/ob-python.el (org-babel-execute:python): Check for async header argument. (org-babel-python-evaluate): Check whether to use async evaluation. (org-babel-python-async-indicator): Add constant for indicating the start/end of async evaluations. (org-babel-python-async-evaluate-session): Add function for Python async eval. * testing/lisp/test-ob-python.el (test-ob-python/async-simple-session-output): Unit test for Python async session eval. (test-ob-python/async-named-output): Unit test that Python async eval can replace named output. (test-ob-python/async-output-drawer): Unit test that Python async eval works with drawer results.
2021-05-18 11:45:05 -04: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 12:58:30 -04:00
(session body &optional result-type result-params graphics-file)
ob-comint.el, ob-python.el: Async session evaluation Adds functionality to ob-comint.el to implement async session eval on a per-language basis. Adds a reference implementation for ob-python. * lisp/ob-comint.el (org-babel-comint-with-output): Remove comment. (org-babel-comint-async-indicator, org-babel-comint-async-buffers, org-babel-comint-async-file-callback, org-babel-comint-async-chunk-callback, org-babel-comint-async-dangling): Add buffer-local variables used for async comint evaluation. (org-babel-comint-use-async): Add function to determine whether block should be evaluated asynchronously. (org-babel-comint-async-filter): Add filter function to attach to comint-output-filter-functions for babel async eval. (org-babel-comint-async-register): Add function to setup buffer variables and hooks for session eval. (org-babel-comint-async-delete-dangling-and-eval): Add helper function for async session eval. * lisp/ob-python.el (org-babel-execute:python): Check for async header argument. (org-babel-python-evaluate): Check whether to use async evaluation. (org-babel-python-async-indicator): Add constant for indicating the start/end of async evaluations. (org-babel-python-async-evaluate-session): Add function for Python async eval. * testing/lisp/test-ob-python.el (test-ob-python/async-simple-session-output): Unit test for Python async session eval. (test-ob-python/async-named-output): Unit test that Python async eval can replace named output. (test-ob-python/async-output-drawer): Unit test that Python async eval works with drawer results.
2021-05-18 11:45:05 -04: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)
"ob_comint_async_python_\\(.+\\)_\\(.+\\)"
'org-babel-chomp 'org-babel-python-async-value-callback)
(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 12:58:30 -04:00
(insert (if graphics-file
(format org-babel-python--output-graphics-wrapper
body graphics-file)
body))
(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 12:58:30 -04:00
(let ((results-file (or graphics-file
(org-babel-temp-file "python-")))
(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 12:58:30 -04:00
(insert (org-babel-python-format-session-value
tmp-src-file results-file result-params))
(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 12:58:30 -04:00
(unless graphics-file
(insert (format org-babel-python-async-indicator "file" results-file)))
(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 12:58:30 -04:00
results-file))))
ob-comint.el, ob-python.el: Async session evaluation Adds functionality to ob-comint.el to implement async session eval on a per-language basis. Adds a reference implementation for ob-python. * lisp/ob-comint.el (org-babel-comint-with-output): Remove comment. (org-babel-comint-async-indicator, org-babel-comint-async-buffers, org-babel-comint-async-file-callback, org-babel-comint-async-chunk-callback, org-babel-comint-async-dangling): Add buffer-local variables used for async comint evaluation. (org-babel-comint-use-async): Add function to determine whether block should be evaluated asynchronously. (org-babel-comint-async-filter): Add filter function to attach to comint-output-filter-functions for babel async eval. (org-babel-comint-async-register): Add function to setup buffer variables and hooks for session eval. (org-babel-comint-async-delete-dangling-and-eval): Add helper function for async session eval. * lisp/ob-python.el (org-babel-execute:python): Check for async header argument. (org-babel-python-evaluate): Check whether to use async evaluation. (org-babel-python-async-indicator): Add constant for indicating the start/end of async evaluations. (org-babel-python-async-evaluate-session): Add function for Python async eval. * testing/lisp/test-ob-python.el (test-ob-python/async-simple-session-output): Unit test for Python async session eval. (test-ob-python/async-named-output): Unit test that Python async eval can replace named output. (test-ob-python/async-output-drawer): Unit test that Python async eval works with drawer results.
2021-05-18 11:45:05 -04:00
(provide 'ob-python)
;;; ob-python.el ends here