mwouts / jupytext

Jupyter Notebooks as Markdown Documents, Julia, Python or R scripts
https://jupytext.readthedocs.io
MIT License
6.65k stars 386 forks source link

Pairing expands collapsed cells in `jupyterlab` #1040

Closed st-bender closed 1 year ago

st-bender commented 1 year ago

Hi, Jupyterlab allows collapsing (sub)sections of a notebook by clicking a little arrow beside the heading markdown cells. This helps to keep a bit of order in long notebooks, and the collapsing persists when saving and reloading the notebook. However, when pairing the notebook with a python script (e.g. for version control), this collapsing metadata is ignored and the cells are expanded when (re)loading the notebook in jupyterlab. I found that re-collapsing the same cells and saving the paired notebook adds more jp-MarkdownHeadingCollapsed=true entries to the markdown metadata, in the python "percent" script:

# %% [markdown] tags=[] jp-MarkdownHeadingCollapsed=true jp-MarkdownHeadingCollapsed=true tags=[]
# ## Data

and in the notebook that shows up as:

  {
   "cell_type": "markdown",
   "id": "e9070a6f-0cc1-4bc9-ba38-15c94fa334e1",
   "metadata": {
    "incorrectly_encoded_metadata": "tags=[] jp-MarkdownHeadingCollapsed=true",
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "## Data"
   ]
  },

Note the "incorrectly_encoded_metadata" entry and despite having the "jp-MarkdownHeadingCollapsed": true, entry, the cells are not collapsed.

Saving the notebook without jupytexts pairing information restores the collapsing behaviour.

mwouts commented 1 year ago

Hello @st-bender , thank you for reporting this! I think the regular expression that we use to parse the metadata key does not accept - at the moment. That should be easy to fix. Stay tuned!

mwouts commented 1 year ago

Hi @st-bender , this should be fixed in the development version. Would you mind to give it a try? You can install it with

BUILD_JUPYTERLAB_EXTENSION=1 pip install git+https://github.com/mwouts/jupytext.git
st-bender commented 1 year ago

Hi @mwouts that was quick, thanks a lot. Yes, it works with the dev version.