On 17.08.2023 06:04, Jack Kamm wrote:
I attach a 2nd patch implementing this. It also makes ":results table"
the default return type for dict. (Use ":results verbatim" to get the
dict as a string instead).
I am also putting a branch with these changes here:
https://github.com/jackkamm/org-mode/tree/python-results-revisited-2023
Happy to see that ob-python gets so much love!
Your patches allow anyone to change org-babel-python--def-format-value.
For instance, I want to use black to "pretty-print" certain tree-like
structures
and I have now in my init.el:
(with-eval-after-load 'ob-python
(setq 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 black
f.write(black.format_str(repr(result), mode=black.Mode()))
else:
if not set(result_params).intersection(\
['scalar', 'verbatim', 'raw']):
try:
import pandas
except ImportError:
pass
else:
if isinstance(result, pandas.DataFrame):
result = [[''] + list(result.columns), None] + \
[[i] + list(row) for i, row in result.iterrows()]
elif isinstance(result, pandas.Series):
result = list(result.items())
try:
import numpy
except ImportError:
pass
else:
if isinstance(result, numpy.ndarray):
result = result.tolist()
f.write(str(result))"))
Without your patches I use advice to override
org-babel-python-format-session-value, which is worse IMO.
This also allows anyone to format for instance AstroPy tables
(https://docs.astropy.org/en/stable/table/).
I do not know how much this "abuse" of defconst is frowned
upon (elisp manual says defconst is advisory), but maybe it
can be advertised as a feature.
Best regards -- Gerard