File: configure.md

package info (click to toggle)
sphinx-thebe 0.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 204 kB
  • sloc: python: 292; javascript: 92; makefile: 15
file content (245 lines) | stat: -rw-r--r-- 6,266 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
---
thebe-kernel: ir
---

# Configuration

(configure:selector)=
## Change the HTML selector to mark interactive cells

By default, `sphinx-thebe` will be run on any cells with the `thebe` class.
However, you can customize the HTML selector to use other classes, elements, etc.

For example, if you wanted to convert **all code cells**, you could use the following
selector:

```python
thebe_config = {
   "selector": "div.highlight"
}
```

```{note}
`sphinx-thebe` will subsequently look for any `pre` blocks inside of elements it
finds with the `selector` configuration value. These are the blocks that will be
converted to interactive with `thebe`.
```

## Including outputs with your code

If you'd like to include outputs in the *static* version of your page, and only
overwrite them once the user has run that Thebe cell, you can configure `sphinx-thebe`
to detect and keep the outputs associated with some code. To do so, use
the `selector_output` configuration. This is a selector that is searched for *within* any
items discovered by `selector`. If an output is found, it will be placed just after the
code and Thebe will detect it.

For example, the following code:

``````{tab-set}

`````{tab-item} Myst Markdown

````{container} thebe
```{code-block} r
print("hi")
```

```{container} output
"hi"
```
````

`````

`````{tab-item} reStructuredText

```{code-block} rst
.. container:: thebe
   .. code-block:: r
      print("hi")

   .. container:: output
      "hi"
```

`````

``````

Defines a *parent container* in which we'll put both code and the output of the
code. We'll use a `code-block` for the code, and another `container` node with our
`output` class for the output. `sphinx-gallery` will detect the parent container because
it has a `thebe` class. It will detect the `pre` block inside the container as the
code, and it will detect the `<div>` block with the `output` class as the output.

The result is that initializing Thebe *retains* the output until the cell is
executed, like so:

```{thebe-button}
```

````{container} thebe
```{code-block} r
print("hi")
```

```{container} output
"hi"
```
````

## Setting the Kernel

You can set the kernel that Thebe uses on a page by adding metadata to your
page. To do so, add the following metadata to the top of your page:

```
thebe-kernel: <kernel-name>
```

For example, this page had the following metadata in it:

```
thebe-kernel: ir
```

In addition, this website is configured to use the [Binder R example repository](https://github.com/binder-examples/r)
for its environment. As a result, we can now run R code interactively with Thebe:


```{thebe-button} Launch thebe in R
```

```{code-block}
:class: thebe, thebe-init
# Load ggplot - this will be automatically-run
library(ggplot2)
```

```{code-block}
:class: thebe, thebe-init
# create factors with value labels
mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5),
  	labels=c("3gears","4gears","5gears"))
mtcars$am <- factor(mtcars$am,levels=c(0,1),
  	labels=c("Automatic","Manual"))
mtcars$cyl <- factor(mtcars$cyl,levels=c(4,6,8),
   labels=c("4cyl","6cyl","8cyl"))
```

```{code-block}
:class: thebe, thebe-init
# Kernel density plots for mpg
# grouped by number of gears (indicated by color)
qplot(mpg, data=mtcars, geom="density", fill=gear, alpha=I(.5),
   main="Distribution of Gas Milage", xlab="Miles Per Gallon",
   ylab="Density")
```

```{code-block}
:class: thebe

# Scatterplot of mpg vs. hp for each combination of gears and cylinders
# in each facet, transmittion type is represented by shape and color
qplot(hp, mpg, data=mtcars, shape=am, color=am,
   facets=gear~cyl, size=I(3),
   xlab="Horsepower", ylab="Miles per Gallon")
```

## Automatically running some code

You can tag code blocks to run as soon as the kernel is ready (i.e., without any user input)
by adding the `thebe-init` class to the code blocks. For example:

`````{tab-set}

````{tab-item} MyST Markdown
```{code-block}
:class: thebe, thebe-init
print("hi")
```
````

````{tab-item} reStructuredText

```rst
.. code-block::
   :class: thebe, thebe-init
   
   print("hi")
```

````

`````

These code blocks will be run automatically once the kernel is ready, and their outputs
will be displayed below.


(add-custom-button)=
## Adding your own button to start Thebe

By default, Thebe encourages users to use the `thebe-button` directive to
insert a thebe button into their documentation. However, you can add your own
buttons wherever you wish. Simply ensure that an HTML element has this attribute:

```js
onclick="initThebe()"
```

and it will be able to initialize Thebe on that page on its own.

For example, here is the HTML for the Thebe button generated by the `thebe-button`
directive:

```html
<button title="Make live" class="thebe-button" onclick="initThebe()">
  Make live
</button>
```

## Choose a codemirror theme

You can customize `sphinx-thebe` to use the codemirror theme of your choice.
To do so, use the following configuration:

```python
thebe_config = {
   "codemirror-theme": "<cm-theme>"
}
```

See [the CodeMirror theme demo](https://codemirror.net/demo/theme.html) for a list
of themes that you can use, and what they look like.

## Load `thebe` automatically on all pages

By default, `sphinx-thebe` will lazily load the JS/CSS from `thebe` when the `sphinx-thebe` initialization button is pressed.
This means that no Javascript is loaded until a person explicitly tries to start thebe, which reduces page load times.

If you want `thebe` to be loaded on every page, in an "eager" fashion, you may do so with the following configuration:

```python
thebe_config = {
   "always_load": True
}
```

## Configuration reference

Here's a reference of all of the configuration values avialable to `sphinx-thebe`.
Many of these eventually make their was into the `thebe` configuration. You can
find a [reference for `thebe` configuration here](https://thebe.readthedocs.io/en/latest/config_reference.html).

```python
thebe_config = {
    "always_load": bool (default True)
    "repository_url": "<your-repo-url>",
    "repository_branch": "<your-repo-branch>",
    "selector": "<selector-for-code-cells>",
    "selector_input": "<selector-for-cell-input>",
    "selector_output": "<selector-for-cell-output>",
}
```