I saw a python code block example to output an array as an org-table. An example would be,
#+BEGIN_SRC python :results value
r = 3389.5 # km average radius
re = 3396.2 # equatorial radius in km
rp = 3376.2 # polar radius in km
m = 6.4171 * 10**(23) # kg
return[["Parameter", "Value"],
None,
["Mass Mars (kg)", "{:.5g}".format(m)],
["Mean Radius (km)", "{:.5g}".format(r)],
["Polar Radius (km)", "{:.5g}".format(rp)],
["Equatorial Radius (km)", "{:.5g}".format(re)]]
#+END_SRC
#+RESULTS:
| Parameter | Value |
|------------------------+------------|
| Mass Mars (kg) | 6.4171e+23 |
| Mean Radius (km) | 3389.5 |
| Polar Radius (km) | 3376.2 |
| Equatorial Radius (km) | 3396.2 |
This is almost perfect for me. I would like to be able to use ipython in session mode in order to pull results from various other code blocks without naming them individually with :var headers.
However, with ipython I get the error:
SyntaxError: 'return' outside function
I'm not sure why there is this difference in behavior between python and ipython code blocks. If I remove "return" from the ipython code block, I get the following output (no table):
#+BEGIN_SRC ipython :session mars :results value
[["Parameter", "Value"],
["Mass Mars (kg)", "{:.5g}".format(m)],
["Mean Radius (km)", "{:.5g}".format(r)],
["Polar Radius (km)", "{:.5g}".format(rp)],
["Equatorial Radius (km)", "{:.5g}".format(re)]]
#+END_SRC
#+RESULTS:
:RESULTS:
# Out[109]:
# text/plain
: [['Parameter', 'Value'],
: ['Mass Mars (kg)', '6.4171e+23'],
: ['Mean Radius (km)', '3389.5'],
: ['Polar Radius (km)', '3376.2'],
: ['Equatorial Radius (km)', '3396.2']]
:END:
Using the header argument ":results value table" doesn't work either:
#+BEGIN_SRC ipython :session mars :results value table
[["Parameter", "Value"],
["Mass Mars (kg)", "{:.5g}".format(m)],
["Mean Radius (km)", "{:.5g}".format(r)],
["Polar Radius (km)", "{:.5g}".format(rp)],
["Equatorial Radius (km)", "{:.5g}".format(re)]]
#+END_SRC
#+RESULTS:
:RESULTS:
| # Out[1]: |
:END:
In this case, is the best option just to do something like following? Am I missing a better solution? My end use scenario is exporting the org-file to pdf with a mixture of latex formulas as well as ipython code blocks and text.
#+BEGIN_SRC ipython :session mars
print("|-")
print("| Parameter | Value |")
print("|-")
print(f"|Mass Mars (kg) | {m}|")
print(f"|Mean Radius (km) | {r}|")
print(f"|Polar Radius (km) | {rp}|")
print(f"|Equatorial Radius (km) | {re}|")
print(f"|-")
print("#+TBLFM: $2=$0;%0.5g")
#+END_SRC
#+RESULTS:
:RESULTS:
# Out[116]:
# output
|------------------------+------------|
| Parameter | Value |
|------------------------+------------|
| Mass Mars (kg) | 6.4171e+23 |
| Mean Radius (km) | 3389.5 |
| Polar Radius (km) | 3376.2 |
| Equatorial Radius (km) | 3396.2 |
|------------------------+------------|
#+TBLFM: $2=$0;%0.5g