emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Jack Kamm <jackkamm@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: emacs-orgmode@gnu.org, Liu Hui <liuhui1610@gmail.com>
Subject: Re: [PATCH] ob-python results handling for dicts, dataframes, arrays, and plots
Date: Wed, 16 Aug 2023 21:04:16 -0700	[thread overview]
Message-ID: <87350i9uwv.fsf@gmail.com> (raw)
In-Reply-To: <871qg3pc2o.fsf@localhost>

[-- Attachment #1: Type: text/plain, Size: 961 bytes --]

Ihor Radchenko <yantar92@posteo.net> writes:

> What about 
>
>  #+begin_src python :results table
>    return {"a": 1, "b": 2}
>  #+end_src
>
>  #+RESULTS:
>  | a | 1 |
>  | b | 2 |

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

>
> or 
>
>  #+begin_src python :results list
>    return {"a": 1, "b": 2}
>  #+end_src
>
>  #+RESULTS:
>  - a :: 1
>  - b :: 2

This seems harder, and may require more widespread changes beyond
ob-python. In particular, I think we'd need to change
`org-babel-insert-result' so that it can call `org-list-to-org' with a
list of type "descriptive" instead of "unordered" here:

https://git.sr.ht/~bzg/org-mode/tree/cc435cba71a99ee7b12676be3b6e1211a9cb7285/item/lisp/ob-core.el#L2535


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0002-ob-python-Convert-dicts-to-tables.patch --]
[-- Type: text/x-patch, Size: 2598 bytes --]

From c24d2eeb3b8613df9b9c23583a4b26a6c0934931 Mon Sep 17 00:00:00 2001
From: Jack Kamm <jackkamm@gmail.com>
Date: Wed, 16 Aug 2023 20:27:10 -0700
Subject: [PATCH 2/2] ob-python: Convert dicts to tables

This commit to be squashed with its parent before applying
---
 etc/ORG-NEWS      |  8 +++-----
 lisp/ob-python.el | 12 +++++++++---
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 2630554ae..509011737 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -578,11 +578,9 @@ tested property is actually present.
 
 *** =ob-python.el=: Support for more result types and plotting
 
-=ob-python= now recognizes numpy arrays, and pandas dataframes/series,
-and will convert them to org-mode tables when appropriate.
-
-In addition, dict results are now returned in appropriate string form,
-instead of being mangled as they were previously.
+=ob-python= now recognizes dictionaries, numpy arrays, and pandas
+dataframes/series, and will convert them to org-mode tables when
+appropriate.
 
 When the header argument =:results graphics= is set, =ob-python= will
 use matplotlib to save graphics. The behavior depends on whether value
diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 35a82afc0..3d987da2f 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -144,9 +144,7 @@ (defun org-babel-python-table-or-string (results)
   "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."
-  (let ((res (if (string-equal "{" (substring results 0 1))
-                 results ;don't covert dicts to elisp
-               (org-babel-script-escape results))))
+  (let ((res (org-babel-script-escape results)))
     (if (listp res)
         (mapcar (lambda (el) (if (eq el 'None)
                                  org-babel-python-None-to el))
@@ -242,6 +240,14 @@ (defconst org-babel-python--def-format-value "\
         else:
             if not set(result_params).intersection(\
 ['scalar', 'verbatim', 'raw']):
+                def dict2table(res):
+                    if isinstance(res, dict):
+                        return [(k, dict2table(v)) for k, v in res.items()]
+                    elif isinstance(res, list) or isinstance(res, tuple):
+                        return [dict2table(x) for x in res]
+                    else:
+                        return res
+                result = dict2table(result)
                 try:
                     import pandas
                 except ImportError:
-- 
2.41.0


  reply	other threads:[~2023-08-17  4:05 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 [this message]
2023-08-17  9:14     ` gerard.vermeulen
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=87350i9uwv.fsf@gmail.com \
    --to=jackkamm@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --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).