Reload opens a second tab in the Editor for a file open in the Jupytext Notebook view
Description
On a page reload, a file that was open in the Jupytext Notebook view of jupytext opens again in the Editor, in a second tab. #18034 fixed this in 4.5.0 for views such as a notebook open in the Editor. It does not cover the Jupytext Notebook view (see Possible solution).
Reproduce
Install
jupyterlab==4.6.4andjupytext==1.19.5.Save this file as
notebook.py:# --- # jupyter: # kernelspec: # display_name: Python 3 (ipykernel) # language: python # name: python3 # --- # %% print("hello")In the file browser, right-click
notebook.pyand select Open With → Jupytext Notebook. The page URL changes to/lab/tree/notebook.py.Reload the page.
A second notebook.py tab shows the file in the Editor, and this tab is active. Later reloads restore both tabs.
With jupyter-collaboration==5.0.4 also installed, the reload opens the warning that jupytext/jupytext#1449 reports: "The file notebook.py has been opened with two different views. This is not supported. Please close this view; otherwise, some of your edits may not be saved properly."
--
This in general affects any rendered or notebook, not only jupytext.
Expected behavior
The Jupytext Notebook tab alone, as before the reload.
Possible solution
The notebook tracker restores every panel with the Notebook factory:
jupytext adds its panels to this tracker (factory.ts in jupytext 1.19.5):
// Add the notebook panel to the tracker.
// @ts-expect-error Trick using private API
void notebookTracker.add(widget);On load, filebrowser:open-path opens the file of the URL with docmanager:open and no factory. Since #18034, that command reveals an open view only when the factory of the view is in preferredWidgetFactories(path):
For notebook.py that list is Editor and Jupytext Notebook. The restored Notebook view is not found, and the file opens in the Editor, its default view. Either change prevents the second tab: the tracker restores each panel with the factory that created it, or the lookup accepts a view of any factory that is open for the path.
Context
- Operating System and version: Linux
- Browser and version: Chromium 149
- JupyterLab version: 4.6.4 and 4.7.0a2, with jupytext 1.19.5
Source: jupyterlab/jupyterlab