From mboxrd@z Thu Jan 1 00:00:00 1970 From: "numbchild@gmail.com" Subject: Re: Hope ob-js can support :session feature Date: Sat, 23 Dec 2017 09:07:11 +0800 Message-ID: References: <0a1fd382-6c99-e69d-c998-b3157edab040@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="001a113b1a18633a6a0560f78f94" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:37862) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eSYHx-0004Qt-26 for emacs-orgmode@gnu.org; Fri, 22 Dec 2017 20:07:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eSYHv-0002Rc-Dn for emacs-orgmode@gnu.org; Fri, 22 Dec 2017 20:07:45 -0500 Received: from mail-oi0-x234.google.com ([2607:f8b0:4003:c06::234]:42854) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eSYHv-0002R8-5P for emacs-orgmode@gnu.org; Fri, 22 Dec 2017 20:07:43 -0500 Received: by mail-oi0-x234.google.com with SMTP id o64so19954924oia.9 for ; Fri, 22 Dec 2017 17:07:43 -0800 (PST) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Martin Alsinet Cc: emacs-orgmode --001a113b1a18633a6a0560f78f94 Content-Type: text/plain; charset="UTF-8" Can you describe how do you do this in detailed? Like: > but since I am using docker containers to run node, I always mount the current directory as a volume in /app inside the container, so that works out fine. > I also think that this way forces me to separate the code in modular blocks, which is already a good practice in itself. How to do this? About this: > With sessions you have to make sure to execute the blocks in the correct order to build the "state" that your current block needs. I think have to use `noweb` reference here. (If you have a blog article describe this whole JS literate programming setup, that will be useful.) Finally, thanks you provide this paradigm. [stardiviner] GPG key ID: 47C32433 IRC(freeenode): stardiviner Twitter: @numbchild Key fingerprint = 9BAA 92BC CDDD B9EF 3B36 CB99 B8C4 B8E5 47C3 2433 Blog: http://stardiviner.github.io/ On Sat, Dec 23, 2017 at 2:32 AM, Martin Alsinet wrote: > Hello stardiviner, > > On Fri, Dec 22, 2017 at 6:57 AM stardiviner wrote: > >> >> I wish to do JavaScript Literate Programming in Org-mode. >> >> So the :session header argument is very necessary. >> >> > I do Literate Programming in Javascript with Org-mode, and I have found a > workaround using a combination of tangled files and require. > > I separate the logic in source code blocks which then I tangle into js > files and I require those js files in other source blocks. > > Example: > > #+BEGIN_SRC js :tangle src/parser.js > const fs = require('fs'); > const parse = require('csv-parse') > > function columns(line){ > return line.map(s => s.toLowerCase()); > } > parse_csv = function(filename, fn, limit){ > fs.readFile(filename, "utf8", function (err, fileData) { > var opts = {columns: columns, trim: true}; > if (limit) { > opts.to = limit; > } > parse(fileData, opts, (err, rows) => fn(rows)); > }); > } > module.exports = parse_csv; > #+END_SRC > > So, I tangle that source block into a js file, and then I can use it from > other blocks, without needing sessions at all: > > #+BEGIN_SRC > const parser = require("/app/src/parser.js"); > const inputFile = './data/records.csv'; > parse_csv(inputFile, console.log); > #+END_SRC > > The only drawback is that you have to use absolute paths requiring the js > files, but since I am using docker containers to run node, I always mount > the current directory as a volume in /app inside the container, so that > works out fine. > I also think that this way forces me to separate the code in modular > blocks, which is already a good practice in itself. > With sessions you have to make sure to execute the blocks in the correct > order to build the "state" that your current block needs. > This way source blocks function as standalone units that can be run at any > time, I just run *org-babel-tangle* and then hit *C-c *inside the js > block. > > I hope that helps you. > > > Martin > --001a113b1a18633a6a0560f78f94 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Can you describe how do you do this in detailed?
Like:
> but since I am using docker containers to run node, I always mount t= he current directory as a volume in /app inside the container, so that work= s out fine.
> I also think that this way forces me to separate the code in modular= blocks, which is already a good practice in itself.
How to do this? About this: =20 > With sessions you have to make sure to execute the blocks in the cor= rect order to build the "state" that your current block needs. =20 I think have to use `noweb` reference here.
=20 (If you have a blog article describe this whole JS literate programming s= etup, that will be useful.)
Finally, thanks you provide this paradigm.

=
[stardiviner]=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 <Hack this world!>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 GPG = key ID: 47C32433
IRC(freeenode): stardiviner =C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 Twitter:=C2=A0 @numbchild
Key fingerprint =3D 9BAA 92BC CDD= D B9EF 3B36=C2=A0 CB99 B8C4 B8E5 47C3 2433
Blog: http://stardiviner.github.io/
<= /div>

On Sat, Dec 23, 2017 at 2:32 AM, Martin Alsi= net <martin@alsinet.com.ar> wrote:
Hello stardiviner,

On Fri, Dec 22, 2017 at 6:57 AM= stardiviner <n= umbchild@gmail.com> wrote:
<= br> I wish to do JavaScript Literate Programming in Org-mode.

So the :session header argument is very necessary.


I do Literate Programming in Javascript with Org-mod= e, and I have found a workaround using a combination of tangled files and r= equire.

I separate the logic in source code blocks= which then I tangle into js files and I require those js files in other so= urce blocks.

Example:=C2=A0

#+BEGIN_SRC js :tangle src/parser.js
const fs =3D require('fs');
const parse =3D require('csv-pa= rse')

function= columns(line){
=C2=A0 =C2=A0 return line.map(s =3D> s.toLow= erCase());
}
parse_csv =3D function(filename, fn, limit){
=C2=A0 =C2=A0 fs.readFile(filename, "= ;utf8", function (err, fileData) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 var opts =3D {columns: columns, trim: tr= ue};
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = if (limit) {
=C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 opts.to<= /a> =3D limit;
=C2=A0 =C2=A0 });

<= div>
So, I tangle that source block into a js file, and then I can use = it from other blocks, without needing sessions at all:
=
With sessions you have to make sure to execute the blocks in the corre= ct order to build the "state" that your current block needs.=C2= =A0
This way source blocks function as standalone units that can = be run at any time, I just run org-babel-tangle and then hit C-c = inside the js block.

I hope that helps you.


<= /div>
Martin

--001a113b1a18633a6a0560f78f94--