Skip to content

Commit

Permalink
Simplify the imports now that jupyter_server is a dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Feb 22, 2025
1 parent aea406f commit 7370eb5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 72 deletions.
7 changes: 1 addition & 6 deletions jupyterlab/jupyterlab_jupytext/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
"""Jupyter server and lab extension entry points"""

from jupytext.reraise import reraise

try:
from jupytext.contentsmanager import build_jupytext_contents_manager_class
except ImportError as err:
build_jupytext_contents_manager = reraise(err)
from jupytext.contentsmanager import build_jupytext_contents_manager_class


def load_jupyter_server_extension(app): # pragma: no cover
Expand Down
20 changes: 4 additions & 16 deletions src/jupytext/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
"""Read and write Jupyter notebooks as text files"""

from .contentsmanager import (
TextFileContentsManager,
build_jupytext_contents_manager_class,
)
from .formats import NOTEBOOK_EXTENSIONS, get_format_implementation, guess_format
from .jupytext import read, reads, write, writes
from .reraise import reraise
from .version import __version__

try:
from .contentsmanager import build_jupytext_contents_manager_class
except ImportError as err:
build_jupytext_contents_manager_class = reraise(err)

try:
from .contentsmanager import TextFileContentsManager
except ImportError as err:
TextFileContentsManager = reraise(err)

try:
from .contentsmanager import AsyncTextFileContentsManager
except ImportError as err:
AsyncTextFileContentsManager = reraise(err)

__all__ = [
"read",
"write",
Expand Down
57 changes: 7 additions & 50 deletions src/jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@
from datetime import timedelta

import nbformat
from jupyter_core.utils import ensure_async
from jupyter_server.services.contents.largefilemanager import AsyncLargeFileManager
from jupyter_server.utils import ensure_async
from tornado.web import HTTPError

# import notebook.transutils before notebook.services.contents.filemanager #75
try:
import notebook.transutils # noqa
except ImportError:
pass

from .config import (
JUPYTEXT_CONFIG_FILES,
PYPROJECT_FILE,
Expand Down Expand Up @@ -50,7 +45,10 @@


def build_jupytext_contents_manager_class(base_contents_manager_class):
"""Derives a TextFileContentsManager class from the given base class"""
"""
Derives an (async) TextFileContentsManager class from the given base class.
The base class can either be sync or async.
"""

class JupytextContentsManager(base_contents_manager_class, JupytextConfiguration):
"""
Expand Down Expand Up @@ -710,45 +708,4 @@ async def get_config(self, path, use_cache=False):
return JupytextContentsManager


try:
# The LargeFileManager is taken by default from jupyter_server if available
from jupyter_server.services.contents.largefilemanager import LargeFileManager

TextFileContentsManager = build_jupytext_contents_manager_class(LargeFileManager)
except ImportError:
# If we can't find jupyter_server then we take it from notebook
try:
from notebook.services.contents.largefilemanager import LargeFileManager

TextFileContentsManager = build_jupytext_contents_manager_class(
LargeFileManager
)
except ImportError:
# Older versions of notebook do not have the LargeFileManager #217
from notebook.services.contents.filemanager import FileContentsManager

TextFileContentsManager = build_jupytext_contents_manager_class(
FileContentsManager
)
try:
# The AsyncLargeFileManager is taken by default from jupyter_server if available
from jupyter_server.services.contents.largefilemanager import AsyncLargeFileManager

AsyncTextFileContentsManager = build_jupytext_contents_manager_class(
AsyncLargeFileManager
)
except ImportError:
# If we can't find jupyter_server then we take it from notebook
try:
from notebook.services.contents.largefilemanager import AsyncLargeFileManager

AsyncTextFileContentsManager = build_jupytext_contents_manager_class(
AsyncLargeFileManager
)
except ImportError:
# Older versions of notebook do not have the AsyncLargeFileManager #217
from notebook.services.contents.filemanager import AsyncFileContentsManager

AsyncTextFileContentsManager = build_jupytext_contents_manager_class(
AsyncFileContentsManager
)
TextFileContentsManager = build_jupytext_contents_manager_class(AsyncLargeFileManager)

0 comments on commit 7370eb5

Please sign in to comment.