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

Is it possible to keep the standard behaviour when clicking on a Python script? #224

Closed
ghost opened this issue May 2, 2019 · 7 comments

Comments

@ghost
Copy link

ghost commented May 2, 2019

When clicking on a Python script in Jupyter Notebook, by default it opens as a notebook. It's still possible to open it with the Jupyter built-in text editor by selecting the script and clicking the Edit button. However is there a configuration settings that would preserve Jupyter default behaviour while still allowing users to pair notebooks with scripts if they wish to? That way Jupytext would be less intrusive and would require explicit opt-in from users. To give you some context, I introduced Jupytext on a multi-user JupyterHub deployment to allow some users to edit R Markdown documents as notebooks, but my Python users got surprised that their scripts where suddenly opening as notebooks and thought that it might be a bug.

@mwouts
Copy link
Owner

mwouts commented May 2, 2019

Hello @amarandon , thanks for your feedback. I understand that this comes as a surprise to your users! Actually, this question was initially raised at #183 and at that time we chose not to change the behavior. But we can have another look at this, certainly.

For now the option you have is to disable Jupytext globally with

jupyter serverextension disable jupytext

and ask each user who want Jupytext to either activate the server extension with either the command

jupyter serverextension enable jupytext --user

or by including the line

c.NotebookApp.contents_manager_class = "jupytext.TextFileContentsManager"

in his/her config file.

Would that approach solve the issue you encounter?

Otherwise, we could consider giving an option to classify only certain extensions as notebooks. Technically that would be rather easy - it is just a matter of changing the values returned by all_nb_extensions() in Jupytext's contents manager - but I first want to be sure that we have a good reason to do that!

@ghost
Copy link
Author

ghost commented May 3, 2019

Hi @mwouts Thank you for the quick reply! I'd rather not disable Jupytext globally. I tried to override all_nb_extensions in a subclass but apparently it's not called at all when I start Jupyter Notebook. However this seems to work for me:

from jupytext import TextFileContentsManager
TextFileContentsManager.nb_extensions.remove(".py")
c.NotebookApp.contents_manager_class = TextFileContentsManager

Not very pretty but it does the job for now.

It's true that I was kind of expecting a configuration setting that would allow to specify the list of extensions that should be opened as notebooks.

@mwouts
Copy link
Owner

mwouts commented May 3, 2019

Thank you for the quick reply!

You're welcome, that's an interesting question by the way!

I tried to override all_nb_extensions in a subclass but apparently it's not called at all when I start Jupyter Notebook

I see. The server extension for Jupytext, when active, replaces the contents manager with Jupytext's one in case the current CM is not already an instance or a derived class of jupytext.TextFileContentsManager (#218). If you want a derived class of jupytext.TextFileContentsManager as the contents manager, that must be done explicitly in Jupyter's config file (as you do above, but replace TextFileContentsManager with your derived class).

Not very pretty but it does the job for now. It's true that I was kind of expecting a configuration setting that would allow to specify the list of extensions that should be opened as notebooks.

I see what you mean, and as I mentioned earlier we could work on that. For now let me mention that I am little worried that if we do that, then collaborating on notebooks becomes a little more complex. As the links says, collaboration works in the following way: user A shares the notebook represented as a .py script. User B opens the script as a notebook, and the paired .ipynb file is re-created when he saves the notebook again. So, if B cannot open scripts as notebooks, then he will have to open a terminal and to use the CLI: jupytext --sync notebook.py to recreate the missing .ipynb file, which is probably less user friendly than directly opening the script as a notebook in Jupyter. Can you tell us what you and the users around you think about this point? Thanks!

@hoangthienan95
Copy link

Hello,
Sorry to hijack the thread, but along the same line, is there a way to make the icons for python files differ from the notebook files. For my place, opening the script as a notebook is fine but it's a bit confusing to have the files with the same icons and names. I usually have to look very closely at the files' extension to make sure that I'm editing the right file in jupyter lab. However, I'm also new to jupytext and maybe this is an intended feature? Like it should not matter which file I'm editing since they should be paired and closely synced with each other anyway?

@mwouts
Copy link
Owner

mwouts commented May 13, 2019

Hello @hoangthienan95, thanks for asking ! Well, for now you could use the approach that @amarandon documented above. I.e., modify Jupytext's content manager in your .jupyter_notebook_config.py as follow:

from jupytext import TextFileContentsManager
TextFileContentsManager.nb_extensions = ['.ipynb']
c.NotebookApp.contents_manager_class = TextFileContentsManager

That will restore Jupyter's default classification for text files, and hence their default icon in both Jupyter Lab and Jupyter notebook. The drawback is that you won't have the 'open with notebook' entry in Jupyter Lab's contextual menu any more.

Maybe a better solution, but more involved I am afraid, would be to restore the default icon in Jupyter Lab's JS interface itself, even if the file was classified as a potential notebook by Jupytext... Would you be interested in that? Maybe this PR (the contextual menu) can be of some help for this.

@mwouts
Copy link
Owner

mwouts commented May 15, 2019

Hello @amarandon , since there have been a few people asking for this, I have added the option for this in Jupytext 1.1.2. The option is documented at the end of this paragraph. Essentially you will have to include, say,

c.NotebookApp.contents_manager_class = "jupytext.TextFileContentsManager"
c.ContentsManager.notebook_extensions = "ipynb,Rmd"

in your jupyter configuration file.

@ghost
Copy link
Author

ghost commented May 16, 2019

@mwouts Fantastic! Thank you for letting me know!

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