Replace pprint with black to "pp" Python session source blocks
This commit is contained in:
parent
95f18b0a24
commit
4439109c17
57
README.org
57
README.org
@ -1635,11 +1635,13 @@ buffers with help of an external [[https://nodejs.org][node.js]] program.
|
||||
The code in listing [[lst:customize-org-babel]], [[lst:customize-org]],
|
||||
[[lst:customize-org-link]], [[lst:customize-org-export]], and
|
||||
[[lst:customize-org-latex-export]] does basic customization of [[https://orgmode.org/][Org-mode]] variables.
|
||||
Listing [[lst:customize-org-link]] configures =org-link= to use relative file path
|
||||
links. Listing [[lst:customize-org-latex-classes]] defines [[info:org#LaTeX specific export settings][org-latex-classes (info)]]
|
||||
for backward compatibility. See table [[tab:org-latex-class-tag-placeholder]] and
|
||||
type {{{kbd(C-h v org-latex-classes)}}} for an explanation of the code in
|
||||
listing [[lst:customize-org-latex-classes]].
|
||||
Listing [[lst:customize-ob-python]] allows to pretty-print Python session source
|
||||
block values with [[https://github.com/psf/black#readme][black]] instead of [[https://docs.python.org/3/library/pprint.html][pprint]]. Listing [[lst:customize-org-link]]
|
||||
configures =org-link= to use relative file path links. Listing
|
||||
[[lst:customize-org-latex-classes]] defines [[info:org#LaTeX specific export settings][org-latex-classes (info)]] for backward
|
||||
compatibility. See table [[tab:org-latex-class-tag-placeholder]] and type {{{kbd(C-h
|
||||
v org-latex-classes)}}} for an explanation of the code in listing
|
||||
[[lst:customize-org-latex-classes]].
|
||||
|
||||
#+attr_latex: :booktabs yes :float table
|
||||
#+caption[The relation tag-placeholder in listing [[lst:customize-org-latex-classes]]]:
|
||||
@ -1678,10 +1680,53 @@ listing [[lst:customize-org-latex-classes]].
|
||||
(with-eval-after-load 'sly
|
||||
(custom-set-variables
|
||||
'(org-babel-lisp-eval-fn #'sly-eval))))
|
||||
#+end_src
|
||||
|
||||
#+caption[Customize =ob-python=]:
|
||||
#+caption: Customize =ob-python=.
|
||||
#+name: lst:customize-ob-python
|
||||
#+begin_src emacs-lisp
|
||||
(with-eval-after-load 'ob-python
|
||||
(custom-set-variables
|
||||
'(org-babel-python-command "python -E")))
|
||||
'(org-babel-python-command "python -E"))
|
||||
|
||||
(defun org-babel-python-format-session-value-override
|
||||
(src-file result-file result-params)
|
||||
"Return Python code to evaluate SRC-FILE and write result to RESULT-FILE.
|
||||
Use `black' instead of `pprint' when \"pp\" is a member of RESULT-PARAMS."
|
||||
(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')
|
||||
)
|
||||
with open('%s', 'w') as __org_babel_python_tmpfile:
|
||||
if %s:
|
||||
import black
|
||||
__org_babel_python_tmpfile.write(
|
||||
black.format_str(repr(__org_babel_python_final), mode=black.Mode())
|
||||
)
|
||||
else:
|
||||
__org_babel_python_tmpfile.write(str(__org_babel_python_final))
|
||||
else:
|
||||
exec(compile(__org_babel_python_ast, '<string>', 'exec'))
|
||||
__org_babel_python_final = None"
|
||||
(org-babel-process-file-name src-file 'noquote)
|
||||
(org-babel-process-file-name result-file 'noquote)
|
||||
(if (member "pp" result-params) "True" "False")))
|
||||
|
||||
(defun toggle-org-babel-python-format-session-value-override ()
|
||||
"Toggle `org-babel-python-format-session-value' advice."
|
||||
(interactive)
|
||||
(toggle-advice 'org-babel-python-format-session-value
|
||||
:override #'org-babel-python-format-session-value-override))
|
||||
|
||||
(toggle-org-babel-python-format-session-value-override))
|
||||
#+end_src
|
||||
|
||||
#+caption[Customize =Org=]:
|
||||
|
Loading…
Reference in New Issue
Block a user