emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* ob-python.el: questions about output
@ 2020-02-23 16:43 R C
  2020-02-24  4:52 ` Jack Kamm
  0 siblings, 1 reply; 10+ messages in thread
From: R C @ 2020-02-23 16:43 UTC (permalink / raw)
  To: emacs-orgmode

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

The approach proposed for graphical output at
https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html is

````
#+begin_src python :results file

import matplotlib, numpy
matplotlib.use('Agg')
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(4,2))
x=numpy.linspace(-15,15)
plt.plot(numpy.sin(x)/x)
fig.tight_layout()
plt.savefig('images/python-matplot-fig.png')
return 'images/python-matplot-fig.png' # return filename to org-mode
#+end_src
````

and to use :results output for displaying stdout.

1. Is it possible to have both types of output displayed from a single src
block to avoid having to duplicate the src block, using :exports results
for one and :exports both for the other?
2. Is there an option to suppress export of the the last line of the src
block which is specific to the ob-python implementation.

Thanks,
RC

[-- Attachment #2: Type: text/html, Size: 1312 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ob-python.el: questions about output
  2020-02-23 16:43 ob-python.el: questions about output R C
@ 2020-02-24  4:52 ` Jack Kamm
  2020-02-24  4:53   ` Jack Kamm
  2020-02-24  7:13   ` R C
  0 siblings, 2 replies; 10+ messages in thread
From: Jack Kamm @ 2020-02-24  4:52 UTC (permalink / raw)
  To: R C, emacs-orgmode

Hi RC,

R C <recifx@gmail.com> writes:

> 1. Is it possible to have both types of output displayed from a single src
> block to avoid having to duplicate the src block, using :exports results
> for one and :exports both for the other?

No, there's no option to output both ":results output" and ":results value".

If you want both output and value results, and want to avoid executing
the same code twice, you could consider using a pair of ":session"
blocks, for example: 

#+begin_src python :session session1 :results output
print("Block that does some computations")
x = 1+1
#+end_src

#+begin_src python :session session1 :results output
x
#+end_src

> 2. Is there an option to suppress export of the the last line of the src
> block which is specific to the ob-python implementation.

I'm not sure what you mean by this, could you give an example of what
the observed and desired behaviors are?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ob-python.el: questions about output
  2020-02-24  4:52 ` Jack Kamm
@ 2020-02-24  4:53   ` Jack Kamm
  2020-02-24  7:13   ` R C
  1 sibling, 0 replies; 10+ messages in thread
From: Jack Kamm @ 2020-02-24  4:53 UTC (permalink / raw)
  To: R C, emacs-orgmode

Sorry, my email had a typo:

> #+begin_src python :session session1 :results output
> x
> #+end_src

That should be ":results value".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ob-python.el: questions about output
  2020-02-24  4:52 ` Jack Kamm
  2020-02-24  4:53   ` Jack Kamm
@ 2020-02-24  7:13   ` R C
  2020-02-24 13:42     ` Fraga, Eric
  2020-02-24 16:17     ` Jack Kamm
  1 sibling, 2 replies; 10+ messages in thread
From: R C @ 2020-02-24  7:13 UTC (permalink / raw)
  To: Jack Kamm; +Cc: emacs-orgmode

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

Hi Jack,
Thanks for your response. Sorry for not being clearer.

My python src blocks often have both graphical output as well as the
results of some numerical calculation displayed using print statements.
#+begin_src python :results file
import numpy as np
import matplotlib.pyplot as plt
def f(x): return np.polyval(a, x)
a = [1, -4, 4.5, -1.5]
x = np.roots(a)
print(f'Roots of the polynomial are {x}')
x_ = np.linspace(np.min(x), np.max(x), 100)
plt.plot(x_, f(x_))
plt.plot(x, f(x), marker='o')
plt.savefig('img/ex1.png')
return 'img/ex1.png'
#+end_src

Executing this results in

#+RESULTS:
[[file:img/ex1.png]]

When exported I would like the line: return 'img/ex1.png' not to be
included in the listing of the src block.

Also, the output of the print statement is not displayed in this case. It
seems that the display of the results of the print statement: print(f'Roots
of the polynomial are {x}') , would require  execution of a duplicate src
block without the return statement  in the last line, and with ':results
output' replacing ':results file'  which gives:

#+RESULTS:
: Roots of the polynomial are [2.3660254 1.        0.6339746]

I would have liked to avoid the duplication if possible.

Thanks,
RC

On Sun, Feb 23, 2020 at 11:53 PM Jack Kamm <jackkamm@gmail.com> wrote:

> Hi RC,
>
> R C <recifx@gmail.com> writes:
>
> > 1. Is it possible to have both types of output displayed from a single
> src
> > block to avoid having to duplicate the src block, using :exports results
> > for one and :exports both for the other?
>
> No, there's no option to output both ":results output" and ":results
> value".
>
> If you want both output and value results, and want to avoid executing
> the same code twice, you could consider using a pair of ":session"
> blocks, for example:
>
> #+begin_src python :session session1 :results output
> print("Block that does some computations")
> x = 1+1
> #+end_src
>
> #+begin_src python :session session1 :results output
> x
> #+end_src
>
> > 2. Is there an option to suppress export of the the last line of the src
> > block which is specific to the ob-python implementation.
>
> I'm not sure what you mean by this, could you give an example of what
> the observed and desired behaviors are?
>

[-- Attachment #2: Type: text/html, Size: 3125 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ob-python.el: questions about output
  2020-02-24  7:13   ` R C
@ 2020-02-24 13:42     ` Fraga, Eric
  2020-02-24 16:18       ` Jack Kamm
  2020-02-24 16:17     ` Jack Kamm
  1 sibling, 1 reply; 10+ messages in thread
From: Fraga, Eric @ 2020-02-24 13:42 UTC (permalink / raw)
  To: R C; +Cc: emacs-orgmode

On Monday, 24 Feb 2020 at 02:13, R C wrote:
> When exported I would like the line: return 'img/ex1.png' not to be
> included in the listing of the src block.

I don't think this is currently possible in the sense you want.

You could use :noweb where you have one src block referring to others
and only output the code for the main body.  E.g. something along the
lines of

#+name: mainblock
#+begin_src python :results file :noweb yes
def f(x): return np.polyval(a, x)
a = [1, -4, 4.5, -1.5]
x = np.roots(a)
print(f'Roots of the polynomial are {x}')
x_ = np.linspace(np.min(x), np.max(x), 100)
#+end_src

#+name: full
#+begin_src python :results file :noweb yes
import numpy as np
import matplotlib.pyplot as plt
<<mainblock>>
plt.plot(x_, f(x_))
plt.plot(x, f(x), marker='o')
plt.savefig('img/ex1.png')
return 'img/ex1.png'
#+end_src

Longer term, better would be an implementation of :prologue and
:epilogue options for python src blocks like we have for Maxima.  It
would probably make sense to provide these options for most if not all
src blocks?

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.3.6-354-g9d5880

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ob-python.el: questions about output
  2020-02-24  7:13   ` R C
  2020-02-24 13:42     ` Fraga, Eric
@ 2020-02-24 16:17     ` Jack Kamm
  2020-02-24 16:40       ` Fraga, Eric
  2020-02-27  4:29       ` R C
  1 sibling, 2 replies; 10+ messages in thread
From: Jack Kamm @ 2020-02-24 16:17 UTC (permalink / raw)
  To: R C; +Cc: emacs-orgmode

Hi RC,

R C <recifx@gmail.com> writes:

> My python src blocks often have both graphical output as well as the
> results of some numerical calculation displayed using print statements.
>
> When exported I would like the line: return 'img/ex1.png' not to be
> included in the listing of the src block.
>
> Also, the output of the print statement is not displayed in this case.

The external ob-jupyter [0] accomplishes what you want:

1. It can return multiple types of output, e.g. print statements and plots.
2. It doesn't require the "return" statement.

It's main limitations are:
1. It requires jupyter.
2. It requires using session blocks.

I'd recommend checking it out. It has excellent Python support.

If you prefer a native solution, I think the options are:

1. noweb (as Eric mentioned). Then you only need to write the code
once, but note that it will be executed twice.

2. Using multiple session blocks to return output and value
separately. Since they use the same state, you would only need to
execute the code once.

3. Possibly, you could insert the link to the image separately, create a
named reference to it, and pass it to the Python block via ":var", which
would create at an image at that link. Then use ":results output" to
capture the print statements. I'm not 100% sure if this will work.

[0] https://github.com/dzop/emacs-jupyter

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ob-python.el: questions about output
  2020-02-24 13:42     ` Fraga, Eric
@ 2020-02-24 16:18       ` Jack Kamm
  2020-02-24 16:49         ` Fraga, Eric
  0 siblings, 1 reply; 10+ messages in thread
From: Jack Kamm @ 2020-02-24 16:18 UTC (permalink / raw)
  To: Fraga, Eric, R C; +Cc: emacs-orgmode

Hi Eric,

"Fraga, Eric" <e.fraga@ucl.ac.uk> writes:

> Longer term, better would be an implementation of :prologue and
> :epilogue options for python src blocks like we have for Maxima.  It
> would probably make sense to provide these options for most if not all
> src blocks?

This sounds interesting. Do you know of any documentation or examples
for :prologue and :epilogue? I checked the Worg page for ob-maxima, but
it didn't mention these header arguments.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ob-python.el: questions about output
  2020-02-24 16:17     ` Jack Kamm
@ 2020-02-24 16:40       ` Fraga, Eric
  2020-02-27  4:29       ` R C
  1 sibling, 0 replies; 10+ messages in thread
From: Fraga, Eric @ 2020-02-24 16:40 UTC (permalink / raw)
  To: Jack Kamm; +Cc: R C, emacs-orgmode

On Monday, 24 Feb 2020 at 08:17, Jack Kamm wrote:
> 1. noweb (as Eric mentioned). Then you only need to write the code
> once, but note that it will be executed twice.

Doesn't need to execute twice: maybe add ":eval no" to the first block,
the one that will be included in the other one?

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.3.6-354-g9d5880

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ob-python.el: questions about output
  2020-02-24 16:18       ` Jack Kamm
@ 2020-02-24 16:49         ` Fraga, Eric
  0 siblings, 0 replies; 10+ messages in thread
From: Fraga, Eric @ 2020-02-24 16:49 UTC (permalink / raw)
  To: Jack Kamm; +Cc: R C, emacs-orgmode

On Monday, 24 Feb 2020 at 08:18, Jack Kamm wrote:
> This sounds interesting. Do you know of any documentation or examples
> for :prologue and :epilogue? I checked the Worg page for ob-maxima, but
> it didn't mention these header arguments.

I don't know of any documentation.  They basically simply provide
strings that are included before and after the text within the src block
before evaluation but are not exported in a code listing.

I can give you an example of how I use these with maxima:

#+header: :prologue "fpprintprec: 2; linel: 50;"
#+header: :epilogue "print(solution);"
#+begin_src maxima :exports both :results output :cache yes
  solution: exp(1.0);
#+end_src

which sets the printing precision for numbers to 2 and the line length
to 50 and prints out the contents of the solution variable at the
end.  These are details that are not important for display; I use these
settings all the time for my lecture slides.

The above example gives the following when exported to ascii:

,----
| solution: exp(1.0);
`----

,----
| 2.7 
`----

so the slides can concentrate on the material that is important.

Adding such to ob-python etc. should not be difficult.

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.3.6-354-g9d5880

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ob-python.el: questions about output
  2020-02-24 16:17     ` Jack Kamm
  2020-02-24 16:40       ` Fraga, Eric
@ 2020-02-27  4:29       ` R C
  1 sibling, 0 replies; 10+ messages in thread
From: R C @ 2020-02-27  4:29 UTC (permalink / raw)
  To: Jack Kamm; +Cc: emacs-orgmode

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

Hi Jack,
Thanks for your suggestion to use ob-jupyter. That gives me the stdout and
graphical output in separate results blocks as I wanted, and no return
statement is needed. I don't know if there are any side-effects to using
multiple sessions in a single org file, but so far it seems to work quite
well.

On Mon, Feb 24, 2020 at 11:18 AM Jack Kamm <jackkamm@gmail.com> wrote:

> Hi RC,
>
> R C <recifx@gmail.com> writes:
>
> > My python src blocks often have both graphical output as well as the
> > results of some numerical calculation displayed using print statements.
> >
> > When exported I would like the line: return 'img/ex1.png' not to be
> > included in the listing of the src block.
> >
> > Also, the output of the print statement is not displayed in this case.
>
> The external ob-jupyter [0] accomplishes what you want:
>
> 1. It can return multiple types of output, e.g. print statements and plots.
> 2. It doesn't require the "return" statement.
>
> It's main limitations are:
> 1. It requires jupyter.
> 2. It requires using session blocks.
>
> I'd recommend checking it out. It has excellent Python support.
>
> If you prefer a native solution, I think the options are:
>
> 1. noweb (as Eric mentioned). Then you only need to write the code
> once, but note that it will be executed twice.
>
> 2. Using multiple session blocks to return output and value
> separately. Since they use the same state, you would only need to
> execute the code once.
>
> 3. Possibly, you could insert the link to the image separately, create a
> named reference to it, and pass it to the Python block via ":var", which
> would create at an image at that link. Then use ":results output" to
> capture the print statements. I'm not 100% sure if this will work.
>
> [0] https://github.com/dzop/emacs-jupyter
>


-- 
Regards,
RC

[-- Attachment #2: Type: text/html, Size: 2572 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2020-02-27  9:29 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-23 16:43 ob-python.el: questions about output R C
2020-02-24  4:52 ` Jack Kamm
2020-02-24  4:53   ` Jack Kamm
2020-02-24  7:13   ` R C
2020-02-24 13:42     ` Fraga, Eric
2020-02-24 16:18       ` Jack Kamm
2020-02-24 16:49         ` Fraga, Eric
2020-02-24 16:17     ` Jack Kamm
2020-02-24 16:40       ` Fraga, Eric
2020-02-27  4:29       ` R C

Code repositories for project(s) associated with this 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).