From mboxrd@z Thu Jan 1 00:00:00 1970 From: Martin Alsinet Subject: Re: Hope ob-js can support :session feature Date: Fri, 22 Dec 2017 18:32:07 +0000 Message-ID: References: <0a1fd382-6c99-e69d-c998-b3157edab040@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="001a113a1efe51cf050560f2092d" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:42141) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eSS7K-0006fr-RK for emacs-orgmode@gnu.org; Fri, 22 Dec 2017 13:32:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eSS7H-0001UL-LH for emacs-orgmode@gnu.org; Fri, 22 Dec 2017 13:32:22 -0500 Received: from mail-oi0-f50.google.com ([209.85.218.50]:35144) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eSS7H-0001Sr-Dp for emacs-orgmode@gnu.org; Fri, 22 Dec 2017 13:32:19 -0500 Received: by mail-oi0-f50.google.com with SMTP id 184so19507294oii.2 for ; Fri, 22 Dec 2017 10:32:19 -0800 (PST) In-Reply-To: <0a1fd382-6c99-e69d-c998-b3157edab040@gmail.com> 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: stardiviner Cc: emacs-orgmode --001a113a1efe51cf050560f2092d Content-Type: text/plain; charset="UTF-8" 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 --001a113a1efe51cf050560f2092d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello stardiviner,

On Fri, Dec 22, 2017 at 6:57 AM stardiviner <numbchild@gmail.com> 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 bl= ocks.

Example:=C2=A0

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

function column= s(line){
=C2=A0 =C2=A0 return line.map(s =3D> s.toLowerCase(= ));
}
parse_csv =3D function(filename, fn, limit){
=C2=A0 =C2=A0 fs.readFile(filename, "utf8&q= uot;, function (err, fileData) {
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 var opts =3D {columns: columns, trim: true};
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (lim= it) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 opts.to =3D limit;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
<= div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 parse(fileData, op= ts, (err, rows) =3D> fn(rows));
=C2=A0 =C2=A0 });
}
module.exports =3D parse_csv;
#+END_SRC

So, I tangle that so= urce block into a js file, and then I can use it from other blocks, without= needing sessions at all:

#+BEGIN_SRC
const parser =3D require("/app/src/parser.js");<= /font>
const inputFile =3D './data/r= ecords.csv';
parse_csv(inputF= ile, console.log);
#+END_SRC
<= /font>

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 v= olume in /app inside the container, so that works out fine.
I als= o think that this way forces me to separate the code in modular blocks, whi= ch is already a good practice in itself.
With sessions you have t= o make sure to execute the blocks in the correct order to build the "s= tate" that your current block needs.=C2=A0
This way source b= locks 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.
<= div>
I hope that helps you.


Martin
--001a113a1efe51cf050560f2092d--