Skip to content

Commit 402aeca

Browse files
authored
refactor: reformat the lib according to the new pre-commits
1 parent 51738b1 commit 402aeca

11 files changed

+109
-144
lines changed

jupyter_sphinx/__init__.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
from .execute import ExecuteJupyterCells, JupyterKernel
2929
from .thebelab import ThebeButton, ThebeButtonNode, ThebeOutputNode, ThebeSourceNode
3030

31-
REQUIRE_URL_DEFAULT = (
32-
"https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"
33-
)
31+
REQUIRE_URL_DEFAULT = "https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"
3432
THEBELAB_URL_DEFAULT = "https://unpkg.com/thebelab@^0.4.0"
3533

3634
logger = logging.getLogger(__name__)
@@ -92,23 +90,20 @@ def visit_thebe_source(self, node):
9290

9391
# Sphinx callback functions
9492
def builder_inited(app):
95-
"""
93+
"""Init the build.
94+
9695
2 cases
9796
case 1: ipywidgets 7, with require
98-
case 2: ipywidgets 7, no require
97+
case 2: ipywidgets 7, no require.
9998
"""
10099
require_url = app.config.jupyter_sphinx_require_url
101100
if require_url:
102101
app.add_js_file(require_url)
103102
embed_url = (
104-
app.config.jupyter_sphinx_embed_url
105-
or ipywidgets.embed.DEFAULT_EMBED_REQUIREJS_URL
103+
app.config.jupyter_sphinx_embed_url or ipywidgets.embed.DEFAULT_EMBED_REQUIREJS_URL
106104
)
107105
else:
108-
embed_url = (
109-
app.config.jupyter_sphinx_embed_url
110-
or ipywidgets.embed.DEFAULT_EMBED_SCRIPT_URL
111-
)
106+
embed_url = app.config.jupyter_sphinx_embed_url or ipywidgets.embed.DEFAULT_EMBED_SCRIPT_URL
112107
if embed_url:
113108
app.add_js_file(embed_url)
114109

jupyter_sphinx/_version.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
"""
2-
store the current version info of the project.
3-
4-
"""
1+
"""store the current version info of the project."""
52
import re
63
from typing import List
74

jupyter_sphinx/ast.py

+11-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Manipulating the Sphinx AST with Jupyter objects"""
1+
"""Manipulating the Sphinx AST with Jupyter objects."""
22

33
import json
44
import warnings
@@ -34,9 +34,7 @@ def load_content(cell, location, logger):
3434
env.note_dependency(rel_filename)
3535
if cell.content:
3636
logger.warning(
37-
'Ignoring inline code in Jupyter cell included from "{}"'.format(
38-
rel_filename
39-
),
37+
'Ignoring inline code in Jupyter cell included from "{}"'.format(rel_filename),
4038
location=location,
4139
)
4240
try:
@@ -60,9 +58,7 @@ def get_highlights(cell, content, location, logger):
6058
hl_lines = parselinenos(emphasize_linespec, nlines)
6159
if any(i >= nlines for i in hl_lines):
6260
logger.warning(
63-
"Line number spec is out of range(1-{}): {}".format(
64-
nlines, emphasize_linespec
65-
),
61+
"Line number spec is out of range(1-{}): {}".format(nlines, emphasize_linespec),
6662
location=location,
6763
)
6864
hl_lines = [i + 1 for i in hl_lines if i < nlines]
@@ -78,7 +74,7 @@ class JupyterCell(Directive):
7874
executed when the directive is parsed, but later during a doctree
7975
transformation.
8076
81-
Arguments
77+
Arguments:
8278
---------
8379
filename : str (optional)
8480
If provided, a path to a file containing code.
@@ -166,7 +162,7 @@ def run(self):
166162
class CellInput(Directive):
167163
"""Define a code cell to be included verbatim but not executed.
168164
169-
Arguments
165+
Arguments:
170166
---------
171167
filename : str (optional)
172168
If provided, a path to a file containing code.
@@ -237,7 +233,7 @@ def run(self):
237233
class CellOutput(Directive):
238234
"""Define an output cell to be included verbatim.
239235
240-
Arguments
236+
Arguments:
241237
---------
242238
filename : str (optional)
243239
If provided, a path to a file containing output.
@@ -323,12 +319,10 @@ def __init__(self, rawsource="", *children, **attributes):
323319
super().__init__("", *children, mimetypes=attributes["mimetypes"])
324320

325321
def render_as(self, visitor):
326-
"""Determine which node to show based on the visitor"""
322+
"""Determine which node to show based on the visitor."""
327323
try:
328324
# Or should we go to config via the node?
329-
priority = visitor.builder.env.app.config[
330-
"render_priority_" + visitor.builder.format
331-
]
325+
priority = visitor.builder.env.app.config["render_priority_" + visitor.builder.format]
332326
except (AttributeError, KeyError):
333327
# Not sure what do to, act as a container and show everything just in case.
334328
return super()
@@ -367,9 +361,7 @@ def __init__(self, rawsource="", *children, **attributes):
367361
super().__init__("", view_spec=attributes["view_spec"])
368362

369363
def html(self):
370-
return ipywidgets.embed.widget_view_template.format(
371-
view_spec=json.dumps(self["view_spec"])
372-
)
364+
return ipywidgets.embed.widget_view_template.format(view_spec=json.dumps(self["view_spec"]))
373365

374366

375367
class JupyterWidgetStateNode(docutils.nodes.Element):
@@ -414,7 +406,7 @@ def cell_output_to_nodes(outputs, write_stderr, out_dir, thebe_config, inline=Fa
414406
inline: False
415407
Whether the nodes will be placed in-line with the text.
416408
417-
Returns
409+
Returns:
418410
-------
419411
to_add : list of docutils nodes
420412
Each output, converted into a docutils node.
@@ -509,9 +501,7 @@ def output2sphinx(data, mime_type, metadata, out_dir, inline=False):
509501
math_node = math_block
510502

511503
if mime_type == "text/html":
512-
return docutils.nodes.raw(
513-
text=data, format="html", classes=["output", "text_html"]
514-
)
504+
return docutils.nodes.raw(text=data, format="html", classes=["output", "text_html"])
515505
elif mime_type == "text/plain":
516506
return literal_node(
517507
text=data,

jupyter_sphinx/css/jupyter-sphinx.css

+25-26
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ After a build, this stylesheet is loaded from ./_static/jupyter-sphinx.css .
2323
2424
*/
2525

26-
2726
div.jupyter_container {
28-
padding: .4em;
29-
margin: 0 0 .4em 0;
30-
background-color: #FFFF;
31-
border: 1px solid #CCC;
32-
-moz-box-shadow: 2px 2px 4px rgba(87, 87, 87, 0.2);
33-
-webkit-box-shadow: 2px 2px 4px rgba(87, 87, 87, 0.2);
34-
box-shadow: 2px 2px 4px rgba(87, 87, 87, 0.2);
27+
padding: 0.4em;
28+
margin: 0 0 0.4em 0;
29+
background-color: #ffff;
30+
border: 1px solid #ccc;
31+
-moz-box-shadow: 2px 2px 4px rgba(87, 87, 87, 0.2);
32+
-webkit-box-shadow: 2px 2px 4px rgba(87, 87, 87, 0.2);
33+
box-shadow: 2px 2px 4px rgba(87, 87, 87, 0.2);
3534
}
3635
.jupyter_container div.code_cell {
3736
border: 1px solid #cfcfcf;
@@ -59,8 +58,8 @@ div.jupyter_container div.highlight {
5958
background-color: #f7f7f7; /* for haiku */
6059
}
6160
div.jupyter_container {
62-
padding: 0;
63-
margin: 0;
61+
padding: 0;
62+
margin: 0;
6463
}
6564

6665
/* Prevent alabaster breaking highlight alignment */
@@ -70,10 +69,10 @@ div.jupyter_container .hll {
7069
}
7170

7271
/* overrides for sphinx_rtd_theme */
73-
.rst-content .jupyter_container div[class^='highlight'],
74-
.document .jupyter_container div[class^='highlight'],
72+
.rst-content .jupyter_container div[class^="highlight"],
73+
.document .jupyter_container div[class^="highlight"],
7574
.rst-content .jupyter_container pre.literal-block {
76-
border:none;
75+
border: none;
7776
margin: 0;
7877
padding: 0;
7978
background: none;
@@ -86,21 +85,21 @@ div.jupyter_container .hll {
8685
text-align: center;
8786
}
8887
.jupyter_container .stderr {
89-
background-color: #FCC;
90-
border: none;
91-
padding: 3px;
88+
background-color: #fcc;
89+
border: none;
90+
padding: 3px;
9291
}
9392
.jupyter_container .output {
94-
border: none;
93+
border: none;
9594
}
9695
.jupyter_container div.output pre {
97-
background-color: white;
98-
background: none;
99-
padding: 4px;
100-
border: none;
101-
box-shadow: none;
102-
-webkit-box-shadow: none; /* for nature */
103-
-moz-box-shadow: none; /* for nature */
96+
background-color: white;
97+
background: none;
98+
padding: 4px;
99+
border: none;
100+
box-shadow: none;
101+
-webkit-box-shadow: none; /* for nature */
102+
-moz-box-shadow: none; /* for nature */
104103
}
105104
.jupyter_container .code_cell td.linenos {
106105
text-align: right;
@@ -114,10 +113,10 @@ div.jupyter_container .hll {
114113
/* combine sequential jupyter cells,
115114
by moving sequential ones up higher on y-axis */
116115
div.jupyter_container + div.jupyter_container {
117-
margin: -.5em 0 .4em 0;
116+
margin: -0.5em 0 0.4em 0;
118117
}
119118

120119
/* Fix for sphinx_rtd_theme spacing after jupyter_container #91 */
121120
.rst-content .jupyter_container {
122-
margin: 0 0 24px 0;
121+
margin: 0 0 24px 0;
123122
}

jupyter_sphinx/execute.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def LoggerAdapterWrapper(logger_adapter):
6565
class JupyterKernel(Directive):
6666
"""Specify a new Jupyter Kernel.
6767
68-
Arguments
68+
Arguments:
6969
---------
7070
kernel_name : str (optional)
7171
The name of the kernel in which to execute future Jupyter cells, as
@@ -166,16 +166,10 @@ def apply(self):
166166

167167
# Raise error if cells raised exceptions and were not marked as doing so
168168
for node, cell in zip(nodes, notebook.cells):
169-
errors = [
170-
output
171-
for output in cell.outputs
172-
if output["output_type"] == "error"
173-
]
169+
errors = [output for output in cell.outputs if output["output_type"] == "error"]
174170
allowed_errors = node.attributes.get("raises") or []
175171
raises_provided = node.attributes["raises"] is not None
176-
if (
177-
raises_provided and not allowed_errors
178-
): # empty 'raises': suppress all errors
172+
if raises_provided and not allowed_errors: # empty 'raises': suppress all errors
179173
pass
180174
elif errors and not any(e["ename"] in allowed_errors for e in errors):
181175
raise ExtensionError(
@@ -192,9 +186,7 @@ def apply(self):
192186
if output["output_type"] == "stream" and output["name"] == "stderr"
193187
]
194188
if stderr and not node.attributes["stderr"]:
195-
js.logger.warning(
196-
"Cell printed to stderr:\n{}".format(stderr[0]["text"])
197-
)
189+
js.logger.warning("Cell printed to stderr:\n{}".format(stderr[0]["text"]))
198190

199191
# Insert input/output into placeholders for non-executed cells
200192
for node, cell in zip(nodes, notebook.cells):
@@ -224,9 +216,7 @@ def apply(self):
224216
# The literal_block node with the source
225217
source = node.children[0].children[0]
226218
nlines = source.rawsource.count("\n") + 1
227-
show_numbering = (
228-
linenos_config or source["linenos"] or source["linenostart"]
229-
)
219+
show_numbering = linenos_config or source["linenos"] or source["linenostart"]
230220

231221
if show_numbering:
232222
source["linenos"] = True
@@ -251,9 +241,7 @@ def apply(self):
251241
# Write certain cell outputs (e.g. images) to separate files, and
252242
# modify the metadata of the associated cells in 'notebook' to
253243
# include the path to the output file.
254-
write_notebook_output(
255-
notebook, str(output_dir), file_name, self.env.docname
256-
)
244+
write_notebook_output(notebook, str(output_dir), file_name, self.env.docname)
257245

258246
try:
259247
cm_language = notebook.metadata.language_info.codemirror_mode.name

jupyter_sphinx/thebelab.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
class ThebeSourceNode(docutils.nodes.container):
12-
"""Container that holds the cell source when thebelab is enabled"""
12+
"""Container that holds the cell source when thebelab is enabled."""
1313

1414
def __init__(self, rawsource="", *children, **attributes):
1515
super().__init__("", **attributes)
@@ -30,7 +30,7 @@ def depart_html(self):
3030

3131

3232
class ThebeOutputNode(docutils.nodes.container):
33-
"""Container that holds all the output nodes when thebelab is enabled"""
33+
"""Container that holds all the output nodes when thebelab is enabled."""
3434

3535
def visit_html(self):
3636
return '<div class="thebelab-output" data-output="true">'
@@ -40,7 +40,7 @@ def depart_html(self):
4040

4141

4242
class ThebeButtonNode(docutils.nodes.Element):
43-
"""Appended to the doctree by the ThebeButton directive
43+
"""Appended to the doctree by the ThebeButton directive.
4444
4545
Renders as a button to enable thebelab on the page.
4646
@@ -61,9 +61,9 @@ def html(self):
6161

6262

6363
class ThebeButton(Directive):
64-
"""Specify a button to activate thebelab on the page
64+
"""Specify a button to activate thebelab on the page.
6565
66-
Arguments
66+
Arguments:
6767
---------
6868
text : str (optional)
6969
If provided, the button text to display
@@ -83,7 +83,7 @@ def run(self):
8383

8484

8585
def add_thebelab_library(doctree, env):
86-
"""Adds the thebelab configuration and library to the doctree"""
86+
"""Adds the thebelab configuration and library to the doctree."""
8787
thebe_config = env.config.jupyter_sphinx_thebelab_config
8888
if isinstance(thebe_config, dict):
8989
pass
@@ -101,14 +101,11 @@ def add_thebelab_library(doctree, env):
101101
try:
102102
thebe_config = json.loads(filename.read_text())
103103
except ValueError:
104-
js.logger.warning(
105-
"The supplied thebelab configuration file is not in JSON format."
106-
)
104+
js.logger.warning("The supplied thebelab configuration file is not in JSON format.")
107105
return
108106
else:
109107
js.logger.warning(
110-
"The supplied thebelab configuration should be either"
111-
" a filename or a dictionary."
108+
"The supplied thebelab configuration should be either" " a filename or a dictionary."
112109
)
113110
return
114111

0 commit comments

Comments
 (0)