emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* python/babel inline images
@ 2012-06-05 10:30 henry atting
  2012-06-05 12:46 ` Brett Viren
  2012-06-05 14:57 ` Eric Schulte
  0 siblings, 2 replies; 22+ messages in thread
From: henry atting @ 2012-06-05 10:30 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

I do not succeed in generating an inline image as a result of a
python code block. The code itself works, C-c C-c generates the
according picture, but only in my home directory. The code block:

-*- org-babel-python-command: "python3" -*-
#+begin_src python
import csv
import matplotlib.pyplot as plot
x = []
y = []
csv_reader = csv.reader(open('csv_data.csv'))
for line in csv_reader:
	x.append(int(line[0]))
	y.append(float(line[1]))
plot.plot(x, y, label=r'exp', color='green')
plot.legend(loc='lower right')
plot.savefig("exp_csv.svg")
#+end_src

It tried different combinations of `:exports results', `:results
file', `:file filename'


-- 
http://literaturlatenight.de

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

* Re: python/babel inline images
  2012-06-05 10:30 python/babel inline images henry atting
@ 2012-06-05 12:46 ` Brett Viren
  2012-06-05 14:57 ` Eric Schulte
  1 sibling, 0 replies; 22+ messages in thread
From: Brett Viren @ 2012-06-05 12:46 UTC (permalink / raw)
  To: henry atting; +Cc: emacs-orgmode

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

henry atting <nsmp_06@online.de> writes:

> I do not succeed in generating an inline image as a result of a
> python code block. The code itself works, C-c C-c generates the
> according picture, but only in my home directory. The code block:

Could you not just follow the block with a hand-written link to the
output file?  Then, when it becomes available toggle "C-c C-x C-v" (once
or twice) or "M-x org-display-inline-images" once.

Maybe also rewrite the Python to place the output SVG in a directory
associated with your ORG file so that link can be relative.  I simply
use an env. var. to locate where my plots should output.

I'm an org-newbie so maybe there are better ways.

-Brett.

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

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

* Re: python/babel inline images
  2012-06-05 10:30 python/babel inline images henry atting
  2012-06-05 12:46 ` Brett Viren
@ 2012-06-05 14:57 ` Eric Schulte
  2012-06-05 22:34   ` William LÉCHELLE
  2012-06-06  0:59   ` Mikhail Titov
  1 sibling, 2 replies; 22+ messages in thread
From: Eric Schulte @ 2012-06-05 14:57 UTC (permalink / raw)
  To: henry atting; +Cc: emacs-orgmode

henry atting <nsmp_06@online.de> writes:

> Hi,
>
> I do not succeed in generating an inline image as a result of a
> python code block. The code itself works, C-c C-c generates the
> according picture, but only in my home directory. The code block:
>
> -*- org-babel-python-command: "python3" -*-
> #+begin_src python
> import csv
> import matplotlib.pyplot as plot
> x = []
> y = []
> csv_reader = csv.reader(open('csv_data.csv'))
> for line in csv_reader:
> 	x.append(int(line[0]))
> 	y.append(float(line[1]))
> plot.plot(x, y, label=r'exp', color='green')
> plot.legend(loc='lower right')
> plot.savefig("exp_csv.svg")
> #+end_src
>
> It tried different combinations of `:exports results', `:results
> file', `:file filename'

I'm not python expert, but the code block should be run in your current
directory, e.g., the following outputs the current working path expected
for me.

#+begin_src sh
  pwd
#+end_src

If you want to explicitly pass the current directory to your code block
as an argument, you could try something like the following

#+begin_src python :var mydir=(file-name-directory (buffer-file-name))
  return mydir
#+end_src

Hope this helps,

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

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

* Re: python/babel inline images
  2012-06-05 14:57 ` Eric Schulte
@ 2012-06-05 22:34   ` William LÉCHELLE
  2012-06-06 15:19     ` henry atting
  2012-06-06  0:59   ` Mikhail Titov
  1 sibling, 1 reply; 22+ messages in thread
From: William LÉCHELLE @ 2012-06-05 22:34 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode

At Tue, 05 Jun 2012 08:57:19 -0600,
Eric Schulte wrote:
> 
> henry atting <nsmp_06@online.de> writes:
> 
> > Hi,
> >
> > I do not succeed in generating an inline image as a result of a
> > python code block. The code itself works, C-c C-c generates the
> > according picture, but only in my home directory. The code block:
> >
> > -*- org-babel-python-command: "python3" -*-
> > #+begin_src python
> > import csv
> > import matplotlib.pyplot as plot
> > x = []
> > y = []
> > csv_reader = csv.reader(open('csv_data.csv'))
> > for line in csv_reader:
> > 	x.append(int(line[0]))
> > 	y.append(float(line[1]))
> > plot.plot(x, y, label=r'exp', color='green')
> > plot.legend(loc='lower right')
> > plot.savefig("exp_csv.svg")
> > #+end_src
> >
> > It tried different combinations of `:exports results', `:results
> > file', `:file filename'
> 
> I'm not python expert, but the code block should be run in your current
> directory, e.g., the following outputs the current working path expected
> for me.
> 
> #+begin_src sh
>   pwd
> #+end_src
> 
> If you want to explicitly pass the current directory to your code block
> as an argument, you could try something like the following
> 
> #+begin_src python :var mydir=(file-name-directory (buffer-file-name))
>   return mydir
> #+end_src

I guess if it's a paths' problem, the python equivalent would be os.getcwd(),
from the os module, and a solution to have the picture in the right place
could be os.chdir(path) (or maybe plot.savefig can take a full path as an
argument), but I think the OP (and I'm very interested too) wants org-babel to
manage the python output somehow to inline the image automatically.

I don't see that possible, because I don't think the "results" of plot.savefig
is the value of the image, but rather an i/o operation somehow (and I may well
be wrong) (and nothing goes to stdout). But maybe to output a link could do ?

#+begin_src python :results output file
  <<stuff>>
  path = "exp_csv.svg"
  plot.savefig(path)
  print path
#+end_src

HTH

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

* Re: python/babel inline images
  2012-06-05 14:57 ` Eric Schulte
  2012-06-05 22:34   ` William LÉCHELLE
@ 2012-06-06  0:59   ` Mikhail Titov
  2012-06-06  5:19     ` Eric Schulte
  1 sibling, 1 reply; 22+ messages in thread
From: Mikhail Titov @ 2012-06-06  0:59 UTC (permalink / raw)
  To: 'Eric Schulte', 'henry atting'; +Cc: emacs-orgmode

> -----Original Message-----
> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
> bounces+mlt=gmx.us@gnu.org] On Behalf Of Eric Schulte
> Sent: Tuesday, June 05, 2012 9:57 AM
> To: henry atting
> Cc: emacs-orgmode@gnu.org
> Subject: Re: [O] python/babel inline images
> 
> I'm not python expert, but the code block should be run in your current
> directory, e.g., the following outputs the current working path expected
> for me.
> 
> #+begin_src sh
>   pwd
> #+end_src
> 
> If you want to explicitly pass the current directory to your code block
> as an argument, you could try something like the following
> 
> #+begin_src python :var mydir=(file-name-directory (buffer-file-name))
>   return mydir
> #+end_src
> 

I've noticed some inconsistency between various languages in this aspect.
For instance, ob-R starts session in proper working directory, while all
looks like everything (?) else does not.

Should not it be somewhat standardized? I think it make sense to always cd
to org doc folder.

M.

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

* Re: python/babel inline images
  2012-06-06  0:59   ` Mikhail Titov
@ 2012-06-06  5:19     ` Eric Schulte
  2012-06-06 17:41       ` [babel] session initialization (was RE: python/babel inline images) Mikhail Titov
  0 siblings, 1 reply; 22+ messages in thread
From: Eric Schulte @ 2012-06-06  5:19 UTC (permalink / raw)
  To: Mikhail Titov; +Cc: emacs-orgmode, 'henry atting'

"Mikhail Titov" <mlt@gmx.us> writes:

>> -----Original Message-----
>> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
>> bounces+mlt=gmx.us@gnu.org] On Behalf Of Eric Schulte
>> Sent: Tuesday, June 05, 2012 9:57 AM
>> To: henry atting
>> Cc: emacs-orgmode@gnu.org
>> Subject: Re: [O] python/babel inline images
>> 
>> I'm not python expert, but the code block should be run in your current
>> directory, e.g., the following outputs the current working path expected
>> for me.
>> 
>> #+begin_src sh
>>   pwd
>> #+end_src
>> 
>> If you want to explicitly pass the current directory to your code block
>> as an argument, you could try something like the following
>> 
>> #+begin_src python :var mydir=(file-name-directory (buffer-file-name))
>>   return mydir
>> #+end_src
>> 
>
> I've noticed some inconsistency between various languages in this aspect.
> For instance, ob-R starts session in proper working directory, while all
> looks like everything (?) else does not.
>
> Should not it be somewhat standardized? I think it make sense to always cd
> to org doc folder.
>

Most languages should and (at least those I use regularly) do run in the
directory of the containing Org-mode file.  Which languages do not?

Thanks,

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

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

* Re: python/babel inline images
  2012-06-05 22:34   ` William LÉCHELLE
@ 2012-06-06 15:19     ` henry atting
  2012-06-06 19:16       ` Mikhail Titov
  0 siblings, 1 reply; 22+ messages in thread
From: henry atting @ 2012-06-06 15:19 UTC (permalink / raw)
  To: emacs-orgmode

I don't think its a path problem. The code itself works flawlessly. So
the workaround which I already have used is to link to the resulting
image. The only drawback with this solution is that after every
evaluation I have to remove the empty `'Results:' but the heck with
it, I can live with it happily till the end of my days.
However I find that some inconsistency lies therein. Before
python/matplotlib I used gnuplot with which babel had no problem of
this type.


Cheers,
henry

-- 
http://literaturlatenight.de

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

* [babel] session initialization (was RE: python/babel inline images)
  2012-06-06  5:19     ` Eric Schulte
@ 2012-06-06 17:41       ` Mikhail Titov
  2012-06-06 19:01         ` Eric Schulte
  0 siblings, 1 reply; 22+ messages in thread
From: Mikhail Titov @ 2012-06-06 17:41 UTC (permalink / raw)
  To: 'Eric Schulte'; +Cc: emacs-orgmode, 'henry atting'

> -----Original Message-----
> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
> bounces+mlt=gmx.us@gnu.org] On Behalf Of Eric Schulte
> Sent: Wednesday, June 06, 2012 12:20 AM
> To: Mikhail Titov
> Cc: emacs-orgmode@gnu.org; 'henry atting'
> Subject: Re: [O] python/babel inline images
> 
> "Mikhail Titov" <mlt@gmx.us> writes:
> 
> >> -----Original Message-----
> >> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
> >> bounces+mlt=gmx.us@gnu.org] On Behalf Of Eric Schulte
> >> Sent: Tuesday, June 05, 2012 9:57 AM
> >> To: henry atting
> >> Cc: emacs-orgmode@gnu.org
> >> Subject: Re: [O] python/babel inline images
> >>
> >> I'm not python expert, but the code block should be run in your current
> >> directory, e.g., the following outputs the current working path
expected
> >> for me.
> >>
> >> #+begin_src sh
> >>   pwd
> >> #+end_src
> >>
> >> If you want to explicitly pass the current directory to your code block
> >> as an argument, you could try something like the following
> >>
> >> #+begin_src python :var mydir=(file-name-directory (buffer-file-name))
> >>   return mydir
> >> #+end_src
> >>
> >
> > I've noticed some inconsistency between various languages in this
aspect.
> > For instance, ob-R starts session in proper working directory, while all
> > looks like everything (?) else does not.
> >
> > Should not it be somewhat standardized? I think it make sense to always
cd
> > to org doc folder.
> >
> 
> Most languages should and (at least those I use regularly) do run in the
> directory of the containing Org-mode file.  Which languages do not?

I'm working on ob-octave.el which does not. So I'll fix it in this case.
Which leads me to another question I was about to ask. How to comint
commands in org-babel-XXX-initiate-session as session is not assigned yet?
Right now I have something like the following in that function

	  (comint-send-string
	   (get-buffer-process (current-buffer))
	  		       "set(0, 'defaultfigurevisible', 'off');\n")

I do it there as it does not make sense to call for each block. I was about
to write that ob-R does show stuff but I believe it was in earlier versions
of ob-R.el or something as I've checked and indeed nothing appears on screen
as code being wrapped in a device output block.

Also IIRC ob-sh does not change directory though I tried it on Windows with
cmd.exe . Worth mentioning that it tangles into dot sh instead of dot bat or
dot cmd on that platform. It misses platform specific

(defvar org-babel-tangle-lang-exts) 
(if (string-equal system-type "windows-nt") 
  (add-to-list 'org-babel-tangle-lang-exts '("sh" . "bat"))
)

P.S. I feel like I'm hijacking the thread....

M.

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

* Re: [babel] session initialization (was RE: python/babel inline images)
  2012-06-06 17:41       ` [babel] session initialization (was RE: python/babel inline images) Mikhail Titov
@ 2012-06-06 19:01         ` Eric Schulte
  2012-06-06 19:41           ` Mikhail Titov
  0 siblings, 1 reply; 22+ messages in thread
From: Eric Schulte @ 2012-06-06 19:01 UTC (permalink / raw)
  To: Mikhail Titov
  Cc: emacs-orgmode, 'henry atting', 'Eric Schulte'

>> 
>> Most languages should and (at least those I use regularly) do run in the
>> directory of the containing Org-mode file.  Which languages do not?
>
> I'm working on ob-octave.el which does not. So I'll fix it in this
> case.

Great, thanks.

> 
> Which leads me to another question I was about to ask. How to comint
> commands in org-babel-XXX-initiate-session as session is not assigned
> yet?

I don't understand.

> Right now I have something like the following in that function
>
> 	  (comint-send-string
> 	   (get-buffer-process (current-buffer))
> 	  		       "set(0, 'defaultfigurevisible', 'off');\n")
>
> I do it there as it does not make sense to call for each block. I was about
> to write that ob-R does show stuff but I believe it was in earlier versions
> of ob-R.el or something as I've checked and indeed nothing appears on screen
> as code being wrapped in a device output block.
>

I'm not aware of a way to run code on the start of a session.  I do see
how this could be a useful addition.

>
> Also IIRC ob-sh does not change directory though I tried it on Windows with
> cmd.exe.

Granted I only run on linux, but (on linux) ob-sh *does* run in the
directory of the containing Org-mode file.

> Worth mentioning that it tangles into dot sh instead of dot bat or dot
> cmd on that platform. It misses platform specific
>
> (defvar org-babel-tangle-lang-exts) 
> (if (string-equal system-type "windows-nt") 
>   (add-to-list 'org-babel-tangle-lang-exts '("sh" . "bat"))
> )
>

OK, could you convert the above into a patch which we could apply to
ob-sh?

>
> P.S. I feel like I'm hijacking the thread....
>
> M.
>

I am certainly not the OP, but I don't mind, these varied topics all
seem important.

Cheers,

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

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

* Re: python/babel inline images
  2012-06-06 15:19     ` henry atting
@ 2012-06-06 19:16       ` Mikhail Titov
  2012-06-06 19:25         ` Eric Schulte
  2012-06-07  8:43         ` henry atting
  0 siblings, 2 replies; 22+ messages in thread
From: Mikhail Titov @ 2012-06-06 19:16 UTC (permalink / raw)
  To: 'henry atting', emacs-orgmode

> -----Original Message-----
> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
> bounces+mlt=gmx.us@gnu.org] On Behalf Of henry atting
> Sent: Wednesday, June 06, 2012 10:19 AM
> To: emacs-orgmode@gnu.org
> Subject: Re: [O] python/babel inline images
> 
> I don't think its a path problem.

Indeed

#+begin_src python :results output
import os
print(os.getcwd())
#+end_src

Shows location of my org doc.

> The code itself works flawlessly. So
> the workaround which I already have used is to link to the resulting
> image. The only drawback with this solution is that after every
> evaluation I have to remove the empty `'Results:'

You can use :results silent

> but the heck with
> it, I can live with it happily till the end of my days.
> However I find that some inconsistency lies therein. Before
> python/matplotlib I used gnuplot with which babel had no problem of
> this type.

Try using

... :file exp_csv.svg
...
plot.savefig(file=sys.stdout)


#+begin_src python :results output :file zzz.xxx
import os, sys
print(os.getcwd(), file=sys.stdout)
#+end_src

#+RESULTS:
[[file:zzz.xxx]]


Meanwhile I've noticed that I can't return back from editing python code in
a sub-editing buffer. C-c ' does not work and M-x org-edit-src-exit says
"This is not a sub-editing buffer, something is wrong".

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

* Re: python/babel inline images
  2012-06-06 19:16       ` Mikhail Titov
@ 2012-06-06 19:25         ` Eric Schulte
  2012-06-06 19:32           ` Mikhail Titov
  2012-06-07  8:43         ` henry atting
  1 sibling, 1 reply; 22+ messages in thread
From: Eric Schulte @ 2012-06-06 19:25 UTC (permalink / raw)
  To: Mikhail Titov; +Cc: emacs-orgmode, 'henry atting'

"Mikhail Titov" <mlt@gmx.us> writes:

>
> Meanwhile I've noticed that I can't return back from editing python code in
> a sub-editing buffer. C-c ' does not work and M-x org-edit-src-exit says
> "This is not a sub-editing buffer, something is wrong".
>

This works on my system, perhaps the problem is due to something in your
configuration.  Do you still notice this problem when launching emacs
with the -Q option.

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

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

* Re: python/babel inline images
  2012-06-06 19:25         ` Eric Schulte
@ 2012-06-06 19:32           ` Mikhail Titov
  0 siblings, 0 replies; 22+ messages in thread
From: Mikhail Titov @ 2012-06-06 19:32 UTC (permalink / raw)
  To: 'Eric Schulte'; +Cc: emacs-orgmode, 'henry atting'

> -----Original Message-----
> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
> bounces+mlt=gmx.us@gnu.org] On Behalf Of Eric Schulte
> Sent: Wednesday, June 06, 2012 2:25 PM
> To: Mikhail Titov
> Cc: emacs-orgmode@gnu.org; 'henry atting'
> Subject: Re: [O] python/babel inline images
> 
> > Meanwhile I've noticed that I can't return back from editing python code
in
> > a sub-editing buffer. C-c ' does not work and M-x org-edit-src-exit says
> > "This is not a sub-editing buffer, something is wrong".
> >
> 
> This works on my system, perhaps the problem is due to something in your
> configuration.  Do you still notice this problem when launching emacs
> with the -Q option.

Huh... -Q solves it. I'm loading CEDET early in dot emacs for matlab-emacs
(as per manual) and it does something nasty that ruins normal behavior of
C-c '. Bummer.

Outdated eieio 1.3 shadowed to meet minimum version 1.4
Outdated semantic 2.0 shadowed to meet minimum version 2.1
Outdated srecode 1.0 shadowed to meet minimum version 1.1
Outdated ede 1.0 shadowed to meet minimum version 1.1
Outdated speedbar 1.0 shadowed to meet minimum version 1.0.4
Setting up CEDET packages...done

M.

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

* Re: [babel] session initialization (was RE: python/babel inline images)
  2012-06-06 19:01         ` Eric Schulte
@ 2012-06-06 19:41           ` Mikhail Titov
  2012-06-06 23:06             ` Mikhail Titov
  2012-06-07 22:48             ` Eric Schulte
  0 siblings, 2 replies; 22+ messages in thread
From: Mikhail Titov @ 2012-06-06 19:41 UTC (permalink / raw)
  To: 'Eric Schulte'; +Cc: emacs-orgmode

> -----Original Message-----
> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
> bounces+mlt=gmx.us@gnu.org] On Behalf Of Eric Schulte
> Sent: Wednesday, June 06, 2012 2:02 PM
> To: Mikhail Titov
> Cc: emacs-orgmode@gnu.org; 'henry atting'; 'Eric Schulte'
> Subject: Re: [O] [babel] session initialization (was RE: python/babel
inline
> images)
> 
> > Right now I have something like the following in that function
> >
> > 	  (comint-send-string
> > 	   (get-buffer-process (current-buffer))
> > 	  		       "set(0, 'defaultfigurevisible', 'off');\n")
> >
> > I do it there as it does not make sense to call for each block. I was
about
> > to write that ob-R does show stuff but I believe it was in earlier
versions
> > of ob-R.el or something as I've checked and indeed nothing appears on
screen
> > as code being wrapped in a device output block.
> >
> 
> I'm not aware of a way to run code on the start of a session.  I do see
> how this could be a useful addition.

So the fragment above does look legitimate to you? I just add

(format "cd('%s');\n" (file-name-directory (buffer-file-name)))

and problem solved? Well... for sessions. I don't know if it is common to
use non-session based calculations...

> > Also IIRC ob-sh does not change directory though I tried it on Windows
with
> > cmd.exe.
> 
> Granted I only run on linux, but (on linux) ob-sh *does* run in the
> directory of the containing Org-mode file.
> 
> > Worth mentioning that it tangles into dot sh instead of dot bat or dot
> > cmd on that platform. It misses platform specific
> >
> > (defvar org-babel-tangle-lang-exts)
> > (if (string-equal system-type "windows-nt")
> >   (add-to-list 'org-babel-tangle-lang-exts '("sh" . "bat"))
> > )
> >
> 
> OK, could you convert the above into a patch which we could apply to
> ob-sh?

I think there should be more than that. I do sometimes use bash from msys on
Win32 so I'd expect sh as a tangled file name extension in this case. I
believe it can be accomplished as with matlab / octave by adding a new
language cmd that will reuse most of sh. I'll see what I can do.

M.

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

* Re: [babel] session initialization (was RE: python/babel inline images)
  2012-06-06 19:41           ` Mikhail Titov
@ 2012-06-06 23:06             ` Mikhail Titov
  2012-06-07 22:32               ` Mikhail Titov
  2012-06-07 22:48             ` Eric Schulte
  1 sibling, 1 reply; 22+ messages in thread
From: Mikhail Titov @ 2012-06-06 23:06 UTC (permalink / raw)
  To: 'Eric Schulte'; +Cc: emacs-orgmode

> -----Original Message-----
> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
> bounces+mlt=gmx.us@gnu.org] On Behalf Of Mikhail Titov
> Sent: Wednesday, June 06, 2012 2:42 PM
> To: 'Eric Schulte'
> Cc: emacs-orgmode@gnu.org
> Subject: Re: [O] [babel] session initialization (was RE: python/babel
inline
> images)
> 
> > -----Original Message-----
> > From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
> > bounces+mlt=gmx.us@gnu.org] On Behalf Of Eric Schulte
> > Sent: Wednesday, June 06, 2012 2:02 PM
> > To: Mikhail Titov
> > Cc: emacs-orgmode@gnu.org; 'henry atting'; 'Eric Schulte'
> > Subject: Re: [O] [babel] session initialization (was RE: python/babel
> inline
> > images)
> >
> > > Right now I have something like the following in that function
> > >
> > > 	  (comint-send-string
> > > 	   (get-buffer-process (current-buffer))
> > > 	  		       "set(0, 'defaultfigurevisible', 'off');\n")
> > >
> > > I do it there as it does not make sense to call for each block. I was
> about
> > > to write that ob-R does show stuff but I believe it was in earlier
> versions
> > > of ob-R.el or something as I've checked and indeed nothing appears on
> screen
> > > as code being wrapped in a device output block.
> > >
> >
> > I'm not aware of a way to run code on the start of a session.  I do see
> > how this could be a useful addition.
> 
> So the fragment above does look legitimate to you? I just add
> 
> (format "cd('%s');\n" (file-name-directory (buffer-file-name)))
> 
> and problem solved? Well... for sessions. I don't know if it is common to
> use non-session based calculations...

How would I reliably refer to the org doc buffer from where everything was
called? The following works just fine in session initialization code for
Matlab but not for Octave

(file-name-directory (buffer-file-name (other-buffer)))

At this point, (current-buffer) refers to the one with inferior process.

M.

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

* Re: python/babel inline images
  2012-06-06 19:16       ` Mikhail Titov
  2012-06-06 19:25         ` Eric Schulte
@ 2012-06-07  8:43         ` henry atting
  2012-06-07 15:09           ` Mikhail Titov
  1 sibling, 1 reply; 22+ messages in thread
From: henry atting @ 2012-06-07  8:43 UTC (permalink / raw)
  To: Mikhail Titov; +Cc: emacs-orgmode, 'henry atting'

"Mikhail Titov" <mlt@gmx.us> writes:

>> -----Original Message-----
>> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
>> bounces+mlt=gmx.us@gnu.org] On Behalf Of henry atting
>> Sent: Wednesday, June 06, 2012 10:19 AM
>> To: emacs-orgmode@gnu.org
>> Subject: Re: [O] python/babel inline images
>> 
>> I don't think its a path problem.
>
> Indeed
>
> #+begin_src python :results output
> import os
> print(os.getcwd())
> #+end_src
>
> Shows location of my org doc.

Yes, shows my working directory.


>> The code itself works flawlessly. So
>> the workaround which I already have used is to link to the resulting
>> image. The only drawback with this solution is that after every
>> evaluation I have to remove the empty `'Results:'
>
> You can use :results silent

This is great! Actually this solves my problem which means: create a link
to the file and set results to silent.

>> but the heck with
>> it, I can live with it happily till the end of my days.
>> However I find that some inconsistency lies therein. Before
>> python/matplotlib I used gnuplot with which babel had no problem of
>> this type.
>
> Try using
>
> ... :file exp_csv.svg
> ...
> plot.savefig(file=sys.stdout)

This does not compile, the compiler complains about missing arguments.
I have to write it like this:

plot.savefig("file.svg", format='svg')

>
> #+begin_src python :results output :file zzz.xxx
> import os, sys
> print(os.getcwd(), file=sys.stdout)
> #+end_src
>
> #+RESULTS:
> [[file:zzz.xxx]]

Works as exspected.

> [...]

So, just to mention the current state of affairs (with a simple
example):

--8<---------------cut here---------------start------------->8---
#+begin_src python   :results output
from pylab import *

t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
plot(t, s)
show()
savefig("file.svg", format='svg')
#+end_src
--8<---------------cut here---------------start------------->8---

The above code works. But if I set `:file' to `file.svg' with every
evaluation the link to the file is placed after #+RESULTS: but the
file itself is empty. If the code is evaluated without `:file
file.svg' the evaluation simply works and the file.svg appears in the
working directory.

henry


-- 
http://literaturlatenight.de

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

* Re: python/babel inline images
  2012-06-07  8:43         ` henry atting
@ 2012-06-07 15:09           ` Mikhail Titov
  0 siblings, 0 replies; 22+ messages in thread
From: Mikhail Titov @ 2012-06-07 15:09 UTC (permalink / raw)
  To: 'henry atting'; +Cc: emacs-orgmode

> -----Original Message-----
> From: henry atting [mailto:nsmp_06@online.de]
> Sent: Thursday, June 07, 2012 3:43 AM
> To: Mikhail Titov
> Cc: 'henry atting'; emacs-orgmode@gnu.org
> Subject: Re: python/babel inline images
> 
> >> The code itself works flawlessly. So
> >> the workaround which I already have used is to link to the resulting
> >> image. The only drawback with this solution is that after every
> >> evaluation I have to remove the empty `'Results:'
> >
> > You can use :results silent
> 
> This is great! Actually this solves my problem which means: create a link
> to the file and set results to silent.

This has an implication of inability to cache results in case you might want
it. I personally would just leave empty #+RESULTS: block as is and I would
place link manually two lines below it (leave 1 blank line so re-evaluation
won't delete your link).

> 
> >> but the heck with
> >> it, I can live with it happily till the end of my days.
> >> However I find that some inconsistency lies therein. Before
> >> python/matplotlib I used gnuplot with which babel had no problem of
> >> this type.
> >
> > Try using
> >
> > ... :file exp_csv.svg
> > ...
> > plot.savefig(file=sys.stdout)
> 
> This does not compile, the compiler complains about missing arguments.
> I have to write it like this:
> 
> plot.savefig("file.svg", format='svg')

I never used that library but it makes sense:-) The only problem with this
solution that you should make sure nothing else goes to stdout. I think it
is a huge shortcoming.

> So, just to mention the current state of affairs (with a simple
> example):
> 
> --8<---------------cut here---------------start------------->8---
> #+begin_src python   :results output
> from pylab import *
> 
> t = arange(0.0, 2.0, 0.01)
> s = sin(2*pi*t)
> plot(t, s)
> show()
> savefig("file.svg", format='svg')
> #+end_src
> --8<---------------cut here---------------start------------->8---
> 
> The above code works. But if I set `:file' to `file.svg' with every
> evaluation the link to the file is placed after #+RESULTS: but the
> file itself is empty.

As William LECHELLE pointed out that in this case file captures the output
of the savefig() call which does not output anything to stdout. So it is
expected.

> If the code is evaluated without `:file
> file.svg' the evaluation simply works and the file.svg appears in the
> working directory.

I think this is the best way to proceed.

M.

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

* Re: [babel] session initialization (was RE: python/babel inline images)
  2012-06-06 23:06             ` Mikhail Titov
@ 2012-06-07 22:32               ` Mikhail Titov
  2012-06-07 22:44                 ` Eric Schulte
  0 siblings, 1 reply; 22+ messages in thread
From: Mikhail Titov @ 2012-06-07 22:32 UTC (permalink / raw)
  To: 'Eric Schulte'; +Cc: emacs-orgmode

> -----Original Message-----
> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
> bounces+mlt=gmx.us@gnu.org] On Behalf Of Mikhail Titov
> Sent: Wednesday, June 06, 2012 6:06 PM
> To: 'Eric Schulte'
> Cc: emacs-orgmode@gnu.org
> Subject: Re: [O] [babel] session initialization (was RE: python/babel
inline
> images)
> 
> ...
> 
> How would I reliably refer to the org doc buffer from where everything was
> called? The following works just fine in session initialization code for
> Matlab but not for Octave
> 
> (file-name-directory (buffer-file-name (other-buffer)))
> 
> At this point, (current-buffer) refers to the one with inferior process.

So I ended up using the following in session initialization for inferior
process to get a current folder of a document from where calculations
initiated

(file-name-directory
   (car (delq nil (mapcar #'buffer-file-name (buffer-list)))))

It works. But I have a feeling that it is not a reliable method. Any
suggestions?

M.

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

* Re: [babel] session initialization (was RE: python/babel inline images)
  2012-06-07 22:32               ` Mikhail Titov
@ 2012-06-07 22:44                 ` Eric Schulte
  2012-06-07 23:02                   ` Mikhail Titov
  0 siblings, 1 reply; 22+ messages in thread
From: Eric Schulte @ 2012-06-07 22:44 UTC (permalink / raw)
  To: Mikhail Titov; +Cc: emacs-orgmode, 'Eric Schulte'

"Mikhail Titov" <mlt@gmx.us> writes:

>> -----Original Message-----
>> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
>> bounces+mlt=gmx.us@gnu.org] On Behalf Of Mikhail Titov
>> Sent: Wednesday, June 06, 2012 6:06 PM
>> To: 'Eric Schulte'
>> Cc: emacs-orgmode@gnu.org
>> Subject: Re: [O] [babel] session initialization (was RE: python/babel
> inline
>> images)
>> 
>> ...
>> 
>> How would I reliably refer to the org doc buffer from where everything was
>> called? The following works just fine in session initialization code for
>> Matlab but not for Octave
>> 
>> (file-name-directory (buffer-file-name (other-buffer)))
>> 
>> At this point, (current-buffer) refers to the one with inferior process.
>
> So I ended up using the following in session initialization for inferior
> process to get a current folder of a document from where calculations
> initiated
>
> (file-name-directory
>    (car (delq nil (mapcar #'buffer-file-name (buffer-list)))))
>
> It works. But I have a feeling that it is not a reliable method. Any
> suggestions?
>

I would recommend using the `default-directory' variable.  That should
hold the value of the directory of the Org-mode file or the value of the
:dir header argument if present.

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

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

* Re: [babel] session initialization (was RE: python/babel inline images)
  2012-06-06 19:41           ` Mikhail Titov
  2012-06-06 23:06             ` Mikhail Titov
@ 2012-06-07 22:48             ` Eric Schulte
  1 sibling, 0 replies; 22+ messages in thread
From: Eric Schulte @ 2012-06-07 22:48 UTC (permalink / raw)
  To: Mikhail Titov; +Cc: emacs-orgmode

"Mikhail Titov" <mlt@gmx.us> writes:

>> -----Original Message-----
>> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
>> bounces+mlt=gmx.us@gnu.org] On Behalf Of Eric Schulte
>> Sent: Wednesday, June 06, 2012 2:02 PM
>> To: Mikhail Titov
>> Cc: emacs-orgmode@gnu.org; 'henry atting'; 'Eric Schulte'
>> Subject: Re: [O] [babel] session initialization (was RE: python/babel inline images)
>> 
>> > Right now I have something like the following in that function
>> >
>> > 	  (comint-send-string
>> > 	   (get-buffer-process (current-buffer))
>> > 	  		       "set(0, 'defaultfigurevisible', 'off');\n")
>> >
>> > I do it there as it does not make sense to call for each block. I
>> > was about to write that ob-R does show stuff but I believe it was
>> > in earlier versions of ob-R.el or something as I've checked and
>> > indeed nothing appears on screen as code being wrapped in a device
>> > output block.
>> >
>> 
>> I'm not aware of a way to run code on the start of a session.  I do see
>> how this could be a useful addition.
>
> So the fragment above does look legitimate to you? I just add
>
> (format "cd('%s');\n" (file-name-directory (buffer-file-name)))
>
> and problem solved? Well... for sessions. I don't know if it is common to
> use non-session based calculations...
>

The vast majority of my code blocks do not use sessions but I don't use
matlab or octave.  Running something like the following as part of the
session start up does seem reasonable.

  (format "cd('%s');\n" default-directory)

>
>> > Also IIRC ob-sh does not change directory though I tried it on
>> > Windows with cmd.exe.
>> 
>> Granted I only run on linux, but (on linux) ob-sh *does* run in the
>> directory of the containing Org-mode file.
>> 
>> > Worth mentioning that it tangles into dot sh instead of dot bat or dot
>> > cmd on that platform. It misses platform specific
>> >
>> > (defvar org-babel-tangle-lang-exts)
>> > (if (string-equal system-type "windows-nt")
>> >   (add-to-list 'org-babel-tangle-lang-exts '("sh" . "bat"))
>> > )
>> >
>> 
>> OK, could you convert the above into a patch which we could apply to
>> ob-sh?
>
> I think there should be more than that. I do sometimes use bash from msys on
> Win32 so I'd expect sh as a tangled file name extension in this case. I
> believe it can be accomplished as with matlab / octave by adding a new
> language cmd that will reuse most of sh. I'll see what I can do.
>

The alternate (and probably simpler) solution here is to set the default
to whichever of .sh or .bat seems more likely to be desired based on the
OS, and then let the user customize the `org-babel-tangle-lang-exts' to
suite their preference.

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

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

* Re: [babel] session initialization (was RE: python/babel inline images)
  2012-06-07 22:44                 ` Eric Schulte
@ 2012-06-07 23:02                   ` Mikhail Titov
  2012-06-08  1:56                     ` Mikhail Titov
  0 siblings, 1 reply; 22+ messages in thread
From: Mikhail Titov @ 2012-06-07 23:02 UTC (permalink / raw)
  To: 'Eric Schulte'; +Cc: emacs-orgmode

> -----Original Message-----
> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
> bounces+mlt=gmx.us@gnu.org] On Behalf Of Eric Schulte
> Sent: Thursday, June 07, 2012 5:44 PM
> To: Mikhail Titov
> Cc: emacs-orgmode@gnu.org; 'Eric Schulte'
> Subject: Re: [O] [babel] session initialization (was RE: python/babel
inline
> images)
> 
> >> ...
> >>
> >> How would I reliably refer to the org doc buffer from where everything
was
> >> called? The following works just fine in session initialization code
for
> >> Matlab but not for Octave
> >>
> >
> > (file-name-directory
> >    (car (delq nil (mapcar #'buffer-file-name (buffer-list)))))
> >
> > It works. But I have a feeling that it is not a reliable method. Any
> > suggestions?
> >
> 
> I would recommend using the `default-directory' variable.  That should
> hold the value of the directory of the Org-mode file or the value of the
> :dir header argument if present.

Huh... now it works... I remember I've tried before but for some reason it
was nil.. maybe typo though I have AC.

The only trick [1] is to use (file-truename default-directory) as Matlab
does not like ~.

Thanks!

M. 

[1]
http://stackoverflow.com/questions/291976/relative-path-to-absolute-path-in-
elisp

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

* Re: [babel] session initialization (was RE: python/babel inline images)
  2012-06-07 23:02                   ` Mikhail Titov
@ 2012-06-08  1:56                     ` Mikhail Titov
  2012-06-08  2:18                       ` Eric Schulte
  0 siblings, 1 reply; 22+ messages in thread
From: Mikhail Titov @ 2012-06-08  1:56 UTC (permalink / raw)
  To: 'Eric Schulte'; +Cc: emacs-orgmode

> -----Original Message-----
> From: emacs-orgmode-bounces+mlt=gmx.us@gnu.org [mailto:emacs-orgmode-
> bounces+mlt=gmx.us@gnu.org] On Behalf Of Mikhail Titov
> Sent: Thursday, June 07, 2012 6:03 PM
> To: 'Eric Schulte'
> Cc: emacs-orgmode@gnu.org
> Subject: Re: [O] [babel] session initialization (was RE: python/babel
inline
> images)
> 
> > >> ...
> > >>
> > >> How would I reliably refer to the org doc buffer from where
everything
> was
> > >> called? The following works just fine in session initialization code
> for
> > >> Matlab but not for Octave
> > >>
> > >
> > > (file-name-directory
> > >    (car (delq nil (mapcar #'buffer-file-name (buffer-list)))))
> > >
> > > It works. But I have a feeling that it is not a reliable method. Any
> > > suggestions?
> > >
> >
> > I would recommend using the `default-directory' variable.  That should
> > hold the value of the directory of the Org-mode file or the value of the
> > :dir header argument if present.
> 
> Huh... now it works... I remember I've tried before but for some reason it
> was nil.. maybe typo though I have AC.
> 
> The only trick [1] is to use (file-truename default-directory) as Matlab
> does not like ~.

Turns out this does not work for octave for some reason. default-directory
points to my home folder.

Also I'm getting "ansi-color-process-output: Marker does not point anywhere"
for Octave. So I have to C-c a couple of times on the block for the first
time. First evaluation just launches session but breaks because of that
error. Second time (when session is up) C-c works just fine and dumps
result. Non-session version of Octave works as expected. I wonder if it is
something Windows-specific... I can see test on :session for Octave and
Achim Gratz said it passed tests.

The problem occurs to me with git version of ob-octave.el as well. So I'm
confident it is not me who broke something during experiments :-)

Any idea? Matlab works just fine (session)!

I'm afraid I'll leave all the things as is for now. Maybe someone else can
fix it later.

M.
 

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

* Re: [babel] session initialization (was RE: python/babel inline images)
  2012-06-08  1:56                     ` Mikhail Titov
@ 2012-06-08  2:18                       ` Eric Schulte
  0 siblings, 0 replies; 22+ messages in thread
From: Eric Schulte @ 2012-06-08  2:18 UTC (permalink / raw)
  To: Mikhail Titov; +Cc: emacs-orgmode

"Mikhail Titov" <mlt@gmx.us> writes:
[...]
>> > I would recommend using the `default-directory' variable.  That should
>> > hold the value of the directory of the Org-mode file or the value of the
>> > :dir header argument if present.
>> 
>> Huh... now it works... I remember I've tried before but for some reason it
>> was nil.. maybe typo though I have AC.
>> 
>> The only trick [1] is to use (file-truename default-directory) as Matlab
>> does not like ~.
>
> Turns out this does not work for octave for some reason. default-directory
> points to my home folder.
>
> Also I'm getting "ansi-color-process-output: Marker does not point anywhere"
> for Octave. So I have to C-c a couple of times on the block for the first
> time. First evaluation just launches session but breaks because of that
> error. Second time (when session is up) C-c works just fine and dumps
> result. Non-session version of Octave works as expected. I wonder if it is
> something Windows-specific... I can see test on :session for Octave and
> Achim Gratz said it passed tests.
>
> The problem occurs to me with git version of ob-octave.el as well. So I'm
> confident it is not me who broke something during experiments :-)
>
> Any idea? Matlab works just fine (session)!
>
> I'm afraid I'll leave all the things as is for now. Maybe someone else can
> fix it later.
>

Unfortunately the developer of matlab/octave support for Babel no longer
tracks this list, and I have no access to matlab/octave, so while I'm
happy to offer advice and commit patches I can't really improve support
for these languages.

RE: default-directory pointing to your home directory, my guess would be
that at the beginning of the `org-babel-execute:octave'
default-directory has the desired value, but then within the octave
comint buffer the value changes, so it should still be possible to use
`default-directory'.

The ansi-color-process-output stuff sounds like something that
octave-mode should be taking care of, i.e., not something directly
related to Org-mode.

Sorry I can't be of more assistance.

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

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

end of thread, other threads:[~2012-06-08  2:18 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-05 10:30 python/babel inline images henry atting
2012-06-05 12:46 ` Brett Viren
2012-06-05 14:57 ` Eric Schulte
2012-06-05 22:34   ` William LÉCHELLE
2012-06-06 15:19     ` henry atting
2012-06-06 19:16       ` Mikhail Titov
2012-06-06 19:25         ` Eric Schulte
2012-06-06 19:32           ` Mikhail Titov
2012-06-07  8:43         ` henry atting
2012-06-07 15:09           ` Mikhail Titov
2012-06-06  0:59   ` Mikhail Titov
2012-06-06  5:19     ` Eric Schulte
2012-06-06 17:41       ` [babel] session initialization (was RE: python/babel inline images) Mikhail Titov
2012-06-06 19:01         ` Eric Schulte
2012-06-06 19:41           ` Mikhail Titov
2012-06-06 23:06             ` Mikhail Titov
2012-06-07 22:32               ` Mikhail Titov
2012-06-07 22:44                 ` Eric Schulte
2012-06-07 23:02                   ` Mikhail Titov
2012-06-08  1:56                     ` Mikhail Titov
2012-06-08  2:18                       ` Eric Schulte
2012-06-07 22:48             ` Eric Schulte

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).