Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leading comments for interpreter and encoding in scripts are not preserved #241

Closed
matteoipri opened this issue Jun 4, 2019 · 4 comments
Milestone

Comments

@matteoipri
Copy link

Hi,
I have a repository with Python scripts that I want to run as notebooks in JupyterLab.

My workflow with jupytext is the following:

  1. pull the changes from the repository;
  2. right-click on a script in the file browser and select "Open with Notebook";
  3. run and edit the notebook/script and save;
  4. commit the changed script in py:light format to the repository.

All my scripts begin with the following two lines:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

The Notebooks are opened exactly as I want them by jupytext, i.e. the cells are split at the points I want and those two lines are not present in the notebook.
Anyway, when I save my changes in the notebook those two lines are deleted from the python script, while I would like to still have them in the scripts.

Why are those two lines not preserved?
Is there a way to have them preserved?

Thanks

PS: this extension is awesome!

@mwouts
Copy link
Owner

mwouts commented Jun 4, 2019

Hello @matteoipri , thanks for reporting this.

I would have expected the two lines to be preserved by Jupytext, cf. this test.

Also, if I open in jupyter notebook a script test.py with the following content

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
a + 1

then I get a notebook with the following "jupytext" metadata (Edit / Edit notebook metadata):

"jupytext": {
    "executable": "/usr/bin/env python3",
    "encoding": "# -*- coding: utf-8 -*-",
    "notebook_metadata_filter": "-all",
    "cell_metadata_filter": "-all",
    "main_language": "python",
    "text_representation": {
      "extension": ".py",
      "format_name": "light"
    }
  },

Can you compare to what you get? Do you have any custom configuration option set on Jupytext?

@matteoipri
Copy link
Author

Hi @mwouts, thanks for your reply. It triggered some more tests on my side.

I confirm that the test you suggest works as expected. The error is that I did not disclose my full test script. In order to avoid the pop-up asking for which kernel to use with the notebook, I add the YAML header to my scripts, like in the following:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.4'
#       jupytext_version: 1.1.2
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

a = 2

a + 1

This script, once opened as notebook and saved again looks like this:

# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.4'
#       jupytext_version: 1.1.2
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

a = 2

a + 1


I also just noticed that I am running two outdated versions: JupyterLab 0.35.5 (instead of 0.35.6) and jupytext 1.1.2 (instead of 1.1.3).
Can this be the root cause? I will upgrade as soon as possible and test again.

Thanks

@mwouts
Copy link
Owner

mwouts commented Jun 4, 2019

I see! Thanks for the additional details - I'll add one more test to be sure this gets fixed.
No need to upgrade now, you can wait for 1.1.4 instead!

@mwouts mwouts added this to the 1.1.4 milestone Jun 4, 2019
@matteoipri
Copy link
Author

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants