Jupytext with JupyterLab Desktop in conda environment

I’m new to JupyterLab and would like to set up Jupytext.

I’m on MacOS BigSur and downloaded the executable for the JupyterLab desktop app. I want to work in one particular environment. I have managed to create a kernel inside the environment, which I can select from the notebook (top-right corner) and all seems well (the notebook runs using libraries that are only installed in that environment). But I can’t seem to make Jupytext work. Here’s what I’ve tried:

  1. Here’s how I got JupyterLab desktop to run a notebook in environment.
     $ conda activate j
     (j)$ conda install ipykernel
     (j)$ ipython kernel install --user --name=myJupyterLabKernel
     (j)$ conda deactivate

After restarting Jupyterlab, the new kernel was available. But the notebook wasn’t running. However, the following seemed to have solved any issues:

    (j)$ conda install nb_conda

To add Jupytext, I followed the instructions:

    $ conda activate j
    (j)$ conda install jupytext -c conda-forge

The Jupytext extension wasn’t present in the menu, so I tried this:

    (j)$ jupyter nbextension install --py jupytext
    (j)$ jupyter nbextension enable --py jupytext

So that’s where I’m stuck.

If I right-click on a .py file and select “open with” the only choice I get is “editor”, evidence that Jupytext is not available. Suggestions?

Screenshot:

Thanks!

P.S. A similar question was asked, but with no applicable solution.

The desktop app version didn’t fully support all extensions unless prebuilt and installabe via pip or conad, see here. Jupytext should qualify for that.

However, jupyter nbextension install - isn’t going to do anything for your desktop JupyterLab. That is for the classic notebook interface running in a browser. Not JupyterLab and not the desktop app.

That line conda install jupytext -c conda-forge should probably have worked though if installing to the right place.

If you try running !jupytext --help in a notebook, does that seem to do anything? That would indicate if some aspects of jupytext installed because you should see the man page.

1 Like

What happens if you right-click on the python file and choose “open-with → notebook”? With my install that opens the file as a notebook in a new tab. Once the file is open, the pairing options are avalilable in the command palette as described in Installation — Jupytext documentation. There’s work in progress to improve this here: Enhance Jupytext for JupyterLab by fcollonval · Pull Request #877 · mwouts/jupytext · GitHub

2 Likes

Thanks @fomightez and @phaustin.

I tried to reinstall Jupyter with/without the desktop app, after cleaning all the Jupyter environment variables I could find (inside Library and .jupyter in particular) and now nothing works at all (cannot run any code without “cannot generate requests” or “kernel cannot be started” or complaints about the ipykernel, which I have installed in the environment). It’s too much of a mess at this point: I’m going to destroy the environment and try again one last time. UPDATE: Jupyter is working! JupyterLab desktop is working! It’s able to find my environment and use the dedicated kernel!

background: I probably didn’t need Jupytext after all, since I do not plan to edit notebooks: my use case is to generate notebooks from markdown. My interest in the JupyterLab desktop app was because I do not want the notebooks to open in browsers. I had been using nteract up to this point, which is rather limiting, and the desktop app looked very promising. I’ll report back if I have any success in repairing jupyter. Thanks for your help!

That’s sort of why I was hoping at least !jupytext --help worked. If it did you could just use the command line, see here. It doesn’t need to show up in the menu for that route. The command line commands will work in the notebook if you put exclamation marks before them.

And if !jupytext --help doesn’t work now, you can just run in the notebook %pip install jupytext and then it will. Or at least it most likely will.

Note the use of the %pip install in the notebook, see here about the use of the newer pip and conda magics being the recommended way to install with pip and conda in the notebook so that you know it installs to same environment the notebook is running.

1 Like

thanks @fomightez ! I’m going to read up on this. At this point I’m happy to have regained the ability to run Jupyter notebooks without a browser and in a dedicated environment!

I’m not sure I did it right to be honest, because I had to install jupyterlab from the command line to be able to use jupyterlab desktop… For the record, here were the steps:

# Steps to install jupyter notebook in a clean environment
conda env remove -n vs
conda create -n vs python=3.9
conda activate vs
conda install numpy scipy pandas matplotlib statsmodels seaborn

# In "Set Python Environment", I set "/Users/ptoche/miniconda3/envs/vs/bin/python3.9", but Jupyterlab desktop complains it cannot find jupyterlab inside my environment, so I install it:
conda install -c conda-forge jupyterlab

# VS Code triggered the following:
conda install -c conda-forge --name vs --update-deps --force-reinstall ipykernel -y

# jupyterlab was not installed successfully, maybe an issue with the ipykernel above
jupyter --version
Selected Jupyter core packages...
IPython          : 7.30.0
ipykernel        : 6.5.0
ipywidgets       : not installed
jupyter_client   : 7.1.0
jupyter_core     : 4.9.1
jupyter_server   : not installed
jupyterlab       : not installed
nbclient         : not installed
nbconvert        : not installed
nbformat         : not installed
notebook         : not installed
qtconsole        : not installed
traitlets        : 5.1.1

# installing jupyterlab again:
conda install -c conda-forge jupyterlab

# At this point I'm able to run a Jupyter notebook inside my environment with the Jupyterlab desktop app. 

Next step will be to figure out Jupytext!

1 Like

For a notebook file .ipynb, I get three options: “Notebook”, “Editor”, and “JSON”. But for a python file .py, I only get one option: “Editor”. As @fomightez explained, I shouldn’t expect Jupytext to work with JupyterLab Desktop out of the box. So it seems I’ve essentially got as far as I could hope to get.

Running

!jupytext --help

from a notebook worked, a good sign! I’ll play around and see if I can get a few steps further. Thanks to both of you for your suggestions!

1 Like

.ipynb is a text file in json format (containing the source code in python or other language, the markdown and the output from execution). Thus there are three ways to open/view/edit it:
a. treat it like a text file (use say or other text editor to edit it),
b. treat it like json file, and use a json editor to edit it).
c. treat it like a jlab notebook and use jupyterlab to use/edit it.

For .py, you can view/edit it as text file (vi), or use an IDE to use/edit it.

One of the use of jupytext is to generate the python code from the .ipynb, and there is another way to do it: $ jupyter nbconvert jupyter nbconvert --to script mynotebook.ipynb
You can also generate a pdf, html, etc. Here is a list supported: [‘asciidoc’, ‘custom’, ‘html’, ‘latex’, ‘markdown’, ‘notebook’, ‘pdf’, ‘python’, ‘rst’, ‘script’, ‘slides’, ‘webpdf’].

You’ll need to conda install or pip install nbconvert. Some output such as latex may also require additional packages.

1 Like

Thanks hikemtshasta. I was interested in Jupytext because I want to work directly with the markdown source (I do not want to edit the Jupyter notebook directly). As you suggest, nbconvert is an interesting option for converting to other formats.

1 Like

Yup jupytext is a good approach for creating notebooks from markdown. It should work in JupyterLab Desktop. To troubleshoot in your case:

  • ensure you are running latest version of JupyterLab Desktop
  • ensure that the conda environment into which it was installed is the same as the python environment selected with the environment selector on the JupyterLab Desktop statusbar
  • from the conda environment (not from notebook! don’t use ! here it may not give you the correct environment and you may get misleading result!) run jupyter labextension list and check whether jupytext is listed there (and whether as coming from a Python package or not)
2 Likes