Skip to content

Commit

Permalink
Jupytext CLI updates the timestamp of the input file
Browse files Browse the repository at this point in the history
To allow paired notebooks to be opened in Jupyter later on.
Closes #254 #335
  • Loading branch information
mwouts committed Oct 12, 2019
1 parent 6a67b4d commit 5988ac6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ def jupytext_single_file(nb_file, args, log):
write(notebook, alt_path, fmt=alt_fmt)
if args.pre_commit:
system('git', 'add', alt_path)
elif os.path.isfile(nb_file) and nb_dest.endswith('.ipynb') and not nb_file.endswith('.ipynb'):
# Update the original text file timestamp, as required by our Content Manager
# Otherwise Jupyter will refuse to open the paired notebook #335
log("[jupytext] Sync timestamp of '{}'".format(nb_file))
os.utime(nb_file)

return 0

Expand Down
29 changes: 29 additions & 0 deletions tests/test_contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tornado.web import HTTPError
from jupytext.compare import compare
import jupytext
from jupytext.cli import jupytext as jupytext_cli
from jupytext.jupytext import writes, write, read
from jupytext.compare import compare_notebooks
from jupytext.header import header_to_metadata_and_cell
Expand Down Expand Up @@ -430,6 +431,34 @@ def test_outdated_text_notebook(nb_file, tmpdir):
cm.get(tmp_nbpy)


@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_py')[:1])
def test_reload_notebook_after_jupytext_cli(nb_file, tmpdir):
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
tmp_nbpy = str(tmpdir.join('notebook.py'))

cm = jupytext.TextFileContentsManager()
cm.default_jupytext_formats = 'py,ipynb'
cm.outdated_text_notebook_margin = 0
cm.root_dir = str(tmpdir)

# write the paired notebook
nb = jupytext.read(nb_file)
cm.save(model=dict(type='notebook', content=nb), path='notebook.py')

assert os.path.isfile(tmp_ipynb)
assert os.path.isfile(tmp_nbpy)

# run jupytext CLI
jupytext_cli([tmp_nbpy, '--to', 'ipynb', '--update'])

# test reload
nb1 = cm.get('notebook.py')['content']
nb2 = cm.get('notebook.ipynb')['content']

compare_notebooks(nb1, nb)
compare_notebooks(nb2, nb)


@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('nb_file', list_notebooks('percent'))
def test_load_save_percent_format(nb_file, tmpdir):
Expand Down

0 comments on commit 5988ac6

Please sign in to comment.