Skip to content

Commit

Permalink
Update glue docs to mention NumPy 2.0 changes (#615)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bryanwweber and bsipocz authored Sep 19, 2024
1 parent c90a70e commit db8cedb
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions docs/render/glue.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ To glue keys from other notebooks, see {ref}`glue/crossdoc`.
## Save variables in code cells

You can use `myst_nb.glue()` to assign the output of a variable to a key of your choice.
`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).
`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).
Choose a key that you will remember, as you will use it later.

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

### The `glue` role/directive

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

````
````md
```{glue} your-key
```
````
Expand Down Expand Up @@ -219,6 +219,27 @@ For example, the following: ``My rounded mean: {glue:text}`boot_mean:.2f` `` wil

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

````{warning}
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).
Using text formatting with NumPy>=2.0 will give warnings like:
```
sphinx.errors.SphinxWarning: <filename>:257:Failed to format text/plain data: could not convert string to float: 'np.float64(0.9643970836113095)' [mystnb.glue]
```
This can be resolved by either formatting the number before glueing or by setting NumPy to use legacy print options, as shown below.
```python
var = np.float(1.0)
# Format the variable before glueing
glue("var_glue", f"{var:.2f}")
# Or set NumPy legacy print options
np.setprintoptions(legacy="1.25")
glue("var_glue", var)
```
````

+++

### The `glue:figure` directive
Expand Down

0 comments on commit db8cedb

Please sign in to comment.