Skip to content

Commit

Permalink
better plot images
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Apr 17, 2019
1 parent 8f7b98d commit 1089b4b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 26 deletions.
7 changes: 1 addition & 6 deletions docs/_static/css/custom.css
Expand Up @@ -33,12 +33,7 @@
}

.rst-content dl:not(.docutils) dl dt .classifier::before {
content: ' : ';
}

/* No narrow parameter lists for plot functions with images */
.rst-content .docutils.field-list {
clear: right;
content: ' : ';
}

.rst-content tt.literal, .rst-content tt.literal, .rst-content code.literal {
Expand Down
2 changes: 0 additions & 2 deletions docs/_templates/autosummary/base.rst
@@ -1,7 +1,5 @@
:github_url: {{ fullname | github_url }}

{{ fullname | api_image }}

{% extends "!autosummary/base.rst" %}

.. http://www.sphinx-doc.org/en/stable/ext/autosummary.html#customizing-templates
24 changes: 6 additions & 18 deletions docs/conf.py
@@ -1,10 +1,7 @@
import sys
import logging
from pathlib import Path
from datetime import datetime

from jinja2.defaults import DEFAULT_FILTERS

import matplotlib # noqa
# Don’t use tkinter agg when importing scanpy → … → matplotlib
matplotlib.use('agg')
Expand All @@ -14,9 +11,6 @@
import scanpy # noqa


logger = logging.getLogger(__name__)


# -- General configuration ------------------------------------------------


Expand Down Expand Up @@ -101,6 +95,7 @@

def setup(app):
app.add_stylesheet('css/custom.css')
app.connect('autodoc-process-docstring', insert_function_images)


# -- Options for other output formats ------------------------------------------
Expand All @@ -122,18 +117,11 @@ def setup(app):
# -- Images for plot functions -------------------------------------------------


def api_image(qualname: str) -> str:
# I’d like to make this a contextfilter, but the jinja context doesn’t contain the path,
# so no chance to not hardcode “api/” here.
path = Path(__file__).parent / 'api' / f'{qualname}.png'
print(path, path.is_file())
return f'.. image:: {path.name}\n :width: 200\n :align: right' if path.is_file() else ''


# html_context doesn’t apply to autosummary templates ☹
# and there’s no way to insert filters into those templates
# so we have to modify the default filters
DEFAULT_FILTERS['api_image'] = api_image
def insert_function_images(app, what, name, obj, options, lines):
path = Path(__file__).parent / 'api' / f'{name}.png'
if what != 'function' or not path.is_file(): return
lines[0:0] = [f'.. image:: {path.name}', ' :width: 200', ' :align: right', '']
print(*lines, sep='\n')


# -- Test for new scanpydoc functionality --------------------------------------
Expand Down

0 comments on commit 1089b4b

Please sign in to comment.