Skip to content

Commit 68c281a

Browse files
authored
Merge pull request #86 from seanpue/std_err_in_div
style stderr differently to stdout Also allows for custom styling to be added to stderr with the 'stderr' CSS class. Closes #71.
2 parents b66ee7e + f324daa commit 68c281a

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

doc/source/_static/custom.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
Custom CSS for stderr stream output.
3+
*/
4+
5+
.stderr {
6+
background-color: #FCC;
7+
}

doc/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,12 @@ produces:
251251

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

254-
.. warning::
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}``
255259

256-
Note that output written to ``stderr`` is not displayed any differently than output written
257-
to ``stdout``.
258260

259261
Controlling the execution environment
260262
-------------------------------------

jupyter_sphinx/execute.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,13 +494,37 @@ def cell_output_to_nodes(cell, data_priority, write_stderr, dir, thebe_config):
494494
if (
495495
output_type == 'stream'
496496
):
497-
if not write_stderr and output["name"] == "stderr":
498-
continue
499-
to_add.append(docutils.nodes.literal_block(
500-
text=output['text'],
501-
rawsource=output['text'],
502-
language='none',
503-
))
497+
if output["name"] == "stderr":
498+
if not write_stderr:
499+
continue
500+
else:
501+
# Output a container with an unhighlighted literal block for
502+
# `stderr` messages.
503+
#
504+
# Adds a "stderr" class that can be customized by the user for both
505+
# the container and the literal_block.
506+
#
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+
#
511+
# Not setting "rawsource" disables Pygment hightlighting, which
512+
# would otherwise add a <div class="highlight">.
513+
514+
container = docutils.nodes.container(classes=["error", "stderr"])
515+
container.append(docutils.nodes.literal_block(
516+
text=output['text'],
517+
rawsource='', # disables Pygment highlighting
518+
language='none',
519+
classes=["error", "stderr"]
520+
))
521+
to_add.append(container)
522+
else:
523+
to_add.append(docutils.nodes.literal_block(
524+
text=output['text'],
525+
rawsource=output['text'],
526+
language='none',
527+
))
504528
elif (
505529
output_type == 'error'
506530
):

tests/test_execute.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ def test_stderr(doctree):
267267
tree = doctree(source)
268268
cell, = tree.traverse(JupyterCellNode)
269269
assert len(cell.children) == 2
270-
assert cell.children[1].rawsource.strip() == "hello world"
270+
assert 'stderr' in cell.children[1].attributes['classes']
271+
assert 'error' in cell.children[1].attributes['classes']
272+
assert cell.children[1].astext().strip() == "hello world"
271273

272274

273275
thebe_config = "jupyter_sphinx_thebelab_config = {\"dummy\": True}"

0 commit comments

Comments
 (0)