Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update glueing docs for NumPy >=2.0 #615

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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