org-mode/lisp/ob-python.el

386 lines
14 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-26 00:56:00 +00:00
;;; ob-python.el --- Babel Functions for Python -*- lexical-binding: t; -*-
2020-01-01 18:38:46 +00:00
;; Copyright (C) 2009-2020 Free Software Foundation, Inc.
2012-04-01 22:53:28 +00:00
;; Authors: Eric Schulte
;; Dan Davison
2020-09-05 00:59:18 +00:00
;; Maintainer: Jack Kamm <jackkamm@gmail.com>
;; Keywords: literate programming, reproducible research
;; Homepage: 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 'ob)
(require 'org-macs)
(require 'python)
(declare-function py-shell "ext:python-mode" (&rest args))
(declare-function py-toggle-shells "ext:python-mode" (arg))
(declare-function py-shell-send-string "ext:python-mode" (strg &optional process))
(defvar org-babel-tangle-lang-exts)
(add-to-list 'org-babel-tangle-lang-exts '("python" . "py"))
(defvar org-babel-default-header-args:python '())
(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-mode
(if (featurep 'python-mode) 'python-mode 'python)
"Preferred python mode for use in running python interactively.
This will typically be either `python' or `python-mode'."
:group 'org-babel
: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 13:05:18 +00:00
:type 'symbol)
(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 15:34:16 +00: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 15:34:16 +00: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 13:05:18 +00:00
:type 'symbol)
(defun org-babel-execute:python (body params)
2010-07-13 23:20:08 +00:00
"Execute a block of Python code with Babel.
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 17:45:15 +00:00
(cdr (assq :session params))))
(result-params (cdr (assq :result-params params)))
(result-type (cdr (assq :result-type params)))
(return-val (when (and (eq result-type 'value) (not 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 17:45:15 +00:00
(cdr (assq :return params))))
(preamble (cdr (assq :preamble params)))
(full-body
(org-babel-expand-body:generic
(concat body (if return-val (format "\nreturn %s" return-val) ""))
params (org-babel-variable-assignments:python params)))
(result (org-babel-python-evaluate
session full-body result-type result-params preamble)))
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 10:21:28 +00: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 17:45:15 +00:00
(org-babel-pick-name (cdr (assq :colname-names params))
(cdr (assq :colnames params)))
(org-babel-pick-name (cdr (assq :rowname-names params))
(cdr (assq :rownames params))))))
2009-06-14 18:34:06 +00: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 22:27:53 +00:00
VARS contains resolved variable references."
2009-06-14 18:34:06 +00:00
(let* ((session (org-babel-python-initiate-session session))
(var-lines
(org-babel-variable-assignments:python params)))
2009-06-14 18:34:06 +00:00
(org-babel-comint-in-buffer session
(mapc (lambda (var)
(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 21:57:37 +00: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 18:34:06 +00:00
;; helper functions
(defun org-babel-variable-assignments:python (params)
"Return a list of Python statements assigning the block's variables."
(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 19:26:11 +00:00
(org-babel--get-vars params)))
(defun org-babel-python-var-to-python (var)
2010-07-13 23:20:08 +00:00
"Convert an elisp value to a python variable.
Convert an elisp value, VAR, into a string of python source code
specifying a variable of the same value."
(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 16:10:55 +00:00
(if (stringp var) (substring-no-properties var) var)))))
(defun org-babel-python-table-or-string (results)
2010-07-13 23:20:08 +00:00
"Convert RESULTS into an appropriate elisp value.
If the results look like a list or tuple, then convert them into an
Emacs-lisp table, otherwise return the results as a string."
Backport changes from Emacs revs 115081 and 115082 2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca> Address some byte-compiler warnings. * ob-abc.el (org-babel-expand-body:abc): Use dolist. (org-babel-execute:abc): Fix regexp quoting. * ob-calc.el (org--var-syms): Rename from `var-syms'. * ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding. * ob-table.el (sbe): Move debug declaration. * org-clock.el (org--msg-extra): Rename from `msg-extra'. * org.el (org-version): Avoid var name starting with _. (org-inhibit-startup, org-called-with-limited-levels) (org-link-search-inhibit-query, org-time-was-given) (org-end-time-was-given, org-def, org-defdecode, org-with-time): * org-colview.el (org-agenda-overriding-columns-format): * org-agenda.el (org-agenda-multi, org-depend-tag-blocked) (org-agenda-show-log-scoped): * ob-python.el (py-which-bufname, python-shell-buffer-name): * ob-haskell.el (org-export-copy-to-kill-ring): * ob-exp.el (org-link-search-inhibit-query): * ob-R.el (ess-eval-visibly-p): * ob-core.el (org-src-window-setup): Declare before use. (org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'. * ox-odt.el (org-odt-hfy-face-to-css): * org-src.el (org-src-associate-babel-session, org-src-get-lang-mode): * org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex) (org-bibtex-check): * ob-tangle.el (org-babel-tangle, org-babel-spec-to-string) (org-babel-tangle-single-block, org-babel-tangle-comment-links): * ob-table.el (sbe): * ob-sqlite.el (org-babel-sqlite-expand-vars): * ob-sql.el (org-babel-sql-expand-vars): * ob-shen.el (org-babel-execute:shen): * ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate): * ob-scala.el (org-babel-scala-evaluate): * ob-ruby.el (org-babel-ruby-table-or-string) (org-babel-ruby-evaluate): * ob-python.el (org-babel-python-table-or-string) (org-babel-python-evaluate-external-process) (org-babel-python-evaluate-session): * ob-picolisp.el (org-babel-execute:picolisp): * ob-perl.el (org-babel-perl-evaluate): * ob-maxima.el (org-babel-execute:maxima): * ob-lisp.el (org-babel-execute:lisp): * ob-java.el (org-babel-execute:java): * ob-io.el (org-babel-io-evaluate): * ob-haskell.el (org-babel-execute:haskell): * ob-fortran.el (org-babel-execute:fortran): * ob-exp.el (org-babel-exp-code): * ob-emacs-lisp.el (org-babel-execute:emacs-lisp): * ob-ditaa.el (org-babel-execute:ditaa): * ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash) (org-babel-parse-header-arguments, org-babel-reassemble-table) (org-babel-goto-src-block-head, org-babel-mark-block) (org-babel-expand-noweb-references, org-babel-script-escape) (org-babel-process-file-name): * ob-clojure.el (org-babel-execute:clojure): * ob-calc.el (org-babel-execute:calc): * ob-awk.el (org-babel-execute:awk): * ob-abc.el (org-babel-execute:abc): * ob-R.el (org-babel-expand-body:R): * ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...). 2013-11-12 Glenn Morris <rgm@gnu.org> * ox-html.el (org-html-scripts): Add 2013 to copyright years. (org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
(let ((res (org-babel-script-escape results)))
(if (listp res)
(mapcar (lambda (el) (if (eq el 'None)
Backport changes from Emacs revs 115081 and 115082 2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca> Address some byte-compiler warnings. * ob-abc.el (org-babel-expand-body:abc): Use dolist. (org-babel-execute:abc): Fix regexp quoting. * ob-calc.el (org--var-syms): Rename from `var-syms'. * ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding. * ob-table.el (sbe): Move debug declaration. * org-clock.el (org--msg-extra): Rename from `msg-extra'. * org.el (org-version): Avoid var name starting with _. (org-inhibit-startup, org-called-with-limited-levels) (org-link-search-inhibit-query, org-time-was-given) (org-end-time-was-given, org-def, org-defdecode, org-with-time): * org-colview.el (org-agenda-overriding-columns-format): * org-agenda.el (org-agenda-multi, org-depend-tag-blocked) (org-agenda-show-log-scoped): * ob-python.el (py-which-bufname, python-shell-buffer-name): * ob-haskell.el (org-export-copy-to-kill-ring): * ob-exp.el (org-link-search-inhibit-query): * ob-R.el (ess-eval-visibly-p): * ob-core.el (org-src-window-setup): Declare before use. (org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'. * ox-odt.el (org-odt-hfy-face-to-css): * org-src.el (org-src-associate-babel-session, org-src-get-lang-mode): * org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex) (org-bibtex-check): * ob-tangle.el (org-babel-tangle, org-babel-spec-to-string) (org-babel-tangle-single-block, org-babel-tangle-comment-links): * ob-table.el (sbe): * ob-sqlite.el (org-babel-sqlite-expand-vars): * ob-sql.el (org-babel-sql-expand-vars): * ob-shen.el (org-babel-execute:shen): * ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate): * ob-scala.el (org-babel-scala-evaluate): * ob-ruby.el (org-babel-ruby-table-or-string) (org-babel-ruby-evaluate): * ob-python.el (org-babel-python-table-or-string) (org-babel-python-evaluate-external-process) (org-babel-python-evaluate-session): * ob-picolisp.el (org-babel-execute:picolisp): * ob-perl.el (org-babel-perl-evaluate): * ob-maxima.el (org-babel-execute:maxima): * ob-lisp.el (org-babel-execute:lisp): * ob-java.el (org-babel-execute:java): * ob-io.el (org-babel-io-evaluate): * ob-haskell.el (org-babel-execute:haskell): * ob-fortran.el (org-babel-execute:fortran): * ob-exp.el (org-babel-exp-code): * ob-emacs-lisp.el (org-babel-execute:emacs-lisp): * ob-ditaa.el (org-babel-execute:ditaa): * ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash) (org-babel-parse-header-arguments, org-babel-reassemble-table) (org-babel-goto-src-block-head, org-babel-mark-block) (org-babel-expand-noweb-references, org-babel-script-escape) (org-babel-process-file-name): * ob-clojure.el (org-babel-execute:clojure): * ob-calc.el (org-babel-execute:calc): * ob-awk.el (org-babel-execute:awk): * ob-abc.el (org-babel-execute:abc): * ob-R.el (org-babel-expand-body:R): * ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...). 2013-11-12 Glenn Morris <rgm@gnu.org> * ox-html.el (org-html-scripts): Add 2013 to copyright years. (org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
org-babel-python-None-to el))
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)
(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)
(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 py-default-interpreter)
Backport changes from Emacs revs 115081 and 115082 2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca> Address some byte-compiler warnings. * ob-abc.el (org-babel-expand-body:abc): Use dolist. (org-babel-execute:abc): Fix regexp quoting. * ob-calc.el (org--var-syms): Rename from `var-syms'. * ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding. * ob-table.el (sbe): Move debug declaration. * org-clock.el (org--msg-extra): Rename from `msg-extra'. * org.el (org-version): Avoid var name starting with _. (org-inhibit-startup, org-called-with-limited-levels) (org-link-search-inhibit-query, org-time-was-given) (org-end-time-was-given, org-def, org-defdecode, org-with-time): * org-colview.el (org-agenda-overriding-columns-format): * org-agenda.el (org-agenda-multi, org-depend-tag-blocked) (org-agenda-show-log-scoped): * ob-python.el (py-which-bufname, python-shell-buffer-name): * ob-haskell.el (org-export-copy-to-kill-ring): * ob-exp.el (org-link-search-inhibit-query): * ob-R.el (ess-eval-visibly-p): * ob-core.el (org-src-window-setup): Declare before use. (org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'. * ox-odt.el (org-odt-hfy-face-to-css): * org-src.el (org-src-associate-babel-session, org-src-get-lang-mode): * org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex) (org-bibtex-check): * ob-tangle.el (org-babel-tangle, org-babel-spec-to-string) (org-babel-tangle-single-block, org-babel-tangle-comment-links): * ob-table.el (sbe): * ob-sqlite.el (org-babel-sqlite-expand-vars): * ob-sql.el (org-babel-sql-expand-vars): * ob-shen.el (org-babel-execute:shen): * ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate): * ob-scala.el (org-babel-scala-evaluate): * ob-ruby.el (org-babel-ruby-table-or-string) (org-babel-ruby-evaluate): * ob-python.el (org-babel-python-table-or-string) (org-babel-python-evaluate-external-process) (org-babel-python-evaluate-session): * ob-picolisp.el (org-babel-execute:picolisp): * ob-perl.el (org-babel-perl-evaluate): * ob-maxima.el (org-babel-execute:maxima): * ob-lisp.el (org-babel-execute:lisp): * ob-java.el (org-babel-execute:java): * ob-io.el (org-babel-io-evaluate): * ob-haskell.el (org-babel-execute:haskell): * ob-fortran.el (org-babel-execute:fortran): * ob-exp.el (org-babel-exp-code): * ob-emacs-lisp.el (org-babel-execute:emacs-lisp): * ob-ditaa.el (org-babel-execute:ditaa): * ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash) (org-babel-parse-header-arguments, org-babel-reassemble-table) (org-babel-goto-src-block-head, org-babel-mark-block) (org-babel-expand-noweb-references, org-babel-script-escape) (org-babel-process-file-name): * ob-clojure.el (org-babel-execute:clojure): * ob-calc.el (org-babel-execute:calc): * ob-awk.el (org-babel-execute:awk): * ob-abc.el (org-babel-execute:abc): * ob-R.el (org-babel-expand-body:R): * ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...). 2013-11-12 Glenn Morris <rgm@gnu.org> * ox-html.el (org-html-scripts): Add 2013 to copyright years. (org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
(defvar py-which-bufname)
(defvar python-shell-buffer-name)
2009-06-14 18:34:06 +00:00
(defun org-babel-python-initiate-session-by-key (&optional session)
2010-07-13 23:20:08 +00:00
"Initiate a python session.
If there is not a current inferior-process-buffer in SESSION
then create. Return the initialized session."
(save-window-excursion
(let* ((session (if session (intern session) :default))
(py-buffer (org-babel-python-session-buffer session))
(cmd (if (member system-type '(cygwin windows-nt ms-dos))
(concat org-babel-python-command " -i")
org-babel-python-command)))
(cond
((eq 'python org-babel-python-mode) ; python.el
(unless py-buffer
(setq py-buffer (org-babel-python-with-earmuffs session)))
(let ((python-shell-buffer-name
(org-babel-python-without-earmuffs py-buffer)))
(run-python cmd)
(sleep-for 0 10)))
((and (eq 'python-mode org-babel-python-mode)
(fboundp 'py-shell)) ; python-mode.el
(require 'python-mode)
;; Make sure that py-which-bufname is initialized, as otherwise
;; it will be overwritten the first time a Python buffer is
;; created.
(py-toggle-shells py-default-interpreter)
;; `py-shell' creates a buffer whose name is the value of
;; `py-which-bufname' with '*'s at the beginning and end
(let* ((bufname (if (and py-buffer (buffer-live-p py-buffer))
(replace-regexp-in-string ;; zap surrounding *
"^\\*\\([^*]+\\)\\*$" "\\1" py-buffer)
(concat "Python-" (symbol-name session))))
(py-which-bufname bufname))
(setq py-buffer (org-babel-python-with-earmuffs bufname))
(py-shell nil nil t org-babel-python-command py-buffer nil nil t nil)))
(t
(error "No function available for running an inferior Python")))
(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-26 00:56:00 +00:00
(defun org-babel-python-initiate-session (&optional session _params)
"Create a session named SESSION according to PARAMS."
(unless (string= session "none")
(org-babel-python-session-buffer
(org-babel-python-initiate-session-by-key session))))
(defconst org-babel-python-wrapper-method
"
def main():
%s
open('%s', 'w').write( str(main()) )")
(defconst org-babel-python-pp-wrapper-method
"
import pprint
def main():
%s
open('%s', 'w').write( pprint.pformat(main()) )")
(defconst org-babel-python--exec-tmpfile "\
with open('%s') as f:
exec(compile(f.read(), f.name, 'exec'))"
"Template for Python session command with output results.
Has a single %s escape, the tempfile containing the source code
to evaluate.")
(defconst org-babel-python--eval-ast "\
import ast
with open('%s') as f:
__org_babel_python_ast = ast.parse(f.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'))
with open('%s', 'w') as f:
if %s:
import pprint
f.write(pprint.pformat(__org_babel_python_final))
else:
f.write(str(__org_babel_python_final))
else:
exec(compile(__org_babel_python_ast, '<string>', 'exec'))
__org_babel_python_final = None"
"Template for Python session command with value results.
Has three %s escapes to be filled in:
1. Tempfile containing source to evaluate.
2. Tempfile to write results to.
3. Whether to pretty print, \"True\" or \"False\".")
(defun org-babel-python-evaluate
(session body &optional result-type result-params preamble)
"Evaluate BODY as Python code."
(if session
(org-babel-python-evaluate-session
session body result-type result-params)
(org-babel-python-evaluate-external-process
body result-type result-params preamble)))
(defun org-babel-python-evaluate-external-process
(body &optional result-type result-params preamble)
"Evaluate BODY in external python process.
If RESULT-TYPE equals `output' then return standard output as a
string. If RESULT-TYPE equals `value' then return the value of the
last statement in BODY, as elisp."
Backport changes from Emacs revs 115081 and 115082 2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca> Address some byte-compiler warnings. * ob-abc.el (org-babel-expand-body:abc): Use dolist. (org-babel-execute:abc): Fix regexp quoting. * ob-calc.el (org--var-syms): Rename from `var-syms'. * ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding. * ob-table.el (sbe): Move debug declaration. * org-clock.el (org--msg-extra): Rename from `msg-extra'. * org.el (org-version): Avoid var name starting with _. (org-inhibit-startup, org-called-with-limited-levels) (org-link-search-inhibit-query, org-time-was-given) (org-end-time-was-given, org-def, org-defdecode, org-with-time): * org-colview.el (org-agenda-overriding-columns-format): * org-agenda.el (org-agenda-multi, org-depend-tag-blocked) (org-agenda-show-log-scoped): * ob-python.el (py-which-bufname, python-shell-buffer-name): * ob-haskell.el (org-export-copy-to-kill-ring): * ob-exp.el (org-link-search-inhibit-query): * ob-R.el (ess-eval-visibly-p): * ob-core.el (org-src-window-setup): Declare before use. (org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'. * ox-odt.el (org-odt-hfy-face-to-css): * org-src.el (org-src-associate-babel-session, org-src-get-lang-mode): * org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex) (org-bibtex-check): * ob-tangle.el (org-babel-tangle, org-babel-spec-to-string) (org-babel-tangle-single-block, org-babel-tangle-comment-links): * ob-table.el (sbe): * ob-sqlite.el (org-babel-sqlite-expand-vars): * ob-sql.el (org-babel-sql-expand-vars): * ob-shen.el (org-babel-execute:shen): * ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate): * ob-scala.el (org-babel-scala-evaluate): * ob-ruby.el (org-babel-ruby-table-or-string) (org-babel-ruby-evaluate): * ob-python.el (org-babel-python-table-or-string) (org-babel-python-evaluate-external-process) (org-babel-python-evaluate-session): * ob-picolisp.el (org-babel-execute:picolisp): * ob-perl.el (org-babel-perl-evaluate): * ob-maxima.el (org-babel-execute:maxima): * ob-lisp.el (org-babel-execute:lisp): * ob-java.el (org-babel-execute:java): * ob-io.el (org-babel-io-evaluate): * ob-haskell.el (org-babel-execute:haskell): * ob-fortran.el (org-babel-execute:fortran): * ob-exp.el (org-babel-exp-code): * ob-emacs-lisp.el (org-babel-execute:emacs-lisp): * ob-ditaa.el (org-babel-execute:ditaa): * ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash) (org-babel-parse-header-arguments, org-babel-reassemble-table) (org-babel-goto-src-block-head, org-babel-mark-block) (org-babel-expand-noweb-references, org-babel-script-escape) (org-babel-process-file-name): * ob-clojure.el (org-babel-execute:clojure): * ob-calc.el (org-babel-execute:calc): * ob-awk.el (org-babel-execute:awk): * ob-abc.el (org-babel-execute:abc): * ob-R.el (org-babel-expand-body:R): * ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...). 2013-11-12 Glenn Morris <rgm@gnu.org> * ox-html.el (org-html-scripts): Add 2013 to copyright years. (org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
(let ((raw
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 14:34:48 +00:00
(pcase result-type
(`output (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 14:34:48 +00:00
body)))
(`value (let ((tmp-file (org-babel-temp-file "python-")))
(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 14:34:48 +00:00
(format
(if (member "pp" result-params)
org-babel-python-pp-wrapper-method
org-babel-python-wrapper-method)
(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)))
(forward-line 1))
(buffer-string))
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 14:34:48 +00:00
(org-babel-process-file-name tmp-file 'noquote))))
(org-babel-eval-read-file tmp-file))))))
Backport changes from Emacs revs 115081 and 115082 2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca> Address some byte-compiler warnings. * ob-abc.el (org-babel-expand-body:abc): Use dolist. (org-babel-execute:abc): Fix regexp quoting. * ob-calc.el (org--var-syms): Rename from `var-syms'. * ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding. * ob-table.el (sbe): Move debug declaration. * org-clock.el (org--msg-extra): Rename from `msg-extra'. * org.el (org-version): Avoid var name starting with _. (org-inhibit-startup, org-called-with-limited-levels) (org-link-search-inhibit-query, org-time-was-given) (org-end-time-was-given, org-def, org-defdecode, org-with-time): * org-colview.el (org-agenda-overriding-columns-format): * org-agenda.el (org-agenda-multi, org-depend-tag-blocked) (org-agenda-show-log-scoped): * ob-python.el (py-which-bufname, python-shell-buffer-name): * ob-haskell.el (org-export-copy-to-kill-ring): * ob-exp.el (org-link-search-inhibit-query): * ob-R.el (ess-eval-visibly-p): * ob-core.el (org-src-window-setup): Declare before use. (org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'. * ox-odt.el (org-odt-hfy-face-to-css): * org-src.el (org-src-associate-babel-session, org-src-get-lang-mode): * org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex) (org-bibtex-check): * ob-tangle.el (org-babel-tangle, org-babel-spec-to-string) (org-babel-tangle-single-block, org-babel-tangle-comment-links): * ob-table.el (sbe): * ob-sqlite.el (org-babel-sqlite-expand-vars): * ob-sql.el (org-babel-sql-expand-vars): * ob-shen.el (org-babel-execute:shen): * ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate): * ob-scala.el (org-babel-scala-evaluate): * ob-ruby.el (org-babel-ruby-table-or-string) (org-babel-ruby-evaluate): * ob-python.el (org-babel-python-table-or-string) (org-babel-python-evaluate-external-process) (org-babel-python-evaluate-session): * ob-picolisp.el (org-babel-execute:picolisp): * ob-perl.el (org-babel-perl-evaluate): * ob-maxima.el (org-babel-execute:maxima): * ob-lisp.el (org-babel-execute:lisp): * ob-java.el (org-babel-execute:java): * ob-io.el (org-babel-io-evaluate): * ob-haskell.el (org-babel-execute:haskell): * ob-fortran.el (org-babel-execute:fortran): * ob-exp.el (org-babel-exp-code): * ob-emacs-lisp.el (org-babel-execute:emacs-lisp): * ob-ditaa.el (org-babel-execute:ditaa): * ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash) (org-babel-parse-header-arguments, org-babel-reassemble-table) (org-babel-goto-src-block-head, org-babel-mark-block) (org-babel-expand-noweb-references, org-babel-script-escape) (org-babel-process-file-name): * ob-clojure.el (org-babel-execute:clojure): * ob-calc.el (org-babel-execute:calc): * ob-awk.el (org-babel-execute:awk): * ob-abc.el (org-babel-execute:abc): * ob-R.el (org-babel-expand-body:R): * ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...). 2013-11-12 Glenn Morris <rgm@gnu.org> * ox-html.el (org-html-scripts): Add 2013 to copyright years. (org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
(org-babel-result-cond result-params
raw
(org-babel-python-table-or-string (org-trim 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)))
(if (not (eq 'python-mode org-babel-python-mode))
(let ((python-shell-buffer-name
(org-babel-python-without-earmuffs session)))
(python-shell-send-string body))
(require 'python-mode)
(py-shell-send-string body (get-buffer-process session)))
;; same as `python-shell-comint-end-of-output-p' in emacs-25.1+
(while (not (string-match
(concat "\r?\n?"
(replace-regexp-in-string
(rx string-start ?^) "" comint-prompt-regexp)
(rx eos))
string-buffer))
(accept-process-output (get-buffer-process (current-buffer))))
(substring string-buffer 0 (match-beginning 0)))))
(defun org-babel-python-evaluate-session
Backport changes from Emacs revs 115081 and 115082 2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca> Address some byte-compiler warnings. * ob-abc.el (org-babel-expand-body:abc): Use dolist. (org-babel-execute:abc): Fix regexp quoting. * ob-calc.el (org--var-syms): Rename from `var-syms'. * ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding. * ob-table.el (sbe): Move debug declaration. * org-clock.el (org--msg-extra): Rename from `msg-extra'. * org.el (org-version): Avoid var name starting with _. (org-inhibit-startup, org-called-with-limited-levels) (org-link-search-inhibit-query, org-time-was-given) (org-end-time-was-given, org-def, org-defdecode, org-with-time): * org-colview.el (org-agenda-overriding-columns-format): * org-agenda.el (org-agenda-multi, org-depend-tag-blocked) (org-agenda-show-log-scoped): * ob-python.el (py-which-bufname, python-shell-buffer-name): * ob-haskell.el (org-export-copy-to-kill-ring): * ob-exp.el (org-link-search-inhibit-query): * ob-R.el (ess-eval-visibly-p): * ob-core.el (org-src-window-setup): Declare before use. (org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'. * ox-odt.el (org-odt-hfy-face-to-css): * org-src.el (org-src-associate-babel-session, org-src-get-lang-mode): * org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex) (org-bibtex-check): * ob-tangle.el (org-babel-tangle, org-babel-spec-to-string) (org-babel-tangle-single-block, org-babel-tangle-comment-links): * ob-table.el (sbe): * ob-sqlite.el (org-babel-sqlite-expand-vars): * ob-sql.el (org-babel-sql-expand-vars): * ob-shen.el (org-babel-execute:shen): * ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate): * ob-scala.el (org-babel-scala-evaluate): * ob-ruby.el (org-babel-ruby-table-or-string) (org-babel-ruby-evaluate): * ob-python.el (org-babel-python-table-or-string) (org-babel-python-evaluate-external-process) (org-babel-python-evaluate-session): * ob-picolisp.el (org-babel-execute:picolisp): * ob-perl.el (org-babel-perl-evaluate): * ob-maxima.el (org-babel-execute:maxima): * ob-lisp.el (org-babel-execute:lisp): * ob-java.el (org-babel-execute:java): * ob-io.el (org-babel-io-evaluate): * ob-haskell.el (org-babel-execute:haskell): * ob-fortran.el (org-babel-execute:fortran): * ob-exp.el (org-babel-exp-code): * ob-emacs-lisp.el (org-babel-execute:emacs-lisp): * ob-ditaa.el (org-babel-execute:ditaa): * ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash) (org-babel-parse-header-arguments, org-babel-reassemble-table) (org-babel-goto-src-block-head, org-babel-mark-block) (org-babel-expand-noweb-references, org-babel-script-escape) (org-babel-process-file-name): * ob-clojure.el (org-babel-execute:clojure): * ob-calc.el (org-babel-execute:calc): * ob-awk.el (org-babel-execute:awk): * ob-abc.el (org-babel-execute:abc): * ob-R.el (org-babel-expand-body:R): * ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...). 2013-11-12 Glenn Morris <rgm@gnu.org> * ox-html.el (org-html-scripts): Add 2013 to copyright years. (org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
(session body &optional result-type result-params)
"Pass BODY to the Python process in SESSION.
If RESULT-TYPE equals `output' then return standard output as a
string. If RESULT-TYPE equals `value' then return the value of the
last statement in BODY, as elisp."
(let* ((tmp-src-file (org-babel-temp-file "python-"))
(results
(progn
(with-temp-file tmp-src-file (insert body))
(pcase result-type
(`output
(let ((body (format org-babel-python--exec-tmpfile
(org-babel-process-file-name
tmp-src-file 'noquote))))
(org-babel-python--send-string session body)))
(`value
(let* ((tmp-results-file (org-babel-temp-file "python-"))
(body (format org-babel-python--eval-ast
(org-babel-process-file-name
tmp-src-file 'noquote)
(org-babel-process-file-name
tmp-results-file 'noquote)
(if (member "pp" result-params)
"True" "False"))))
(org-babel-python--send-string session body)
(sleep-for 0 10)
(org-babel-eval-read-file tmp-results-file)))))))
(org-babel-result-cond result-params
results
(org-babel-python-table-or-string results))))
2009-06-12 23:23:28 +00: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 23:23:28 +00:00
string))
(provide 'ob-python)
;;; ob-python.el ends here