Skip to content

Commit

Permalink
--set-formats argument on CLI
Browse files Browse the repository at this point in the history
Simpler to use than the --update-metadata method #141
  • Loading branch information
mwouts committed Jan 18, 2019
1 parent 6b20aa6 commit 8d90a28
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Release History
**Improvements**

- Jupytext command line has more arguments: ``--paired-paths`` to list the paths for the paired representations of the notebook, and ``--sync`` to synchronise the content of all paired paths based on the most recent file (#146). In addition, the ``--from`` argument is optional even when the notebook is read from stdin (#148).
- The pairing information, and more generally the notebook metadata can be edited with the CLL, see the ``--set-formats`` and the ``--update-metadata`` arguments (#141).
- Format specification allow prefix and suffix for path and file name (#138, #142). Use ``ipynb,prefix/suffix.py:percent`` to pair the current notebook named ``notebook.ipynb`` to a script named ``prefixnotebooksuffix.py``. Suffix and prefix can also be configured on the ``ipynb`` file, with the same syntax.
- Introducing a new ``hydrogen`` format for scripts, which derives from ``percent``. In that format Jupyter magic commands are not commented (#59, #126, #132).
- Python scripts or Markdown documents that have no Jupyter metadata receive a metadata filter that ensures that metadata is not exported back to the text representation (#124).
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ jupytext --to md --output - notebook.ipynb # display the markdown version o
jupytext --from ipynb --to py:percent # read ipynb from stdin and write double percent script on stdout
```

The `jupytext` command accepts many arguments. Execute `jupytext --help` to access the documentation. For instance, use the `--set-formats` and the `--update-metadata` arguments to edit the pairing information or more generally the notebook metadata.

Jupytext is also available as a Git pre-commit hook. Use this if you want Jupytext to create and update the `.py` (or `.md`...) representation of the staged `.ipynb` notebooks. All you need is to create an executable `.git/hooks/pre-commit` file with the following content:
```bash
#!/bin/sh
Expand Down
17 changes: 10 additions & 7 deletions jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,7 @@ def convert_notebook_files(nb_files, fmt, input_format=None, output=None, pre_co
continue

if update_metadata:
try:
updated_metadata = json.loads(update_metadata)
except ValueError as exception:
raise ValueError("Could not parse --update-metadata {}. JSONDecodeError: {}".
format(update_metadata, str(exception)))
recursive_update(notebook.metadata, updated_metadata)
recursive_update(notebook.metadata, update_metadata)

if comment_magics is not None:
notebook.metadata.setdefault('jupytext', {})['comment_magics'] = comment_magics
Expand Down Expand Up @@ -271,8 +266,13 @@ def cli_jupytext(args=None):
nargs='?',
default=None,
help='Should Jupyter magic commands be commented? (Y)es/(T)rue/(N)o/(F)alse/(D)efault')
parser.add_argument('--set-formats',
type=str,
help='Set jupytext.formats metadata to the given value. Use this to activate pairing on a '
'notebook, with e.g. --set-formats ipynb,py:light')
parser.add_argument('--update-metadata',
default=None,
default={},
type=json.loads,
help='Update the notebook metadata with the desired dictionary. Argument must be given in JSON '
'format. For instance, if you want to activate a pairing in the generated file, '
"""use e.g. '{"jupytext":{"formats":"ipynb,py:light"}}'""")
Expand All @@ -292,6 +292,9 @@ def cli_jupytext(args=None):
if args.version:
return args

if args.set_formats:
args.update_metadata = recursive_update(args.update_metadata, {'jupytext': {'formats': args.set_formats}})

if args.input_format:
args.input_format = canonize_format(args.input_format)
if not args.notebooks and not args.output:
Expand Down
15 changes: 14 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,19 @@ def hook():
assert not os.path.isfile(tmp_py)


@pytest.mark.parametrize('py_file', list_notebooks('python'))
def test_set_formats(py_file, tmpdir):
tmp_py = str(tmpdir.join('notebook.py'))
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))

copyfile(py_file, tmp_py)

jupytext(['--to', 'ipynb', tmp_py, '--set-formats', 'ipynb,py:light'])

nb = readf(tmp_ipynb)
assert nb.metadata['jupytext']['formats'] == 'ipynb,py:light'


@pytest.mark.parametrize('py_file', list_notebooks('python'))
def test_update_metadata(py_file, tmpdir, capsys):
tmp_py = str(tmpdir.join('notebook.py'))
Expand All @@ -388,7 +401,7 @@ def test_update_metadata(py_file, tmpdir, capsys):
jupytext(['--to', 'ipynb', tmp_py, '--update-metadata', '{"incorrect": "JSON"'])

out, err = capsys.readouterr()
assert 'JSONDecodeError' in err
assert 'invalid' in err


@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_py'))
Expand Down

0 comments on commit 8d90a28

Please sign in to comment.