From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:403:478a::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id ODCLL36c3WSzSgAASxT56A (envelope-from ) for ; Thu, 17 Aug 2023 06:05:18 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:478a::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id iNSlL36c3WSmvAAA9RJhRA (envelope-from ) for ; Thu, 17 Aug 2023 06:05:18 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 23E7C65452 for ; Thu, 17 Aug 2023 06:05:17 +0200 (CEST) Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=HFJhZnpU; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1692245118; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=la/kRWnb2+HCG9wpdTIw+yfuiekRrwSLiLXMFC1FUrs=; b=ANf4+kyifolceusxilIFj4cSwBMDis3RSmi3u2V3PwYDf+fG2VFXtDFj117UrQpdbWGVbc v5+hEHq8Om6Z53QzOuiqt7sn5sOdM02IoozVIafe98Wydes2U2QFPc0cNWxeoVExAUJcZ8 RLO2QGD82qz49KNpbns2F4w3zDVP28l1XdtLFZfZbegm7CZS5dZdqu+gpIpnT1O0xz5WY5 Gk1iVet0dbWJ7rqx65SYCc+CRvwpGPsjTldYfasoNvbQtgtnI2jzqLJYdL6AFEjn83NWy5 RqDFol1EDf8NrzlDTJhEB/vdMG+j0c3N0PTNHTi31lF1lf6MigGFtpYsZ15XGg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=HFJhZnpU; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Seal: i=1; s=key1; d=yhetil.org; t=1692245118; a=rsa-sha256; cv=none; b=h0ErPPU4p1+2IGEDKMUy2K3x2OlfimlqIUMMFFoNW1XAskJN9tZsx/xcXF1cJBYz57lR9K wYlz7V7GrRrv5SZ3+TXHKgZCVUGVS5eR1pQYI1Td15KmudwAqLpAmN2RCEHdQ8DpeeiqSx wGUcYnJJwAG6K4uhu9f751g4mM/h92SrzkUaE/qD45HEzRPn7kwvBMkXRpxvjWG/v9pQ+l AL0Is3mXB6FmjcQDgCRH94mPfgI43++wLwm9YFc40yFOVPumH1GtJIrBt2kSnRlG1YeJKm mFqYPHEQn9lowE7s+44BitAaB1RQxbq9upLMFilZXx9ymeD7sTnY35xiYiGm8g== Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qWUFF-0000Tm-If; Thu, 17 Aug 2023 00:04:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qWUFD-0000TW-Ob for emacs-orgmode@gnu.org; Thu, 17 Aug 2023 00:04:24 -0400 Received: from mail-oi1-x231.google.com ([2607:f8b0:4864:20::231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qWUFB-0004sD-3R for emacs-orgmode@gnu.org; Thu, 17 Aug 2023 00:04:23 -0400 Received: by mail-oi1-x231.google.com with SMTP id 5614622812f47-3a7d402fc6fso5333017b6e.2 for ; Wed, 16 Aug 2023 21:04:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692245059; x=1692849859; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=la/kRWnb2+HCG9wpdTIw+yfuiekRrwSLiLXMFC1FUrs=; b=HFJhZnpUua8M/lNRkNK8COME08hvpbORihkd1oWqE26M6v/T15rVB+cS7n5zArwChG XD6Mf6ytxivyRf/QJbuyOgELsmslsuRcJ3GckUHsLETDDuQCcLVtIc3GQJGfyJh86JeN G9fUtht5KJc/kOXrrh+1TLYLcXnHJoCvwBHMYUqr96SqsjPzOHL/dht9o0RiNLQHwh1O 1RBXvtp+C8uzXskWemdaf5n/z0IVz/sTbUQhdrDfSCnWwyZ0c2S3WKmv4bzIj4xAAV5i Kq5K6xp49L0F9EsWOFvPGPvVzf9lPVv1eAAAeyL+XNNSMl8h7PxFwwzHoFIOpe6tgaQv 09Vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692245059; x=1692849859; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=la/kRWnb2+HCG9wpdTIw+yfuiekRrwSLiLXMFC1FUrs=; b=Uj3kF9QV4jHcoRkXegz72SRwrsd3LfIN5odS8g5+bIvkoBU0pGwZ0ZQuoC24oXR/Aq 0VyB2p+Uqh7yOvw1LRFrv97iHBbFbUBmVpNrwLcfrdiY+oeBf49yLZg45jjU65k5ykVU bIbD22bNX90mIKzzJCDs0e4jd+u8BwGOf8Z2dYblX696k4nLh8IozzuoNbYiho9V3k5t dE6tNrGFzUbEe8lKoefX40aaYDsqqtA74j2RThuKiKHlzQUo/S6CLK8yy1yTAZSWx6Mn JURxRmZh9kq40LHJ9+DPUwtjE0LIVCunyjaytsjrJDEYWY7wvd9Xnj7ybWMUju/zsC43 cjFQ== X-Gm-Message-State: AOJu0YxuYEUDwO04rT7X+wwrK/Du8XWgAOMr6KtJP3wfBOwkltvhxmEU 98X3t8sDlrjS0JwTuVE6TF5TsCX63SU= X-Google-Smtp-Source: AGHT+IFmnBl4qYIMPgdc3FDNhZ1VDF+EA94hZA/EBXVTAdMmoEMgclPhl+P/RnBnVKqkkBbUdGI52g== X-Received: by 2002:a54:4505:0:b0:3a7:56aa:f1a4 with SMTP id l5-20020a544505000000b003a756aaf1a4mr4017078oil.36.1692245059012; Wed, 16 Aug 2023 21:04:19 -0700 (PDT) Received: from localhost (157-131-78-143.fiber.dynamic.sonic.net. [157.131.78.143]) by smtp.gmail.com with ESMTPSA id q4-20020a638c44000000b00563590be25esm12702580pgn.29.2023.08.16.21.04.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Aug 2023 21:04:18 -0700 (PDT) From: Jack Kamm To: Ihor Radchenko Cc: emacs-orgmode@gnu.org, Liu Hui Subject: Re: [PATCH] ob-python results handling for dicts, dataframes, arrays, and plots In-Reply-To: <871qg3pc2o.fsf@localhost> References: <87a5ur6f7w.fsf@gmail.com> <871qg3pc2o.fsf@localhost> Date: Wed, 16 Aug 2023 21:04:16 -0700 Message-ID: <87350i9uwv.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::231; envelope-from=jackkamm@gmail.com; helo=mail-oi1-x231.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Spam-Score: -9.56 X-Migadu-Queue-Id: 23E7C65452 X-Migadu-Scanner: mx0.migadu.com X-Migadu-Spam-Score: -9.56 X-TUID: Y8ncyIbNQ1Yh --=-=-= Content-Type: text/plain Ihor Radchenko 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 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0002-ob-python-Convert-dicts-to-tables.patch >From c24d2eeb3b8613df9b9c23583a4b26a6c0934931 Mon Sep 17 00:00:00 2001 From: Jack Kamm 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 --=-=-=--