Skip to content

Commit 13ee80f

Browse files
committed
added css insertion to improve display
1 parent 68c281a commit 13ee80f

File tree

5 files changed

+132
-23
lines changed

5 files changed

+132
-23
lines changed

doc/source/_static/custom.css

Lines changed: 0 additions & 7 deletions
This file was deleted.

doc/source/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,3 @@
4141
"repo": "jupyter/jupyter-sphinx",
4242
},
4343
}
44-
html_static_path = ["_static/custom.css"] # css overrides for Alabaster theme

doc/source/index.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,6 @@ produces:
251251

252252
print("hello, world!", file=sys.stderr)
253253

254-
.. note::
255-
To adjust the CSS of the ``stderr`` stream, use the ``stderr`` class. If you are using
256-
the default Sphinx theme, for example, add the following
257-
`custom CSS <https://alabaster.readthedocs.io/en/latest/customization.html#custom-stylesheet>`_:
258-
``.stderr {background-color: #FCC}``
259-
260-
261254
Controlling the execution environment
262255
-------------------------------------
263256
The execution environment can be controlled by using the ``jupyter-kernel`` directive. This directive takes

jupyter_sphinx/css/jupyter-sphinx.css

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* Stylesheet for jupyter-sphinx
2+
3+
These styles mimic the Jupyter HTML styles.
4+
5+
The default CSS (Cascading Style Sheet) class structure of jupyter-sphinx
6+
is the following:
7+
8+
jupyter_container
9+
code_cell (optional)
10+
stderr (optional)
11+
output (optional)
12+
13+
If the code_cell is not displayed, then there is not a jupyter_container, and
14+
the output is provided without CSS.
15+
16+
This stylesheet attempts to override the defaults of all packaged Sphinx themes
17+
to display jupter-sphinx cells in a Jupyter-like style.
18+
19+
If you want to adjust the styles, add additional custom CSS to override these
20+
styles.
21+
22+
After a build, this stylesheet is loaded from ./_static/jupyter-sphinx.css .
23+
24+
*/
25+
26+
27+
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);
35+
}
36+
.jupyter_container div.code_cell {
37+
border: 1px solid #cfcfcf;
38+
border-radius: 2px;
39+
background-color: #f7f7f7;
40+
margin: 0 0;
41+
}
42+
43+
.jupyter_container div.code_cell pre {
44+
padding: 4px;
45+
margin: 0 0;
46+
background-color: #f7f7f7;
47+
border: none;
48+
background: none;
49+
-webkit-box-shadow: none; /* for nature */
50+
-moz-box-shadow: none; /* for nature */
51+
}
52+
53+
.jupyter_container div.code_cell * {
54+
margin: 0 0;
55+
}
56+
div.jupyter_container div.highlight {
57+
background-color: #f7f7f7; /* for haiku */
58+
}
59+
div.jupyter_container * {
60+
padding: 0;
61+
margin: 0;
62+
}
63+
/* overrides for sphinx_rtd_theme */
64+
.rst-content .jupyter_container div[class^='highlight'],
65+
.document .jupyter_container div[class^='highlight'],
66+
.rst-content .jupyter_container pre.literal-block {
67+
border:none;
68+
margin: 0;
69+
padding: 0;
70+
background: none;
71+
padding: 3px;
72+
background-color: transparent;
73+
}
74+
/* restore Mathjax CSS, as it assumes a vertical margin. */
75+
.jupyter_container .MathJax_Display {
76+
margin: 1em 0em;
77+
text-align: center;
78+
}
79+
.jupyter_container .stderr {
80+
background-color: #FCC;
81+
border: none;
82+
}
83+
.jupyter_container .output {
84+
border: none;
85+
}
86+
.jupyter_container div.output pre {
87+
background-color: white;
88+
background: none;
89+
padding: 4px;
90+
border: none;
91+
-webkit-box-shadow: none; /* for nature */
92+
-moz-box-shadow: none; /* for nature */
93+
}
94+
.jupyter_container .code_cell td.linenos {
95+
align: right;
96+
padding: 0 0 0 1em;
97+
border-right: 1px solid #cfcfcf;
98+
color: #999;
99+
}
100+
.jupyter_container .output .highlight {
101+
background-color: #ffffff;
102+
}
103+
.jupyter_container .output td.linenos {
104+
align: right;
105+
padding: 0 0 0 1em;
106+
border-right: 1px solid #cfcfcf;
107+
}

jupyter_sphinx/execute.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,12 @@ def apply(self):
400400
source = node.children[0]
401401
source["linenos"] = True
402402

403+
404+
# Add code cell CSS class
405+
for node in nodes:
406+
source = node.children[0]
407+
source.attributes["classes"] = ["code_cell"]
408+
403409
# Write certain cell outputs (e.g. images) to separate files, and
404410
# modify the metadata of the associated cells in 'notebook' to
405411
# include the path to the output file.
@@ -504,26 +510,23 @@ def cell_output_to_nodes(cell, data_priority, write_stderr, dir, thebe_config):
504510
# Adds a "stderr" class that can be customized by the user for both
505511
# the container and the literal_block.
506512
#
507-
# Also adds "error" as a base class, which is a fairly common
508-
# class in Sphinx themes. It should result in differentiation
509-
# from stdout in most Sphinx themes.
510-
#
511513
# Not setting "rawsource" disables Pygment hightlighting, which
512514
# would otherwise add a <div class="highlight">.
513515

514-
container = docutils.nodes.container(classes=["error", "stderr"])
516+
container = docutils.nodes.container(classes=["stderr"])
515517
container.append(docutils.nodes.literal_block(
516518
text=output['text'],
517519
rawsource='', # disables Pygment highlighting
518520
language='none',
519-
classes=["error", "stderr"]
521+
classes=["stderr"]
520522
))
521523
to_add.append(container)
522524
else:
523525
to_add.append(docutils.nodes.literal_block(
524526
text=output['text'],
525527
rawsource=output['text'],
526528
language='none',
529+
classes=["output", "stream"]
527530
))
528531
elif (
529532
output_type == 'error'
@@ -534,6 +537,7 @@ def cell_output_to_nodes(cell, data_priority, write_stderr, dir, thebe_config):
534537
text=text,
535538
rawsource=text,
536539
language='ipythontb',
540+
classes =["output", "traceback"]
537541
))
538542
elif (
539543
output_type in ('display_data', 'execute_result')
@@ -558,19 +562,23 @@ def cell_output_to_nodes(cell, data_priority, write_stderr, dir, thebe_config):
558562
elif mime_type == 'text/html':
559563
to_add.append(docutils.nodes.raw(
560564
text=data,
561-
format='html'
565+
format='html',
566+
classes=["output", "text_html"]
567+
562568
))
563569
elif mime_type == 'text/latex':
564570
to_add.append(math_block(
565571
text=data,
566572
nowrap=False,
567573
number=None,
574+
classes=["output", "text_latex"]
568575
))
569576
elif mime_type == 'text/plain':
570577
to_add.append(docutils.nodes.literal_block(
571578
text=data,
572579
rawsource=data,
573580
language='none',
581+
classes=["output", "text_plain"]
574582
))
575583
elif mime_type == 'application/javascript':
576584
to_add.append(docutils.nodes.raw(
@@ -587,13 +595,13 @@ def cell_output_to_nodes(cell, data_priority, write_stderr, dir, thebe_config):
587595
def attach_outputs(output_nodes, node, thebe_config, cm_language):
588596
if thebe_config:
589597
source = node.children[0]
590-
591598
thebe_source = ThebeSourceNode(hide_code=node.attributes['hide_code'],
592599
code_below=node.attributes['code_below'],
593600
language=cm_language)
594601
thebe_source.children = [source]
595602

596603
node.children = [thebe_source]
604+
node.attributes["classes"] = ["jupyter_container"] # add jupyter classes even if thebe_config <-- CHECK
597605

598606
if not node.attributes['hide_output']:
599607
thebe_output = ThebeOutputNode()
@@ -603,8 +611,11 @@ def attach_outputs(output_nodes, node, thebe_config, cm_language):
603611
else:
604612
node.children = node.children + [thebe_output]
605613
else:
614+
# Only add container class if code is shown
606615
if node.attributes['hide_code']:
607616
node.children = []
617+
else:
618+
node.attributes["classes"] = ["jupyter_container"]
608619

609620
if not node.attributes['hide_output']:
610621
if node.attributes['code_below']:
@@ -767,6 +778,12 @@ def build_finished(app, env):
767778

768779
def setup(app):
769780
# Configuration
781+
# Copy stylesheet
782+
src = os.path.join(os.path.dirname(__file__), 'css')
783+
dst = os.path.join(app.outdir, '_static')
784+
copy_asset(src, dst)
785+
app.add_css_file('jupyter-sphinx.css')
786+
770787
app.add_config_value(
771788
'jupyter_execute_kwargs',
772789
dict(timeout=-1, allow_errors=True, store_widget_state=True),

0 commit comments

Comments
 (0)