Skip to content

Commit ff1fd4b

Browse files
authored
ENH: Add defaults for MyST-NB (#48)
1 parent b16a287 commit ff1fd4b

15 files changed

Lines changed: 226 additions & 89 deletions

File tree

CHANGELOG.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
# Changelog
22

3-
## v0.1.0
3+
## v0.1.0 - 2022-02-05
44

55
([full changelog](https://github.com/executablebooks/sphinx-thebe/compare/v0.0.10...4d1a60c5126ce633b1a36de43b4990b2f4d08730))
66

77
**Lazy load thebe javascript** [#41](https://github.com/executablebooks/sphinx-thebe/pull/41) ([@choldgraf](https://github.com/choldgraf))
88

99
`thebe` will now "lazily load" its javascript only when a bootstrap button is pressed, rather than loading the Javascript on each page.
1010
This saves on bandwidth and pageload speed, since Thebe is generally _not_ used on a page even if it _could_ be used.
11+
12+
## v0.0.10 - 2021-08-24
13+
14+
([full changelog](https://github.com/executablebooks/sphinx-thebe/compare/v0.0.9...e18d1bf94a8fa79476f035a349bd63d03bba83e7))
15+
16+
This is a minor release to conditionally load the JS on pages that have a "launch button".
17+
This will save some load time on non-interactive pages.
18+
19+
### Enhancements made
20+
21+
- Option to conditionally load on pages, see [documentation for details](https://sphinx-thebe.readthedocs.io/en/latest/configure.html#only-load-js-on-certain-pages) [#30](https://github.com/executablebooks/sphinx-thebe/pull/30) ([@choldgraf](https://github.com/choldgraf))
22+
23+
### Other merged PRs
24+
25+
- PIN: thebe v0.5.1 [#31](https://github.com/executablebooks/sphinx-thebe/pull/31) ([@choldgraf](https://github.com/choldgraf))
26+
27+
## v0.0.9 - 2021-08-21
28+
29+
### Updates
30+
31+
- `sphinx-thebe` now uses the correct and latest version of thebe, since it has been renamed from `thebelab` to `thebe`.

docs/changelog.md

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,2 @@
1-
# Changelog
2-
3-
## v0.0.10 - 2021-08-24
4-
5-
([full changelog](https://github.com/executablebooks/sphinx-thebe/compare/v0.0.9...e18d1bf94a8fa79476f035a349bd63d03bba83e7))
6-
7-
This is a minor release to conditionally load the JS on pages that have a "launch button".
8-
This will save some load time on non-interactive pages.
9-
10-
### Enhancements made
11-
12-
- Option to conditionally load on pages, see [documentation for details](https://sphinx-thebe.readthedocs.io/en/latest/configure.html#only-load-js-on-certain-pages) [#30](https://github.com/executablebooks/sphinx-thebe/pull/30) ([@choldgraf](https://github.com/choldgraf))
13-
14-
### Other merged PRs
15-
16-
- PIN: thebe v0.5.1 [#31](https://github.com/executablebooks/sphinx-thebe/pull/31) ([@choldgraf](https://github.com/choldgraf))
17-
18-
## v0.0.9 - 2021-08-21
19-
20-
### Updates
21-
22-
- `sphinx-thebe` now uses the correct and latest version of thebe, since it has been renamed from `thebelab` to `thebe`.
1+
```{include} ../CHANGELOG.md
2+
```

docs/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
# Add any Sphinx extension module names here, as strings. They can be
3939
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
4040
# ones.
41-
extensions = ["sphinx_thebe", "myst_parser", "sphinx_panels"]
41+
extensions = ["myst_nb", "sphinx_copybutton", "sphinx_panels", "sphinx_thebe"]
4242

4343
thebe_config = {
4444
"repository_url": "https://github.com/binder-examples/jupyter-stacks-datascience",
4545
"path_to_docs": "docs",
4646
# "repository_branch": "master",
4747
# "selector": ".thebe",
48-
# "selector_input": ,
49-
# "selector_output": ,
48+
# "selector_input": "",
49+
# "selector_output": "",
5050
# "codemirror-theme": "blackboard", # Doesn't currently work
5151
# "always_load": True, # To load thebe on every page
5252
}

docs/configure.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
thebe-kernel: ir
33
---
44

5-
# Configuring thebe
5+
# Configuration
66

7+
(configure:selector)=
78
## Change the HTML selector to mark interactive cells
89

910
By default, `sphinx-thebe` will be run on any cells with the `thebe` class.

docs/contribute.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
# Contributing to Sphinx-Thebe
1+
# Contribute to `sphinx-thebe`
22

33
Thanks for your interest in contributing to `sphinx-thebe`, your contributions are welcome and appreciated 🎉. This page contains some information to help you get started.
44

5+
## Design philosophy
6+
7+
`sphinx-thebe` is designed to be a simple bridge between Sphinx and `thebe`.
8+
It should not add a lot of extra functionality on top of Thebe, beyond special-casing its configuration for Sphinx objects and its build system.
9+
It's primary goal is to make it pain-free to load `thebe.js` and apply it to websites generated by Sphinx with reasonable default choices.
10+
11+
`sphinx-thebe` adds default configuration to support major Jupyter Notebook extensions in the Sphinx ecosystem.
12+
Currently this means [MyST-NB](https://myst-nb.readthedocs.io/), but in the future we'd also like to support [nbsphinx](https://nbsphinx.readthedocs.io/) by default.
13+
514
## Contributing guide
615

716
See the [ExecutableBooks developer guidelines](https://executablebooks.org/en/latest/contributing.html) for conventions and practices around developing `sphinx-thebe`.
@@ -14,6 +23,13 @@ See the [ExecutableBooks developer guidelines](https://executablebooks.org/en/la
1423
- The sphinx package is contained in `sphinx_thebe/`. The `__init__.py` file contains the code that activates and loads the proper CSS and JS when Sphinx is run.
1524
- CSS and Javascript assets are in `sphinx_thebe/_static`. These handle the activation of `thebe` on a page via a button-click, and also make minor modification to the page's DOM to make it work well with `thebe`.
1625

26+
## Code style
27+
28+
The JavaScript and CSS assets for this repository follow the default values for [prettier](https://prettier.io/).
29+
30+
Python code follows `black` and `pep8` as described in [the EBP contributing guide](https://executablebooks.org/en/latest/contributing.html#coding-style).
31+
32+
1733
## Installation for development
1834

1935
To install `sphinx-thebe` for development clone and install `sphinx-thebe` locally:

docs/examples/notebooks.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
jupytext:
3+
formats: md:myst
4+
text_representation:
5+
extension: .md
6+
format_name: myst
7+
kernelspec:
8+
display_name: Python 3
9+
language: python
10+
name: python3
11+
---
12+
13+
# Jupyter Notebooks
14+
15+
This page was written for [MyST-NB](https://myst-nb.readthedocs.io/).
16+
It demonstrates `sphinx-thebe`'s usage with Jupyter Notebooks.
17+
18+
Activate Thebe by clicking the launch button below.
19+
You should then be able to run and edit the code cell in the notebook.
20+
21+
```{thebe-button} Launch thebe
22+
```
23+
24+
The outputs should be displayed below, but they will be collected by `sphinx-thebe` when it is activated so that they are cleared when you first run the cell.
25+
26+
```{code-cell}
27+
import numpy as np
28+
import matplotlib.pyplot as plt
29+
30+
# Create some fake data
31+
data = np.random.randn(3, 1000)
32+
33+
# Create a figure
34+
fig, ax = plt.subplots()
35+
36+
# Plot data
37+
ax.scatter(data[0], data[1], c=np.abs(data[2]), s=np.abs(data[2])*100)
38+
```
39+
40+
## Code style
41+
42+
Thebe uses CodeMirror in the background, which uses different styles than pygments, which is used for static code syntax highlighting.
43+
44+
The below code block is **static** and will not be converted with `thebe`.
45+
We include it in order to compare the active Thebe cell's syntax highlighting with an inactive cell.
46+
47+
```
48+
import numpy as np
49+
import matplotlib.pyplot as plt
50+
51+
# Create some fake data
52+
data = np.random.randn(3, 1000)
53+
54+
# Create a figure
55+
fig, ax = plt.subplots()
56+
57+
# Plot data
58+
ax.scatter(data[0], data[1], c=np.abs(data[2]), s=np.abs(data[2])*100)
59+
```

docs/examples/rst.rst

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

docs/index.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
Make your code cells interactive with a kernel provided by [Thebe](http://thebe.readthedocs.org/) and [Binder](https://mybinder.org).
1515

16+
It uses the excellent [thebe project](http://thebe.readthedocs.org/), and pre-configures `thebe` to be compatible with common Jupyter-related patterns in the Sphinx ecosystem, such as [MyST-NB](https://myst-nb.readthedocs.io/).
17+
1618
For example, click the button below. Notice that the code block beneath becomes
1719
editable and runnable!
1820

@@ -27,15 +29,10 @@ print("hi")
2729

2830
See [](use.md) for more information about what you can do with `sphinx-thebe`.
2931

30-
```{note}
31-
This package is a Sphinx wrapper around the excellent [thebe project](http://thebe.readthedocs.org/),
32-
a javascript tool to convert static code cells into interactive cells backed
33-
by a kernel.
34-
```
3532

3633
## Install
3734

38-
To install `sphinx-thebe` first clonse and install it:
35+
To install `sphinx-thebe` first clone and install it:
3936

4037
```
4138
pip install sphinx-thebe
@@ -61,7 +58,7 @@ For more information on using `sphinx-thebe`, see [](use.md).
6158
:hidden:
6259
use
6360
configure
61+
examples/notebooks
6462
contribute
65-
examples/rst
6663
changelog
6764
```

docs/use.md

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
# Using `sphinx-thebe`
1+
# Use `sphinx-thebe`
2+
3+
`sphinx-thebe` uses remote Jupyter kernels to execute your page's code and return the
4+
results, and [Binder](https://mybinder.org) to run the infrastructure for execution.
5+
You can do nearly anything with `sphinx-thebe` that you could do from within a Jupyter Notebook cell.
26

37
## Get started
48

59
There are two steps to using `sphinx-thebe`. First, you must mark certain
610
parts of your page as "ready for thebe". Next, you must insert a button onto
711
the page to tell Thebe to initialize.
812

9-
:::{admonition} Using reStructuredText vs. MyST Markdown
10-
:class: tip
11-
The examples on this page use **MyST Markdown syntax**, a form of markdown that works with Sphinx directives. You can also use reStructuredText if you wish. For information about reStructuredText vs. MyST Markdown, see [the MyST Parser documentation](https://myst-parser.readthedocs.io/en/latest/using/syntax.html) as well as [](examples/rst) for some tips.
12-
:::
13-
1413
### Mark elements for thebe
1514

1615
By default, thebe will be run on any elements in your documentation that contain
@@ -19,8 +18,9 @@ the class `thebe` and that have a `<pre`> element underneath them.
1918
You can add code blocks like so:
2019

2120
````
22-
```{code-block}
21+
```{code-block} python
2322
:class: thebe
23+
print("hello world!")
2424
```
2525
````
2626

@@ -37,36 +37,34 @@ directive:
3737
```
3838
````
3939

40-
The button looks like this:
40+
### An example
4141

42-
```{thebe-button}
42+
Here is what it looks like when you add the two snippets above in one example:
43+
44+
First the code block:
45+
46+
```{code-block} python
47+
:class: thebe
48+
print("hello world!")
4349
```
4450

45-
Clicking this button will activate Thebe on the page. If you'd like to manually
46-
add your own button (e.g. with your own extension or theme), see [](add-custom-button).
51+
And now the Thebe button:
4752

48-
```{note}
49-
By default, `sphinx-thebe` will serve the Binder environment for the
50-
[jupyter-stacks-datascience repository](https://github.com/binder-examples/jupyter-stacks-datascience).
51-
See [](configure.md) for information on choosing your own environment.
53+
```{thebe-button}
5254
```
5355

54-
## What can I do with `sphinx-thebe`?
56+
Clicking this button will activate Thebe on the page.
57+
If you'd like to manually add your own button (e.g. with your own extension or theme), see [](add-custom-button).
5558

56-
`sphinx-thebe` uses Jupyter kernels to execute your page's code and return the
57-
results, and Binder in order to run the infrastructure for execution. This means that
58-
you can do nearly anything with `sphinx-thebe` that you could do from within a
59-
Jupyter Notebook cell.
59+
### Customize your environment
6060

61-
```{admonition} You can customize your environment
62-
:class: tip
61+
By default, `sphinx-thebe` will serve the Binder environment for the
62+
[jupyter-stacks-datascience repository](https://github.com/binder-examples/jupyter-stacks-datascience).
63+
See [](configure.md) for information on choosing your own environment.
6364

64-
You can customize the environment that powers your interactive code sessions using
65-
a Binder repository. This may allow for different kinds of functionality depending on
66-
the libraries that are installed. See [](configure.md) for more information.
67-
```
65+
## A few examples
6866

69-
For example:
67+
For example, click the button below (if you have not already clicked the button at the top of the page) and see the sections underneath to watch `sphinx-thebe` in action:
7068

7169
```{thebe-button} Launch examples below!
7270
```
@@ -100,6 +98,52 @@ import matplotlib.pyplot as plt
10098
plt.scatter(*data, c=data[0], s=200)
10199
```
102100

101+
## Structure of a `thebe` cell
102+
103+
`sphinx-thebe` can work with two basic code cell structures:
104+
105+
1. **A single code cell**. In this case, there is just a single code cell that has content that should be made executable, like so:
106+
107+
```html
108+
<div class="highlight">
109+
<pre>print("hi!")</pre>
110+
</div>
111+
```
112+
2. **An input/output cell**. Jupyter tools define code cells as a combination of inputs and outputs.
113+
For example:
114+
115+
```html
116+
<div class="cell">
117+
<div class="cell_input">
118+
<pre>print("hi!")</pre>
119+
</div>
120+
<div class="cell_output">
121+
...outputs here...
122+
</div>
123+
</div>
124+
```
125+
126+
In this case, `sphinx-thebe` will treat the `cell_output` in a special fashion, so that it is cleared when you first run its corresponding input code.
127+
128+
129+
(use:selectors)=
130+
## Selectors `sphinx-thebe` looks for by default
131+
132+
By default `sphinx-thebe` will look for two types of code blocks to turn into interactive cells:
133+
134+
- Cells that match a custom class you can add manually.
135+
It will match:
136+
- Whole cells: match `.thebe`
137+
- Cell inputs: match `pre`
138+
- Cell outputs: match `.output`
139+
- Cells that match [MyST-NB code cells](https://myst-nb.readthedocs.io/).
140+
It will match:
141+
- Whole cells: match `.cell`
142+
- Cell inputs: match `.cell_input`
143+
- Cell outputs: match `.cell_output`
144+
145+
To customize the selectors that `sphinx-thebe` looks for, see [](configure:selector).
146+
103147
## Interactive outputs
104148

105149
Interactive outputs work with `sphinx-thebe` **if their web dependencies are loaded**.

setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@
5050
install_requires=["sphinx>=3.5,<5"],
5151
extras_require={
5252
"sphinx": [
53-
"myst-parser[sphinx]",
53+
"matplotlib",
54+
"myst-nb",
5455
"sphinx-book-theme",
55-
"jupyter-sphinx",
56+
"sphinx-copybutton",
5657
"sphinx-panels",
5758
],
58-
"testing": ["pytest", "pytest-regressions", "beautifulsoup4"],
59+
"testing": ["matplotlib", "pytest", "pytest-regressions", "beautifulsoup4"],
5960
},
6061
)

0 commit comments

Comments
 (0)