Skip to content

Commit 9c6f06a

Browse files
authored
Gallery (#328)
* Remove language specification to get rid of 404 error. * Initial version of gallery using nbsphinx with a hack to make it work with myst_nb. * Add gallery. * Run black. * Add all dirs to gallery. * WIP: Trying to get thumbnails to work. * Remove sphinxext stuff
1 parent 41d03df commit 9c6f06a

File tree

4 files changed

+212
-3
lines changed

4 files changed

+212
-3
lines changed

examples/conf.py

+94-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import os
2+
from sphinx.application import Sphinx
23

34
# -- Project information -----------------------------------------------------
45
project = "PyMC"
5-
copyright = "2021, PyMC Community"
6+
copyright = "2022, PyMC Community"
67
author = "PyMC Community"
78

89
# -- General configuration ---------------------------------------------------
@@ -21,6 +22,7 @@
2122
"sphinxcontrib.bibtex",
2223
"sphinx_codeautolink",
2324
"notfound.extension",
25+
"sphinx_gallery.load_style",
2426
]
2527

2628
# List of patterns, relative to source directory, that match files and
@@ -37,6 +39,96 @@
3739
]
3840

3941

42+
def hack_nbsphinx(app: Sphinx) -> None:
43+
from nbsphinx import (
44+
depart_gallery_html,
45+
doctree_resolved,
46+
GalleryNode,
47+
NbGallery,
48+
patched_toctree_resolve,
49+
NotebookParser,
50+
NbInput,
51+
NbOutput,
52+
NbInfo,
53+
NbWarning,
54+
CodeAreaNode,
55+
depart_codearea_html,
56+
visit_codearea_latex,
57+
depart_codearea_latex,
58+
GetSizeFromImages,
59+
)
60+
from sphinx.environment.adapters import toctree
61+
62+
nbsphinx_thumbnails = {
63+
"case_studies/stochastic_volatility": "_static/stochastic_volatility.png",
64+
}
65+
66+
def builder_inited(app: Sphinx):
67+
if not hasattr(app.env, "nbsphinx_thumbnails"):
68+
app.env.nbsphinx_thumbnails = {}
69+
70+
def do_nothing(*node):
71+
pass
72+
73+
app.add_source_parser(NotebookParser)
74+
# app.add_config_value('nbsphinx_execute', 'auto', rebuild='env')
75+
app.add_config_value("nbsphinx_kernel_name", "", rebuild="env")
76+
app.add_config_value("nbsphinx_execute_arguments", [], rebuild="env")
77+
app.add_config_value("nbsphinx_allow_errors", False, rebuild="")
78+
app.add_config_value("nbsphinx_timeout", None, rebuild="")
79+
app.add_config_value("nbsphinx_codecell_lexer", "none", rebuild="env")
80+
app.add_config_value("nbsphinx_prompt_width", "4.5ex", rebuild="html")
81+
app.add_config_value("nbsphinx_responsive_width", "540px", rebuild="html")
82+
app.add_config_value("nbsphinx_prolog", None, rebuild="env")
83+
app.add_config_value("nbsphinx_epilog", None, rebuild="env")
84+
app.add_config_value("nbsphinx_input_prompt", "[%s]:", rebuild="env")
85+
app.add_config_value("nbsphinx_output_prompt", "[%s]:", rebuild="env")
86+
app.add_config_value("nbsphinx_custom_formats", {}, rebuild="env")
87+
# Default value is set in config_inited():
88+
app.add_config_value("nbsphinx_requirejs_path", None, rebuild="html")
89+
# Default value is set in config_inited():
90+
app.add_config_value("nbsphinx_requirejs_options", None, rebuild="html")
91+
# This will be updated in env_updated():
92+
app.add_config_value("nbsphinx_widgets_path", None, rebuild="html")
93+
app.add_config_value("nbsphinx_widgets_options", {}, rebuild="html")
94+
# app.add_config_value('nbsphinx_thumbnails', {}, rebuild='html')
95+
app.add_config_value("nbsphinx_assume_equations", True, rebuild="env")
96+
97+
app.add_directive("nbinput", NbInput)
98+
app.add_directive("nboutput", NbOutput)
99+
app.add_directive("nbinfo", NbInfo)
100+
app.add_directive("nbwarning", NbWarning)
101+
app.add_directive("nbgallery", NbGallery)
102+
app.add_node(
103+
CodeAreaNode,
104+
html=(do_nothing, depart_codearea_html),
105+
latex=(visit_codearea_latex, depart_codearea_latex),
106+
text=(do_nothing, do_nothing),
107+
)
108+
app.connect("builder-inited", builder_inited)
109+
app.connect("doctree-resolved", doctree_resolved)
110+
app.add_post_transform(GetSizeFromImages)
111+
112+
app.add_config_value("nbsphinx_execute", "auto", rebuild="env")
113+
app.add_config_value("nbsphinx_thumbnails", nbsphinx_thumbnails, rebuild="html")
114+
app.add_directive("nbgallery", NbGallery)
115+
app.add_node(
116+
GalleryNode,
117+
html=(do_nothing, depart_gallery_html),
118+
latex=(do_nothing, do_nothing),
119+
text=(do_nothing, do_nothing),
120+
)
121+
app.connect("builder-inited", builder_inited)
122+
app.connect("doctree-resolved", doctree_resolved)
123+
124+
# Monkey-patch Sphinx TocTree adapter
125+
toctree.TocTree.resolve = patched_toctree_resolve
126+
127+
128+
def setup(app: Sphinx):
129+
hack_nbsphinx(app)
130+
131+
40132
# -- Options for HTML output -------------------------------------------------
41133

42134
# The theme to use for HTML and HTML Help pages. See the documentation for
@@ -93,7 +185,7 @@
93185
# Add any paths that contain custom static files (such as style sheets) here,
94186
# relative to this directory. They are copied after the builtin static files,
95187
# so a file named "default.css" will overwrite the builtin "default.css".
96-
html_static_path = ["../_static"]
188+
html_static_path = ["../_static", "../thumbnails"]
97189
html_css_files = ["custom.css"]
98190
templates_path = ["../_templates"]
99191
html_sidebars = {

examples/gallery.rst

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
Gallery
2+
=======
3+
4+
5+
(Generalized) Linear and Hierarchical Linear Models
6+
-------------------------
7+
8+
.. nbgallery::
9+
:caption: This is a thumbnail gallery:
10+
:name: generalized_linear_models
11+
:glob:
12+
:reversed:
13+
14+
generalized_linear_models/*
15+
16+
17+
Case Studies
18+
------------
19+
20+
.. nbgallery::
21+
:caption: This is a thumbnail gallery:
22+
:name: case_studies
23+
:glob:
24+
:reversed:
25+
26+
case_studies/*
27+
28+
Diagnostics and Model Criticism
29+
-------------------------------
30+
31+
.. nbgallery::
32+
:caption: This is a thumbnail gallery:
33+
:name: diagnostics_and_criticism
34+
:glob:
35+
:reversed:
36+
37+
diagnostics_and_criticism/*
38+
39+
40+
Gaussian Processes
41+
------------------
42+
43+
.. nbgallery::
44+
:caption: This is a thumbnail gallery:
45+
:name: gaussian_processes
46+
:glob:
47+
:reversed:
48+
49+
gaussian_processes/*
50+
51+
Inference in ODE models
52+
-----------------------
53+
54+
.. nbgallery::
55+
:caption: This is a thumbnail gallery:
56+
:name: ode_models
57+
:glob:
58+
:reversed:
59+
60+
ode_models/*
61+
62+
MCMC
63+
----
64+
65+
.. nbgallery::
66+
:caption: This is a thumbnail gallery:
67+
:name: samplers
68+
:glob:
69+
:reversed:
70+
71+
samplers/*
72+
73+
Mixture Models
74+
--------------
75+
76+
.. nbgallery::
77+
:caption: This is a thumbnail gallery:
78+
:name: mixture_models
79+
:glob:
80+
:reversed:
81+
82+
mixture_models/*
83+
84+
Survival Analysis
85+
-----------------
86+
87+
.. nbgallery::
88+
:caption: This is a thumbnail gallery:
89+
:name: survival_analysis
90+
:glob:
91+
:reversed:
92+
93+
survival_analysis/*
94+
95+
Time Series
96+
-----------
97+
98+
.. nbgallery::
99+
:caption: This is a thumbnail gallery:
100+
:name: time_series
101+
:glob:
102+
:reversed:
103+
104+
time_series/*
105+
106+
Variational Inference
107+
---------------------
108+
109+
.. nbgallery::
110+
:caption: This is a thumbnail gallery:
111+
:name: variational_inference
112+
:glob:
113+
:reversed:
114+
115+
variational_inference/*

examples/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ has tags in its metadata they are listed on the right sidebar after the {fas}`ta
7474
:::{toctree}
7575
:maxdepth: 1
7676
:hidden:
77-
77+
gallery
7878
blog
7979
object_index/index
8080
:::

requirements-docs.txt

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ ablog
88
sphinxext-opengraph
99
sphinx-codeautolink
1010
sphinx-notfound-page
11+
nbsphinx
12+
sphinx-gallery

0 commit comments

Comments
 (0)