ob-python: Replace session value format string with function
* lisp/ob-python.el (org-babel-python--eval-ast): Removed. (org-babel-python-format-session-value): New function that returns Python code to evaluate for sessions with value results. (org-babel-python-evaluate-session): Replace org-babel-python--eval-ast with org-babel-python-format-session-value. Motivation is that the new function is more flexible than the old format string. We can pass in the full result-params, which can be used to add more formatting options in future. We could also add more optional arguments in the future to extend this functionality. This function will be particularly helpful for a couple patches after 9.4: - ob-reticulate - https://orgmode.org/list/875z98gj4f.fsf@gmail.com/ - Improved formatting - https://orgmode.org/list/87eenpfe77.fsf@gmail.com/ However, I add this function in now 9.4, so those future patches can rely on it in 9.4. In particular, ob-reticulate will probably be packaged separately from org-mode, and I want to provide a stable interface to it so that it can be released after 9.4.
This commit is contained in:
parent
dfad6488d3
commit
6f9929fc3b
|
@ -245,7 +245,10 @@ with open('%s') as f:
|
|||
Has a single %s escape, the tempfile containing the source code
|
||||
to evaluate.")
|
||||
|
||||
(defconst org-babel-python--eval-ast "\
|
||||
(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."
|
||||
(format "\
|
||||
import ast
|
||||
with open('%s') as f:
|
||||
__org_babel_python_ast = ast.parse(f.read())
|
||||
|
@ -264,12 +267,9 @@ if isinstance(__org_babel_python_final, ast.Expr):
|
|||
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\".")
|
||||
(org-babel-process-file-name src-file 'noquote)
|
||||
(org-babel-process-file-name result-file 'noquote)
|
||||
(if (member "pp" result-params) "True" "False")))
|
||||
|
||||
(defun org-babel-python-evaluate
|
||||
(session body &optional result-type result-params preamble)
|
||||
|
@ -359,13 +359,8 @@ last statement in BODY, as elisp."
|
|||
(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"))))
|
||||
(body (org-babel-python-format-session-value
|
||||
tmp-src-file tmp-results-file result-params)))
|
||||
(org-babel-python--send-string session body)
|
||||
(sleep-for 0 10)
|
||||
(org-babel-eval-read-file tmp-results-file)))))))
|
||||
|
|
Loading…
Reference in New Issue