/
__init__.py
95 lines (79 loc) · 3.02 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Read and write Jupyter notebooks as text files"""
from .jupytext import read, write, writes, reads
from .formats import NOTEBOOK_EXTENSIONS, guess_format, get_format_implementation
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 = reraise(err)
try:
from .contentsmanager import TextFileContentsManager
except ImportError as err:
TextFileContentsManager = reraise(err)
def load_jupyter_server_extension(app): # pragma: no cover
"""Use Jupytext's contents manager"""
if hasattr(app.contents_manager_class, "default_jupytext_formats"):
app.log.info(
"[Jupytext Server Extension] NotebookApp.contents_manager_class is "
"(a subclass of) jupytext.TextFileContentsManager already - OK"
)
return
# The server extension call is too late!
# The contents manager was set at NotebookApp.init_configurables
# Let's change the contents manager class
app.log.info(
"[Jupytext Server Extension] Deriving a JupytextContentsManager "
"from {}".format(app.contents_manager_class.__name__)
)
app.contents_manager_class = build_jupytext_contents_manager_class(
app.contents_manager_class
)
try:
# And rerun selected init steps from https://github.com/jupyter/notebook/blob/
# 132f27306522b32fa667a6b208034cb7a04025c9/notebook/notebookapp.py#L1634-L1638
# app.init_configurables()
app.contents_manager = app.contents_manager_class(parent=app, log=app.log)
app.session_manager.contents_manager = app.contents_manager
# app.init_components()
# app.init_webapp()
app.web_app.settings["contents_manager"] = app.contents_manager
# app.init_terminals()
# app.init_signal()
except Exception:
app.log.error(
"""[Jupytext Server Extension] An error occurred. Please deactivate the server extension with
jupyter serverextension disable jupytext
and configure the contents manager manually by adding
c.NotebookApp.contents_manager_class = "jupytext.TextFileContentsManager"
to your .jupyter/jupyter_notebook_config.py file.
"""
)
raise
def _jupyter_nbextension_paths(): # pragma: no cover
"""Allows commands like
jupyter nbextension install --py jupytext
jupyter nbextension enable --py jupytext
jupyter labextension install jupyterlab-jupytext"""
return [
dict(
section="notebook",
# the path is relative to the `jupytext` directory
src="nbextension",
# directory in the `nbextension/` namespace
dest="jupytext",
# _also_ in the `nbextension/` namespace
require="jupytext/index",
)
]
__all__ = [
"read",
"write",
"writes",
"reads",
"NOTEBOOK_EXTENSIONS",
"guess_format",
"get_format_implementation",
"TextFileContentsManager",
"__version__",
]