emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: gerard.vermeulen@posteo.net
To: Jack Kamm <jackkamm@gmail.com>
Cc: Ihor Radchenko <yantar92@posteo.net>,
	emacs-orgmode@gnu.org, Liu Hui <liuhui1610@gmail.com>,
	emacs-orgmode-bounces+gerard.vermeulen=posteo.net@gnu.org
Subject: Re: [PATCH] ob-python results handling for dicts, dataframes, arrays, and plots
Date: Thu, 17 Aug 2023 09:14:02 +0000	[thread overview]
Message-ID: <9948eae293cae8766808778d6390eb64@posteo.net> (raw)
In-Reply-To: <87350i9uwv.fsf@gmail.com>



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



  reply	other threads:[~2023-08-17  9:15 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-15 23:46 [PATCH] ob-python results handling for dicts, dataframes, arrays, and plots Jack Kamm
2023-08-16  9:32 ` Ihor Radchenko
2023-08-17  4:04   ` Jack Kamm
2023-08-17  9:14     ` gerard.vermeulen [this message]
2023-08-17 12:10       ` Ihor Radchenko
2023-08-18  4:37         ` gerard.vermeulen
2023-08-18  6:01           ` gerard.vermeulen
2023-08-18 23:30       ` Jack Kamm
2023-08-19  8:50         ` Ihor Radchenko
2023-08-20 18:01           ` Jack Kamm
2023-08-20 18:21             ` Ihor Radchenko
2023-08-19  8:58         ` Ihor Radchenko
2023-08-20 18:13           ` Jack Kamm
2023-08-20 18:25             ` Ihor Radchenko
2023-08-22 23:37               ` Jack Kamm
2023-08-17 12:07     ` Ihor Radchenko
2023-08-18 22:49       ` Jack Kamm
2023-08-17  5:35 ` Liu Hui
2023-08-18 23:09   ` Jack Kamm
2023-08-20 12:13     ` Liu Hui
2023-08-20 18:31       ` Jack Kamm
2023-08-21  6:21         ` Liu Hui
2023-08-22 23:44         ` Jack Kamm
2023-08-17 11:57 ` Ihor Radchenko
2023-08-18 23:18   ` Jack Kamm
2023-08-19  8:54     ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9948eae293cae8766808778d6390eb64@posteo.net \
    --to=gerard.vermeulen@posteo.net \
    --cc=emacs-orgmode-bounces+gerard.vermeulen=posteo.net@gnu.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=jackkamm@gmail.com \
    --cc=liuhui1610@gmail.com \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).