Repository: mwouts/jupytext (python)

Issue:
Jupytext --execute fails in subfolder
If I test/execute a notebook in a subfolder, then the notebook can't read files located in the same subfolder - it seems that Jupytext executes the notebook from its current directory instead.

Pull request description:
Execute notebooks in their folder
Closes #382

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9febf36d..d6612e15 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -35,9 +35,9 @@
 - `jupytext --test textfile.ext` now really compares the text file to its round trip (rather than the corresponding notebook) (#339)
 - Markdown cells that contain code are now preserved in a round trip through the Markdown and R Markdown formats (#361)
 - Code cells with a `%%python3` cell magic are now preserved in a round trip through the Markdown format (#365)
+- `jupytext --execute` runs the notebook in its folder (#382)
 - Strings in the metadata of code cells are quoted in the Rmd representation. And we escape R code in chunk options with `#R_CODE#` (#383)
 
-
 1.2.4 (2019-09-19)
 ------------------
 
diff --git a/jupytext/cli.py b/jupytext/cli.py
index 99973708..d90282b1 100644
--- a/jupytext/cli.py
+++ b/jupytext/cli.py
@@ -384,7 +384,13 @@ def jupytext_single_file(nb_file, args, log):
         kernel_name = notebook.metadata.get('kernelspec', {}).get('name')
         log("[jupytext] Executing notebook with kernel {}".format(kernel_name))
         exec_proc = ExecutePreprocessor(timeout=None, kernel_name=kernel_name)
-        exec_proc.preprocess(notebook, resources={})
+        if nb_dest is not None and nb_dest != '-':
+            resources = {'metadata': {'path': str(os.path.dirname(nb_dest))}}
+        elif nb_file != '-':
+            resources = {'metadata': {'path': str(os.path.dirname(nb_file))}}
+        else:
+            resources = {}
+        exec_proc.preprocess(notebook, resources=resources)
 
     # III. ### Possible actions ###
     modified = args.update_metadata or args.pipe or args.execute


Test patch:
diff --git a/tests/test_execute.py b/tests/test_execute.py
index 3c972af4..6a9c0b98 100644
--- a/tests/test_execute.py
+++ b/tests/test_execute.py
@@ -121,3 +121,30 @@ def test_execute_r(tmpdir):  # pragma: no cover
     nb = read(tmp_ipynb)
     assert len(nb.cells) == 1
     assert nb.cells[0].outputs[0]['data']['text/markdown'] == '6'
+
+
+@requires_nbconvert
+def test_execute_in_subfolder(tmpdir):
+    tmpdir.mkdir('subfolder')
+
+    tmp_csv = str(tmpdir.join('subfolder', 'inputs.csv'))
+    tmp_py = str(tmpdir.join('subfolder', 'notebook.py'))
+    tmp_ipynb = str(tmpdir.join('subfolder', 'notebook.ipynb'))
+
+    with open(tmp_csv, 'w') as fp:
+        fp.write("1\n2\n")
+
+    with open(tmp_py, 'w') as fp:
+        fp.write("""import ast
+
+with open('inputs.csv') as fp:
+    text = fp.read()
+
+sum(ast.literal_eval(line) for line in text.splitlines())
+""")
+
+    jupytext(args=[tmp_py, '--to', 'ipynb', '--execute'])
+
+    nb = read(tmp_ipynb)
+    assert len(nb.cells) == 3
+    assert nb.cells[2].outputs[0]['data'] == {'text/plain': '3'}