diff --git a/doc/a-normal-rst-file.rst b/doc/a-normal-rst-file.rst
index 47994665..cb0ee5b2 100644
--- a/doc/a-normal-rst-file.rst
+++ b/doc/a-normal-rst-file.rst
@@ -316,7 +316,7 @@ __ https://www.sphinx-doc.org/en/master/usage/restructuredtext/
.. note::
- The notes regarding LaTeX in :ref:`/subdir/gallery.ipynb`
+ The notes regarding LaTeX in :ref:`/gallery/gallery-with-nested-documents.ipynb`
and :ref:`/subdir/toctree.ipynb` also apply here!
The following example gallery was created using:
diff --git a/doc/conf.py b/doc/conf.py
index f5196378..425bc7f3 100644
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -8,7 +8,6 @@
'nbsphinx',
'sphinxcontrib.bibtex', # for bibliographic references
'sphinxcontrib.rsvgconverter', # for SVG->PDF conversion in LaTeX output
- 'sphinx_gallery.load_style', # load CSS for gallery (needs SG >= 0.6)
'sphinx_last_updated_by_git', # get "last updated" from Git
'sphinx_codeautolink', # automatic links from code to documentation
'sphinx.ext.intersphinx', # links to other Sphinx projects (e.g. NumPy)
@@ -37,6 +36,7 @@
nbsphinx_thumbnails = {
'gallery/thumbnail-from-conf-py': 'gallery/a-local-file.png',
'gallery/*-rst': 'images/notebook_icon.png',
+ 'orphan': '_static/favicon.svg',
}
# This is processed by Jinja2 and inserted before each notebook
diff --git a/doc/configuration.ipynb b/doc/configuration.ipynb
index b3fa97a4..5e1bc938 100644
--- a/doc/configuration.ipynb
+++ b/doc/configuration.ipynb
@@ -77,8 +77,7 @@
" for [SVG->PDF conversion in LaTeX output](markdown-cells.ipynb#SVG-support-for-LaTeX)\n",
"* `'sphinx_copybutton'`\n",
" for [adding \"copy to clipboard\" buttons](https://sphinx-copybutton.readthedocs.io/)\n",
- " to all text/code boxes\n",
- "* `'sphinx_gallery.load_style'` to load CSS styles for [thumbnail galleries](subdir/gallery.ipynb)"
+ " to all text/code boxes"
]
},
{
diff --git a/doc/requirements.txt b/doc/requirements.txt
index 66deb78a..12964890 100644
--- a/doc/requirements.txt
+++ b/doc/requirements.txt
@@ -5,7 +5,6 @@ pandas
sphinxcontrib-bibtex>=2.1
sphinxcontrib-svg2pdfconverter
ipywidgets
-sphinx-gallery<0.11 # https://github.com/spatialaudio/nbsphinx/issues/655
jupytext
sphinx-last-updated-by-git
sphinx-codeautolink
diff --git a/doc/subdir/gallery.ipynb b/doc/subdir/gallery.ipynb
index e1329f56..2e81139e 100644
--- a/doc/subdir/gallery.ipynb
+++ b/doc/subdir/gallery.ipynb
@@ -19,41 +19,16 @@
"you can create thumbnail galleries from a list of Jupyter notebooks\n",
"(or other Sphinx source files).\n",
"\n",
- "`nbsphinx` does *not* provide any gallery styles,\n",
- "but you can easily use the styles from Sphinx-Gallery\n",
- "by installing it:\n",
- "\n",
- " python3 -m pip install sphinx-gallery\n",
- "\n",
- "... and loading the styles in your `conf.py` with:\n",
- "\n",
- "```python\n",
- "extensions = [\n",
- " 'nbsphinx',\n",
- " 'sphinx_gallery.load_style',\n",
- " # more extensions, if needed ...\n",
- "]\n",
- "```\n",
- "\n",
- "You'll need Sphinx-Gallery version 0.6 or higher.\n",
- "\n",
- "
\n",
- "\n",
- "Note\n",
- "\n",
- "Sphinx-Gallery has changed its CSS classes in version 0.11\n",
- "in an incompatible way (see [issue #655](https://github.com/spatialaudio/nbsphinx/issues/655)).\n",
- "To restore the intended look, update your dependencies\n",
- "to an older version:\n",
- "\n",
- " sphinx-gallery<0.11\n",
- "\n",
- "
\n",
- "\n",
- "However, you can also create your own CSS styles if you prefer\n",
- "(then you don't need to install Sphinx-Gallery).\n",
- "You can load your CSS files with\n",
+ "`nbsphinx` provides CSS styles for galleries,\n",
+ "but like all styles you can tweak them\n",
+ "with your own CSS files loaded via\n",
"[html_css_files](https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_css_files).\n",
+ "If you want to disable all the original styling,\n",
+ "you can create a file named `nbsphinx-gallery.css` somewhere in your\n",
+ "[html_static_path](https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_static_path),\n",
+ "which will replace the original CSS file\n",
+ "(adding it to `html_css_files` is not necessary in this case,\n",
+ "because it is automatically added by `nbsphinx`).\n",
"\n",
"The following sections present\n",
"two different ways of creating thumbnail galleries\n",
diff --git a/setup.py b/setup.py
index e0a80c6a..4c57600d 100644
--- a/setup.py
+++ b/setup.py
@@ -4,7 +4,7 @@
# "import" __version__
__version__ = 'unknown'
-with Path('src/nbsphinx.py').open() as f:
+with Path('src/nbsphinx/__init__.py').open() as f:
for line in f:
if line.startswith('__version__'):
exec(line)
@@ -14,7 +14,13 @@
name='nbsphinx',
version=__version__,
package_dir={'': 'src'},
- py_modules=['nbsphinx'],
+ packages=['nbsphinx'],
+ package_data={'nbsphinx': [
+ '_static/nbsphinx-code-cells.css_t',
+ '_static/nbsphinx-gallery.css',
+ '_static/nbsphinx-no-thumbnail.svg',
+ '_static/nbsphinx-broken-thumbnail.svg',
+ ]},
python_requires='>=3.6',
install_requires=[
'docutils',
diff --git a/src/nbsphinx.py b/src/nbsphinx/__init__.py
similarity index 88%
rename from src/nbsphinx.py
rename to src/nbsphinx/__init__.py
index ea156451..9f1e2045 100644
--- a/src/nbsphinx.py
+++ b/src/nbsphinx/__init__.py
@@ -1,23 +1,3 @@
-# Copyright (c) 2015-2022 Matthias Geier
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-
"""Jupyter Notebook Tools for Sphinx.
https://nbsphinx.readthedocs.io/
@@ -498,314 +478,6 @@
"""
-CSS_STRING = """
-/* CSS for nbsphinx extension */
-
-/* remove conflicting styling from Sphinx themes */
-div.nbinput.container div.prompt *,
-div.nboutput.container div.prompt *,
-div.nbinput.container div.input_area pre,
-div.nboutput.container div.output_area pre,
-div.nbinput.container div.input_area .highlight,
-div.nboutput.container div.output_area .highlight {
- border: none;
- padding: 0;
- margin: 0;
- box-shadow: none;
-}
-
-div.nbinput.container > div[class*=highlight],
-div.nboutput.container > div[class*=highlight] {
- margin: 0;
-}
-
-div.nbinput.container div.prompt *,
-div.nboutput.container div.prompt * {
- background: none;
-}
-
-div.nboutput.container div.output_area .highlight,
-div.nboutput.container div.output_area pre {
- background: unset;
-}
-
-div.nboutput.container div.output_area div.highlight {
- color: unset; /* override Pygments text color */
-}
-
-/* avoid gaps between output lines */
-div.nboutput.container div[class*=highlight] pre {
- line-height: normal;
-}
-
-/* input/output containers */
-div.nbinput.container,
-div.nboutput.container {
- display: -webkit-flex;
- display: flex;
- align-items: flex-start;
- margin: 0;
- width: 100%%;
-}
-@media (max-width: %(nbsphinx_responsive_width)s) {
- div.nbinput.container,
- div.nboutput.container {
- flex-direction: column;
- }
-}
-
-/* input container */
-div.nbinput.container {
- padding-top: 5px;
-}
-
-/* last container */
-div.nblast.container {
- padding-bottom: 5px;
-}
-
-/* input prompt */
-div.nbinput.container div.prompt pre {
- color: #307FC1;
-}
-
-/* output prompt */
-div.nboutput.container div.prompt pre {
- color: #BF5B3D;
-}
-
-/* all prompts */
-div.nbinput.container div.prompt,
-div.nboutput.container div.prompt {
- width: %(nbsphinx_prompt_width)s;
- padding-top: 5px;
- position: relative;
- user-select: none;
-}
-
-div.nbinput.container div.prompt > div,
-div.nboutput.container div.prompt > div {
- position: absolute;
- right: 0;
- margin-right: 0.3ex;
-}
-
-@media (max-width: %(nbsphinx_responsive_width)s) {
- div.nbinput.container div.prompt,
- div.nboutput.container div.prompt {
- width: unset;
- text-align: left;
- padding: 0.4em;
- }
- div.nboutput.container div.prompt.empty {
- padding: 0;
- }
-
- div.nbinput.container div.prompt > div,
- div.nboutput.container div.prompt > div {
- position: unset;
- }
-}
-
-/* disable scrollbars and line breaks on prompts */
-div.nbinput.container div.prompt pre,
-div.nboutput.container div.prompt pre {
- overflow: hidden;
- white-space: pre;
-}
-
-/* input/output area */
-div.nbinput.container div.input_area,
-div.nboutput.container div.output_area {
- -webkit-flex: 1;
- flex: 1;
- overflow: auto;
-}
-@media (max-width: %(nbsphinx_responsive_width)s) {
- div.nbinput.container div.input_area,
- div.nboutput.container div.output_area {
- width: 100%%;
- }
-}
-
-/* input area */
-div.nbinput.container div.input_area {
- border: 1px solid #e0e0e0;
- border-radius: 2px;
- /*background: #f5f5f5;*/
-}
-
-/* override MathJax center alignment in output cells */
-div.nboutput.container div[class*=MathJax] {
- text-align: left !important;
-}
-
-/* override sphinx.ext.imgmath center alignment in output cells */
-div.nboutput.container div.math p {
- text-align: left;
-}
-
-/* standard error */
-div.nboutput.container div.output_area.stderr {
- background: #fdd;
-}
-
-/* ANSI colors */
-.ansi-black-fg { color: #3E424D; }
-.ansi-black-bg { background-color: #3E424D; }
-.ansi-black-intense-fg { color: #282C36; }
-.ansi-black-intense-bg { background-color: #282C36; }
-.ansi-red-fg { color: #E75C58; }
-.ansi-red-bg { background-color: #E75C58; }
-.ansi-red-intense-fg { color: #B22B31; }
-.ansi-red-intense-bg { background-color: #B22B31; }
-.ansi-green-fg { color: #00A250; }
-.ansi-green-bg { background-color: #00A250; }
-.ansi-green-intense-fg { color: #007427; }
-.ansi-green-intense-bg { background-color: #007427; }
-.ansi-yellow-fg { color: #DDB62B; }
-.ansi-yellow-bg { background-color: #DDB62B; }
-.ansi-yellow-intense-fg { color: #B27D12; }
-.ansi-yellow-intense-bg { background-color: #B27D12; }
-.ansi-blue-fg { color: #208FFB; }
-.ansi-blue-bg { background-color: #208FFB; }
-.ansi-blue-intense-fg { color: #0065CA; }
-.ansi-blue-intense-bg { background-color: #0065CA; }
-.ansi-magenta-fg { color: #D160C4; }
-.ansi-magenta-bg { background-color: #D160C4; }
-.ansi-magenta-intense-fg { color: #A03196; }
-.ansi-magenta-intense-bg { background-color: #A03196; }
-.ansi-cyan-fg { color: #60C6C8; }
-.ansi-cyan-bg { background-color: #60C6C8; }
-.ansi-cyan-intense-fg { color: #258F8F; }
-.ansi-cyan-intense-bg { background-color: #258F8F; }
-.ansi-white-fg { color: #C5C1B4; }
-.ansi-white-bg { background-color: #C5C1B4; }
-.ansi-white-intense-fg { color: #A1A6B2; }
-.ansi-white-intense-bg { background-color: #A1A6B2; }
-
-.ansi-default-inverse-fg { color: #FFFFFF; }
-.ansi-default-inverse-bg { background-color: #000000; }
-
-.ansi-bold { font-weight: bold; }
-.ansi-underline { text-decoration: underline; }
-
-
-div.nbinput.container div.input_area div[class*=highlight] > pre,
-div.nboutput.container div.output_area div[class*=highlight] > pre,
-div.nboutput.container div.output_area div[class*=highlight].math,
-div.nboutput.container div.output_area.rendered_html,
-div.nboutput.container div.output_area > div.output_javascript,
-div.nboutput.container div.output_area:not(.rendered_html) > img{
- padding: 5px;
- margin: 0;
-}
-
-/* fix copybtn overflow problem in chromium (needed for 'sphinx_copybutton') */
-div.nbinput.container div.input_area > div[class^='highlight'],
-div.nboutput.container div.output_area > div[class^='highlight']{
- overflow-y: hidden;
-}
-
-/* hide copybtn icon on prompts (needed for 'sphinx_copybutton') */
-.prompt .copybtn {
- display: none;
-}
-
-/* Some additional styling taken form the Jupyter notebook CSS */
-.jp-RenderedHTMLCommon table,
-div.rendered_html table {
- border: none;
- border-collapse: collapse;
- border-spacing: 0;
- color: black;
- font-size: 12px;
- table-layout: fixed;
-}
-.jp-RenderedHTMLCommon thead,
-div.rendered_html thead {
- border-bottom: 1px solid black;
- vertical-align: bottom;
-}
-.jp-RenderedHTMLCommon tr,
-.jp-RenderedHTMLCommon th,
-.jp-RenderedHTMLCommon td,
-div.rendered_html tr,
-div.rendered_html th,
-div.rendered_html td {
- text-align: right;
- vertical-align: middle;
- padding: 0.5em 0.5em;
- line-height: normal;
- white-space: normal;
- max-width: none;
- border: none;
-}
-.jp-RenderedHTMLCommon th,
-div.rendered_html th {
- font-weight: bold;
-}
-.jp-RenderedHTMLCommon tbody tr:nth-child(odd),
-div.rendered_html tbody tr:nth-child(odd) {
- background: #f5f5f5;
-}
-.jp-RenderedHTMLCommon tbody tr:hover,
-div.rendered_html tbody tr:hover {
- background: rgba(66, 165, 245, 0.2);
-}
-"""
-
-CSS_STRING_READTHEDOCS = """
-/* CSS overrides for sphinx_rtd_theme */
-
-/* 24px margin */
-.nbinput.nblast.container,
-.nboutput.nblast.container {
- margin-bottom: 19px; /* padding has already 5px */
-}
-
-/* ... except between code cells! */
-.nblast.container + .nbinput.container {
- margin-top: -19px;
-}
-
-.admonition > p:before {
- margin-right: 4px; /* make room for the exclamation icon */
-}
-
-/* Fix math alignment, see https://github.com/rtfd/sphinx_rtd_theme/pull/686 */
-.math {
- text-align: unset;
-}
-"""
-
-CSS_STRING_CLOUD = """
-/* CSS overrides for cloud theme */
-
-/* nicer titles and more space for info and warning logos */
-
-div.admonition p.admonition-title {
- background: rgba(0, 0, 0, .05);
- margin: .5em -1em;
- margin-top: -.5em !important;
- padding: .5em .5em .5em 2.65em;
-}
-
-/* indent single paragraph */
-div.admonition {
- text-indent: 20px;
-}
-/* don't indent multiple paragraphs */
-div.admonition > p {
- text-indent: 0;
-}
-/* remove excessive padding */
-div.admonition.inline-title p.admonition-title {
- padding-left: .2em;
-}
-"""
-
-
class Exporter(nbconvert.RSTExporter):
"""Convert Jupyter notebooks to reStructuredText.
@@ -1164,7 +836,7 @@ class FancyOutputNode(docutils.nodes.Element):
def _create_code_nodes(directive):
"""Create nodes for an input or output code cell."""
- directive.state.document['nbsphinx_include_css'] = True
+ directive.state.document['nbsphinx_code_css'] = True
execution_count = directive.options.get('execution-count')
config = directive.state.document.settings.env.config
if isinstance(directive, NbInput):
@@ -2098,20 +1770,12 @@ def env_purge_doc(app, env, docname):
def html_page_context(app, pagename, templatename, context, doctree):
- """Add CSS string to HTML pages that contain code cells."""
- style = ''
- if doctree and doctree.get('nbsphinx_include_css'):
- style += CSS_STRING % app.config
- if doctree and app.config.html_theme in (
- 'sphinx_rtd_theme',
- 'julia',
- 'dask_sphinx_theme',
- ):
- style += CSS_STRING_READTHEDOCS
- if doctree and app.config.html_theme.endswith('cloud'):
- style += CSS_STRING_CLOUD
- if style:
- context['body'] = '\n\n' + context['body']
+ """Add CSS files for code cells and galleries."""
+ # NB: the CSS files are copied in html_collect_pages().
+ if doctree and doctree.get('nbsphinx_code_css'):
+ app.add_css_file('nbsphinx-code-cells.css')
+ if doctree and doctree.get('nbsphinx_gallery_css'):
+ app.add_css_file('nbsphinx-gallery.css')
def html_collect_pages(app):
@@ -2137,8 +1801,23 @@ def html_collect_pages(app):
sphinx.util.copyfile(
os.path.join(app.env.nbsphinx_auxdir, notebook),
os.path.join(app.builder.outdir, notebook))
- return [] # No new HTML pages are created
+ context = {
+ 'nbsphinx_responsive_width': app.config.nbsphinx_responsive_width,
+ 'nbsphinx_prompt_width': app.config.nbsphinx_prompt_width,
+ }
+ assets = (
+ 'nbsphinx-code-cells.css_t',
+ 'nbsphinx-gallery.css',
+ 'nbsphinx-no-thumbnail.svg',
+ 'nbsphinx-broken-thumbnail.svg',
+ )
+ for a in assets:
+ sphinx.util.fileutil.copy_asset(
+ os.path.join(os.path.dirname(__file__), '_static', a),
+ os.path.join(app.builder.outdir, '_static'),
+ context=context)
+ return [] # No new HTML pages are created
def env_updated(app, env):
widgets_path = app.config.nbsphinx_widgets_path
@@ -2213,7 +1892,7 @@ def has_wildcard(pattern):
filename = thumbnail.get('filename', '')
if filename is _BROKEN_THUMBNAIL:
filename = os.path.join(
- base, '_static', 'broken_example.png')
+ base, '_static', 'nbsphinx-broken-thumbnail.svg')
elif filename:
filename = os.path.join(
base, app.builder.imagedir, filename)
@@ -2221,7 +1900,8 @@ def has_wildcard(pattern):
# NB: Settings from conf.py can be overwritten in notebook
filename = os.path.join(base, conf_py_thumbnail)
else:
- filename = os.path.join(base, '_static', 'no_image.png')
+ filename = os.path.join(
+ base, '_static', 'nbsphinx-no-thumbnail.svg')
entries.append((title, uri, filename, tooltip))
else:
logger.warning(
@@ -2249,12 +1929,13 @@ def has_wildcard(pattern):
newnode['toctree'] = True
toctree_wrapper[0] = newnode
- node.replace_self([toctree_wrapper, gallery])
else:
# NbGallery
toctree['nbsphinx_gallery'] = True
- node.replace_self([toctree_wrapper, gallery])
# NB: Further processing happens in patched_toctree_resolve()
+ toctree_wrapper += gallery
+ node.replace_self(toctree_wrapper)
+ doctree['nbsphinx_gallery_css'] = True
def depart_codearea_html(self, node):
@@ -2387,29 +2068,22 @@ def depart_admonition_text(self, node):
def depart_gallery_html(self, node):
+ self.body.append('
\n')
for title, uri, filename, tooltip in node['entries']:
if tooltip:
- tooltip = ' tooltip="{}"'.format(html.escape(tooltip))
+ tooltip = ' title="{}"'.format(html.escape(tooltip))
self.body.append("""\
-