Skip to content

Commit db8cedb

Browse files
bryanwweberbsipocz
andauthored
Update glue docs to mention NumPy 2.0 changes (#615)
Changes to the text/plain representation of NumPy objects for NumPy >= 2.0 mean that we need a workaround for formatting. Co-authored-by: Brigitta Sipőcz <bsipocz@gmail.com>
1 parent c90a70e commit db8cedb

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

docs/render/glue.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ To glue keys from other notebooks, see {ref}`glue/crossdoc`.
2525
## Save variables in code cells
2626

2727
You can use `myst_nb.glue()` to assign the output of a variable to a key of your choice.
28-
`glue` will store all of the information that is normally used to display that variable (ie, whatever happens when you display the variable by putting it at the end of a cell).
28+
`glue` will store all of the information that is normally used to display that variable (that is, whatever happens when you display the variable by putting it at the end of a cell).
2929
Choose a key that you will remember, as you will use it later.
3030

3131
The following code glues a variable inside the notebook:
@@ -137,12 +137,12 @@ These variables can be pasted using one of the roles or directives in the `glue:
137137

138138
### The `glue` role/directive
139139

140-
The simplest role and directive are `glue` (a.k.a. `glue:any`),
140+
The simplest role and directive are `glue` (also known as `glue:any`),
141141
which paste the glued output inline or as a block respectively,
142142
with no additional formatting.
143143
Simply add:
144144

145-
````
145+
````md
146146
```{glue} your-key
147147
```
148148
````
@@ -219,6 +219,27 @@ For example, the following: ``My rounded mean: {glue:text}`boot_mean:.2f` `` wil
219219

220220
My rounded mean: {glue:text}`boot_mean:.2f` (95% CI: {glue:text}`boot_clo:.2f`/{glue:text}`boot_chi:.2f`).
221221

222+
````{warning}
223+
As of NumPy 2.0, the `text/plain` representation of [NumPy objects has changed](https://numpy.org/devdocs/release/2.0.0-notes.html#representation-of-numpy-scalars-changed).
224+
Using text formatting with NumPy>=2.0 will give warnings like:
225+
226+
```
227+
sphinx.errors.SphinxWarning: <filename>:257:Failed to format text/plain data: could not convert string to float: 'np.float64(0.9643970836113095)' [mystnb.glue]
228+
```
229+
230+
This can be resolved by either formatting the number before glueing or by setting NumPy to use legacy print options, as shown below.
231+
232+
```python
233+
var = np.float(1.0)
234+
# Format the variable before glueing
235+
glue("var_glue", f"{var:.2f}")
236+
237+
# Or set NumPy legacy print options
238+
np.setprintoptions(legacy="1.25")
239+
glue("var_glue", var)
240+
```
241+
````
242+
222243
+++
223244

224245
### The `glue:figure` directive

0 commit comments

Comments
 (0)