* Output result of source block to a file @ 2019-06-05 10:41 Roger Mason 2019-06-05 13:17 ` stardiviner 2019-06-05 13:33 ` Ken Mankoff 0 siblings, 2 replies; 12+ messages in thread From: Roger Mason @ 2019-06-05 10:41 UTC (permalink / raw) To: Org-Mode Hello, I want to output the result of the evaluation of a (python) source block to a (graphics) file and have a link to the file inserted in the buffer. Here is the code: #+begin_src python :results value file :file scaleplot02.pdf :exports results :var data=test import matplotlib.pyplot as plt import csv scale = [] lescale = [] cdscale = [] energy = [] leenergy = [] cdenergy = [] for row in data: scale.append(float(row[0])) energy.append(float(row[1])) if int(row[2]) != 0: lescale.append(float(row[0])) leenergy.append(float(row[1])) if int(row[3]) != 0: cdscale.append(float(row[0])) cdenergy.append(float(row[1])) plt.plot(scale,energy, 'r+') plt.plot(lescale,leenergy, 'go') plt.plot(cdscale,cdenergy, 'bo') plt.xlabel('scale') plt.ylabel('energy (Ha)') plt.title('Energy vs scale') plt.legend() plt.savefig(file) print ("[[./%s]]" % file) #+end_src This is the error: NameError: name 'file' is not defined Obviously the syntax on the begin_src line is wrong, but what should it be instead? Thanks, Roger ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Output result of source block to a file 2019-06-05 10:41 Output result of source block to a file Roger Mason @ 2019-06-05 13:17 ` stardiviner 2019-06-06 11:36 ` Roger Mason 2019-06-06 18:04 ` Roger Mason 2019-06-05 13:33 ` Ken Mankoff 1 sibling, 2 replies; 12+ messages in thread From: stardiviner @ 2019-06-05 13:17 UTC (permalink / raw) To: emacs-orgmode Roger Mason <rmason@mun.ca> writes: > Hello, > > I want to output the result of the evaluation of a (python) source block > to a (graphics) file and have a link to the file inserted in the buffer. For this, you should try updated header arguments like ":results graphics link" or ":results file link". > > Here is the code: > > #+begin_src python :results value file :file scaleplot02.pdf :exports results :var data=test > import matplotlib.pyplot as plt > import csv > > scale = [] > lescale = [] > cdscale = [] > > energy = [] > leenergy = [] > cdenergy = [] > > for row in data: > scale.append(float(row[0])) > energy.append(float(row[1])) > if int(row[2]) != 0: > lescale.append(float(row[0])) > leenergy.append(float(row[1])) > if int(row[3]) != 0: > cdscale.append(float(row[0])) > cdenergy.append(float(row[1])) > > plt.plot(scale,energy, 'r+') > plt.plot(lescale,leenergy, 'go') > plt.plot(cdscale,cdenergy, 'bo') > plt.xlabel('scale') > plt.ylabel('energy (Ha)') > plt.title('Energy vs scale') > plt.legend() > plt.savefig(file) > print ("[[./%s]]" % file) > #+end_src > > This is the error: > > NameError: name 'file' is not defined > > Obviously the syntax on the begin_src line is wrong, but what should it > be instead? > > Thanks, > Roger -- [ stardiviner ] I try to make every word tell the meaning what I want to express. Blog: https://stardiviner.github.io/ IRC(freenode): stardiviner, Matrix: stardiviner GPG: F09F650D7D674819892591401B5DF1C95AE89AC3 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Output result of source block to a file 2019-06-05 13:17 ` stardiviner @ 2019-06-06 11:36 ` Roger Mason 2019-06-07 11:05 ` stardiviner 2019-06-06 18:04 ` Roger Mason 1 sibling, 1 reply; 12+ messages in thread From: Roger Mason @ 2019-06-06 11:36 UTC (permalink / raw) To: numbchild; +Cc: emacs-orgmode Hello, stardiviner <numbchild@gmail.com> writes: > Roger Mason <rmason@mun.ca> writes: > >> Hello, >> >> I want to output the result of the evaluation of a (python) source block >> to a (graphics) file and have a link to the file inserted in the buffer. >> #+begin_src python :results value file :file scaleplot02.pdf :exports results :var data=test ...snip... >> plt.savefig(file) >> print ("[[./%s]]" % file) >> #+end_src > For this, you should try updated header arguments like ":results graphics link" > or ":results file link". Thanks for your reply. The current incarnation: #+begin_src python :results graphics link :exports results :var data=test :var file="scaleplot2.pdf" The output: #+RESULTS: scaleplot02 : None I'm guessing I have the syntax wrong. Is it documented somewhere? I'm on Org mode version 9.1.13 (release_9.1.13-784-ged7d1d @ /home/rmason/.emacs.d/org-git/lisp/). Cheers, Roger ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Output result of source block to a file 2019-06-06 11:36 ` Roger Mason @ 2019-06-07 11:05 ` stardiviner 0 siblings, 0 replies; 12+ messages in thread From: stardiviner @ 2019-06-07 11:05 UTC (permalink / raw) To: Roger Mason; +Cc: emacs-orgmode Roger Mason <rmason@mun.ca> writes: > Hello, > > stardiviner <numbchild@gmail.com> writes: > >> Roger Mason <rmason@mun.ca> writes: >> >>> Hello, >>> >>> I want to output the result of the evaluation of a (python) source block >>> to a (graphics) file and have a link to the file inserted in the buffer. > >>> #+begin_src python :results value file :file scaleplot02.pdf :exports results :var data=test > > ...snip... > >>> plt.savefig(file) >>> print ("[[./%s]]" % file) >>> #+end_src > >> For this, you should try updated header arguments like ":results graphics link" >> or ":results file link". > > Thanks for your reply. > > The current incarnation: > > #+begin_src python :results graphics link :exports results :var data=test :var file="scaleplot2.pdf" > > The output: > #+RESULTS: scaleplot02 > : None > First, check out =org-manual.org= documentation. And you don't need ~print ("[[./%s]]" % file)~ in Python code if you use ~:results graphics link~. > I'm guessing I have the syntax wrong. Is it documented somewhere? I'm > on Org mode version 9.1.13 (release_9.1.13-784-ged7d1d @ > /home/rmason/.emacs.d/org-git/lisp/). > > Cheers, > Roger -- [ stardiviner ] I try to make every word tell the meaning what I want to express. Blog: https://stardiviner.github.io/ IRC(freenode): stardiviner, Matrix: stardiviner GPG: F09F650D7D674819892591401B5DF1C95AE89AC3 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Output result of source block to a file 2019-06-05 13:17 ` stardiviner 2019-06-06 11:36 ` Roger Mason @ 2019-06-06 18:04 ` Roger Mason 2019-06-06 21:12 ` John Kitchin 1 sibling, 1 reply; 12+ messages in thread From: Roger Mason @ 2019-06-06 18:04 UTC (permalink / raw) To: numbchild; +Cc: emacs-orgmode Hello again, stardiviner <numbchild@gmail.com> writes: > Roger Mason <rmason@mun.ca> writes: > >> Hello, >> >> I want to output the result of the evaluation of a (python) source block >> to a (graphics) file and have a link to the file inserted in the buffer. > > For this, you should try updated header arguments like ":results graphics link" > or ":results file link". I updated to Org mode version 9.2.3 (release_9.2.3-390-gfb5091 @ /home/rmason/.emacs.d/org-git/lisp/) This works but sets the results to verbatim, hence the exported & compiled code does not include the graphic. #+name: scaleplot03 #+begin_src python :results output graphics link :exports results :var data=test :var file="scaleplot03.pdf" import matplotlib.pyplot as plt import csv scale = [] lescale = [] cdscale = [] energy = [] leenergy = [] cdenergy = [] for row in data: scale.append(float(row[0])) energy.append(float(row[1])) if int(row[2]) != 0: lescale.append(float(row[0])) leenergy.append(float(row[1])) if int(row[3]) != 0: cdscale.append(float(row[0])) cdenergy.append(float(row[1])) plt.plot(scale,energy, 'r+') plt.plot(lescale,leenergy, 'go') plt.plot(cdscale,cdenergy, 'bo') plt.xlabel('scale') plt.ylabel('energy (Ha)') plt.title('Energy vs scale') plt.legend() plt.savefig(file) print ("./%s" % file) #+end_src #+RESULTS: scaleplot03 : ./scaleplot03.pdf This header does not work either: #+name: scaleplot04 #+begin_src python :results file link :exports results :var data=test :var file="scaleplot04.pdf" ( same pythone code here) #+RESULTS: scaleplot04 [[file:None]] ]] #+end_src Thank you for your help. Roger ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Output result of source block to a file 2019-06-06 18:04 ` Roger Mason @ 2019-06-06 21:12 ` John Kitchin 2019-06-07 10:00 ` Roger Mason 0 siblings, 1 reply; 12+ messages in thread From: John Kitchin @ 2019-06-06 21:12 UTC (permalink / raw) To: Roger Mason; +Cc: org-mode-email [-- Attachment #1: Type: text/plain, Size: 2427 bytes --] I think you can use something like this: #+BEGIN_SRC python :results output file :var fname="test.png" import matplotlib.pyplot as plt import io f = io.StringIO() plt.plot([1, 2, 3, 17]) plt.savefig(fname) print(fname, end='') #+END_SRC It is in Python3. John ----------------------------------- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu On Thu, Jun 6, 2019 at 2:27 PM Roger Mason <rmason@mun.ca> wrote: > Hello again, > > stardiviner <numbchild@gmail.com> writes: > > > Roger Mason <rmason@mun.ca> writes: > > > >> Hello, > >> > >> I want to output the result of the evaluation of a (python) source block > >> to a (graphics) file and have a link to the file inserted in the buffer. > > > > For this, you should try updated header arguments like ":results > graphics link" > > or ":results file link". > > I updated to Org mode version 9.2.3 (release_9.2.3-390-gfb5091 @ > /home/rmason/.emacs.d/org-git/lisp/) > > This works but sets the results to verbatim, hence the exported & > compiled code does not include the graphic. > > #+name: scaleplot03 > #+begin_src python :results output graphics link :exports results :var > data=test :var file="scaleplot03.pdf" > import matplotlib.pyplot as plt > import csv > > scale = [] > lescale = [] > cdscale = [] > > energy = [] > leenergy = [] > cdenergy = [] > > for row in data: > scale.append(float(row[0])) > energy.append(float(row[1])) > if int(row[2]) != 0: > lescale.append(float(row[0])) > leenergy.append(float(row[1])) > if int(row[3]) != 0: > cdscale.append(float(row[0])) > cdenergy.append(float(row[1])) > > plt.plot(scale,energy, 'r+') > plt.plot(lescale,leenergy, 'go') > plt.plot(cdscale,cdenergy, 'bo') > plt.xlabel('scale') > plt.ylabel('energy (Ha)') > plt.title('Energy vs scale') > plt.legend() > plt.savefig(file) > print ("./%s" % file) > #+end_src > > #+RESULTS: scaleplot03 > : ./scaleplot03.pdf > > This header does not work either: > #+name: scaleplot04 > #+begin_src python :results file link :exports results :var data=test :var > file="scaleplot04.pdf" > ( same pythone code here) > > #+RESULTS: scaleplot04 > [[file:None]] > ]] > #+end_src > > Thank you for your help. > > Roger > > > > [-- Attachment #2: Type: text/html, Size: 3633 bytes --] ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Output result of source block to a file 2019-06-06 21:12 ` John Kitchin @ 2019-06-07 10:00 ` Roger Mason 2019-06-07 15:01 ` John Kitchin 0 siblings, 1 reply; 12+ messages in thread From: Roger Mason @ 2019-06-07 10:00 UTC (permalink / raw) To: John Kitchin; +Cc: Roger Mason, org-mode-email Hello John, John Kitchin <jkitchin@andrew.cmu.edu> writes: > I think you can use something like this: > > #+BEGIN_SRC python :results output file :var fname="test.png" > import matplotlib.pyplot as plt > import io > > f = io.StringIO() > plt.plot([1, 2, 3, 17]) > plt.savefig(fname) > print(fname, end='') > #+END_SRC > > It is in Python3. Thank you. That works. Best wishes, Roger ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Output result of source block to a file 2019-06-07 10:00 ` Roger Mason @ 2019-06-07 15:01 ` John Kitchin 2019-06-08 10:51 ` Roger Mason 0 siblings, 1 reply; 12+ messages in thread From: John Kitchin @ 2019-06-07 15:01 UTC (permalink / raw) To: Roger Mason; +Cc: org-mode-email you probably figured out the "import io" and "f = io..." line are not necessary here. I couldn't figure out a reasonable way to use :results graphics link that didn't result in repeating the filename more than desired. These also both work, but seem to both require repeating the filename twice. #+BEGIN_SRC python :results graphics link :var fname="test.png" :file "test.png" import matplotlib.pyplot as plt plt.plot([1, 2, 3, 1]) plt.savefig(fname) #+END_SRC #+BEGIN_SRC python :results graphics link :file "test.png" import matplotlib.pyplot as plt plt.plot([1, 2, 3]) plt.savefig("test.png") #+END_SRC Something like this should work, but there seem to be some extra bytes getting put in the png file from the decoding, and latin-1 is the only one I can get to work. If anyone knows how to get this to work, I am interested in seeing it! #+BEGIN_SRC python :results value :file "io.png" import matplotlib.pyplot as plt import io buf = io.BytesIO() plt.plot([1, 2, 3]) plt.savefig(buf, format='png') s = buf.getvalue() return s.decode('latin-1') #+END_SRC In general though, all of these are much more work than using ob-ipython, which just puts images in the buffer for you. Roger Mason <rmason@mun.ca> writes: > Hello John, > > John Kitchin <jkitchin@andrew.cmu.edu> writes: > >> I think you can use something like this: >> >> #+BEGIN_SRC python :results output file :var fname="test.png" >> import matplotlib.pyplot as plt >> import io >> >> f = io.StringIO() >> plt.plot([1, 2, 3, 17]) >> plt.savefig(fname) >> print(fname, end='') >> #+END_SRC >> >> It is in Python3. > > Thank you. That works. > > Best wishes, > Roger -- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Output result of source block to a file 2019-06-07 15:01 ` John Kitchin @ 2019-06-08 10:51 ` Roger Mason 2019-06-08 20:40 ` John Kitchin 0 siblings, 1 reply; 12+ messages in thread From: Roger Mason @ 2019-06-08 10:51 UTC (permalink / raw) To: John Kitchin; +Cc: Roger Mason, org-mode-email Hello John, John Kitchin <jkitchin@andrew.cmu.edu> writes: > you probably figured out the "import io" and "f = io..." line are not > necessary here. Indeed. > I couldn't figure out a reasonable way to use :results graphics link > that didn't result in repeating the filename more than desired. These > also both work, but seem to both require repeating the filename twice. > > #+BEGIN_SRC python :results graphics link :var fname="test.png" :file "test.png" > import matplotlib.pyplot as plt > > plt.plot([1, 2, 3, 1]) > plt.savefig(fname) > #+END_SRC > > #+BEGIN_SRC python :results graphics link :file "test.png" > import matplotlib.pyplot as plt > > plt.plot([1, 2, 3]) > plt.savefig("test.png") > #+END_SRC > > Something like this should work, but there seem to be some extra bytes > getting put in the png file from the decoding, and latin-1 is the only > one I can get to work. If anyone knows how to get this to work, I am > interested in seeing it! > > #+BEGIN_SRC python :results value :file "io.png" > import matplotlib.pyplot as plt > import io > buf = io.BytesIO() > > plt.plot([1, 2, 3]) > plt.savefig(buf, format='png') > > s = buf.getvalue() > return s.decode('latin-1') > #+END_SRC > > > In general though, all of these are much more work than using > ob-ipython, which just puts images in the buffer for you. I will investigate that, thanks for the tip. I began this bit of work using gnuplot for making x-y plots, but I find that gnuplot syntax gets messy for anything but simple data. I am not a particular fan of python so I'm also looking into guile & racket for plotting. Thanks for your help, it is much appreciated. Roger ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Output result of source block to a file 2019-06-08 10:51 ` Roger Mason @ 2019-06-08 20:40 ` John Kitchin 0 siblings, 0 replies; 12+ messages in thread From: John Kitchin @ 2019-06-08 20:40 UTC (permalink / raw) To: Roger Mason; +Cc: org-mode-email [-- Attachment #1: Type: text/plain, Size: 2394 bytes --] A similar approach as the last one should work, the problem I was having is that to print the binary string from python you have to decode it, and latin-1 seems close to right, but it puts a bunch of extra bytes in it that lead to a bad png file. I feel like this worked in Python2 with StringIO, but not in Python3 with BytesIO. John ----------------------------------- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu On Sat, Jun 8, 2019 at 6:52 AM Roger Mason <rmason@mun.ca> wrote: > Hello John, > > John Kitchin <jkitchin@andrew.cmu.edu> writes: > > > you probably figured out the "import io" and "f = io..." line are not > > necessary here. > > Indeed. > > > I couldn't figure out a reasonable way to use :results graphics link > > that didn't result in repeating the filename more than desired. These > > also both work, but seem to both require repeating the filename twice. > > > > #+BEGIN_SRC python :results graphics link :var fname="test.png" :file > "test.png" > > import matplotlib.pyplot as plt > > > > plt.plot([1, 2, 3, 1]) > > plt.savefig(fname) > > #+END_SRC > > > > #+BEGIN_SRC python :results graphics link :file "test.png" > > import matplotlib.pyplot as plt > > > > plt.plot([1, 2, 3]) > > plt.savefig("test.png") > > #+END_SRC > > > > Something like this should work, but there seem to be some extra bytes > > getting put in the png file from the decoding, and latin-1 is the only > > one I can get to work. If anyone knows how to get this to work, I am > > interested in seeing it! > > > > #+BEGIN_SRC python :results value :file "io.png" > > import matplotlib.pyplot as plt > > import io > > buf = io.BytesIO() > > > > plt.plot([1, 2, 3]) > > plt.savefig(buf, format='png') > > > > s = buf.getvalue() > > return s.decode('latin-1') > > #+END_SRC > > > > > > In general though, all of these are much more work than using > > ob-ipython, which just puts images in the buffer for you. > > I will investigate that, thanks for the tip. I began this bit of work > using gnuplot for making x-y plots, but I find that gnuplot syntax gets > messy for anything but simple data. I am not a particular fan > of python so I'm also looking into guile & racket for plotting. > > Thanks for your help, it is much appreciated. > > Roger > [-- Attachment #2: Type: text/html, Size: 3447 bytes --] ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Output result of source block to a file 2019-06-05 10:41 Output result of source block to a file Roger Mason 2019-06-05 13:17 ` stardiviner @ 2019-06-05 13:33 ` Ken Mankoff 2019-06-06 11:48 ` Roger Mason 1 sibling, 1 reply; 12+ messages in thread From: Ken Mankoff @ 2019-06-05 13:33 UTC (permalink / raw) To: Roger Mason; +Cc: Org-Mode On 2019-06-05 at 12:41 +02, Roger Mason <rmason@mun.ca> wrote... > I want to output the result of the evaluation of a (python) source > block to a (graphics) file and have a link to the file inserted in the > buffer. > > Here is the code: > > #+begin_src python :results value file :file scaleplot02.pdf :exports results :var data=test > print ("[[./%s]]" % file) > #+end_src What about :var file="scaleplot02.pdf". -k. ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Output result of source block to a file 2019-06-05 13:33 ` Ken Mankoff @ 2019-06-06 11:48 ` Roger Mason 0 siblings, 0 replies; 12+ messages in thread From: Roger Mason @ 2019-06-06 11:48 UTC (permalink / raw) To: Ken Mankoff; +Cc: Roger Mason, Org-Mode Hello, Ken Mankoff <mankoff@gmail.com> writes: > > What about :var file="scaleplot02.pdf". That was my starting point, but the resulting link is prefixed by ':' Perhaps I should upgrade org. Cheers, Roger ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2019-06-08 20:41 UTC | newest] Thread overview: 12+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2019-06-05 10:41 Output result of source block to a file Roger Mason 2019-06-05 13:17 ` stardiviner 2019-06-06 11:36 ` Roger Mason 2019-06-07 11:05 ` stardiviner 2019-06-06 18:04 ` Roger Mason 2019-06-06 21:12 ` John Kitchin 2019-06-07 10:00 ` Roger Mason 2019-06-07 15:01 ` John Kitchin 2019-06-08 10:51 ` Roger Mason 2019-06-08 20:40 ` John Kitchin 2019-06-05 13:33 ` Ken Mankoff 2019-06-06 11:48 ` Roger Mason
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).