Richard H Stanton <rhstan...@berkeley.edu> writes:

> what are the recommended headers for a Python code block that exports a 
> table? For example, ":results output raw” and ":results output drawer” both 
> seem to work (without :wrap), while “:results output” puts everything in an 
> example block, which then seems to get exported verbatim, not as a LaTeX 
> table.

Take a look at
https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html. I found
this link in the Org manual, "(org) Results of Evaluation", and then to
"Documentation" link of python. From that page:

    :results {output, value}: Output results come from whatever the python code
    prints on stdout. Value results are the value of the last expression
    evaluated in the code block. Value mode is the default (as with other
    languages). In value mode you can use the following subtypes:

        verbatim: value is returned as string. In particular, use this to
        prevent conversion of lists and tuples to tables.

        table: (Org 9.7+) Try to convert the result to an Org table. Dicts,
        numpy arrays, and pandas DataFrames/Series can be returned as tables
        this way (by default, they are printed verbatim). Note that lists and
        tuples are already converted to table by default (use verbatim to
        prevent that).

So, ':results output' will output whatever python prints to stdout. With
'print()', I think that this will just be a string, and no conversion will take
place. If you specify 'output raw', python will still just print the string, but
*Org mode* will now interpret it as raw Org mode. This will probably work fine,
but you'll have to add '|' and newlines in your string correctly. Alternative:

':results value', the default. In this case ob-python will convert lists and
tuples to tables by default (and optionally, dicts and DataFrames as well).

That would be my recommendation: a code block with the default headers, which
returns a list of lists. I've attached an org file which does this.

> I actually generate this table from a Python code block. In the past, I’ve 
> often run into problems with raw output (like this), where running the code 
> block multiple times causes the output to appear multiple times, rather than 
> overwriting.

':results append' will cause the output to appear multiple times, but I expect
that it happened due to other circumstances (':results replace' is default and I
expect you didn't change that). One such circumstance is when you, say, add a
':results output' header argument, and then change it back. For reasons unknown
to me, Org mode will then see the previous #+RESULTS: block as unrelated to the
current one, and will not replace it.

This is then kind of annoying, because you'll have to add the #+ATTR_LATEX: line
to the table again. I'd fix this manually, but if it gets annoying you can use
the suggestion by PA.

#+begin_src python
  import random
  from collections import namedtuple

  Tier = namedtuple('Tier', ['number', 'total'])

  def generate_table(tiers):
      total = sum(tier.total for tier in tiers)
      total_numbers = sum(tier.number for tier in tiers)

      # A None in a list will result in a hline in org tables
      return [
          None,  # Header separator
          ('Tier', 'Number', 'Total', '% of Total'),
          None,  # Column header separator
          ,*[
              (i+1, tier.number, f'{tier.total:,.2f}', f'{100 * tier.total / total:.2f}')
              for i, tier in enumerate(tiers)
          ],
          None,  # Footer separator
          ('Total', total_numbers, f'{total:,.2f}', '100.00'),
          None,  # Final separator
      ]

  tiers = [
      Tier(4,  random.uniform(1000, 10000)),
      Tier(19, random.uniform(1000, 10000)),
      Tier(24, random.uniform(1000, 10000)),
  ]
  return generate_table(tiers)
#+end_src

#+RESULTS:
|-------+--------+-----------+------------|
|  Tier | Number | Total     | % of Total |
|-------+--------+-----------+------------|
|     1 |      4 | 8,022.65  |      65.45 |
|     2 |     19 | 1,549.71  |      12.64 |
|     3 |     24 | 2,685.28  |      21.91 |
|-------+--------+-----------+------------|
| Total |     47 | 12,257.65 |     100.00 |
|-------+--------+-----------+------------|

#+ATTR_LATEX: :align rrrr :float t :placement [htbp]
#+RESULTS:

Reply via email to