jupyter-book / mystmd

Command line tools for working with MyST Markdown.
https://mystmd.org/guide
MIT License
219 stars 64 forks source link

Cannot eval variable from notebook in markdown file #1557

Open mortenengen opened 1 month ago

mortenengen commented 1 month ago

I want to use a notebook to perform calculations, and one or more markdown files to write a document where I am using figures and variables from the notebook. Matplotlib plots created in the notebook are embedded using the recipe in docs. However, I am not able to use the eval role to embed a variable from the notebook. Steps to reproduce the observed behaviour are shown below.

I am running on:

Description

Please find below three files that can be used to reproduce the observed behaviour: 1) requirements.txt, 2) calculations.ipynb, and 3) report.md.

requirements.txt

The Python environment can be created with the dependencies given in requirements.txt:

matplotlib
jupyter-server
ipykernel

calculations.ipynb

A notebook with the following cells. The first and last cells are markdown, the remaining cells are python.

---
title: Calculations
author: Name Nameson
exports:
  - format: pdf
    output: calculations.pdf
    template: plain_latex
---
#| tags:
#|  - remove-cell
import matplotlib.pyplot as plt
import numpy as np
#| tags:
#|  - remove-cell
#| label: fig-cos
x = np.linspace(0, np.pi)
y = np.cos(x)

fig, ax = plt.subplots()
ax.plot(x, y);
#| tags:
#|  - remove-cell
calculation_result = 42.0
The calculation result is {eval}`calculation_result`.

If I do a myst build calculations.ipynb --pdf --execute the variable is embedded as expected. āœ…

report.md

A markdown file with the following contents.

---
title: The report
author: Name Nameson
exports:
  - format: pdf
    output: report.pdf
    template: plain_latex
kernelspec:
  name: python3
  display_name: venv
---
:::{figure} #fig-cos
:label: figure-from-nb
The `cos` function.
:::

The calculation result is {eval}`calculation_result`.

If I do a myst build report.md --pdf --execute the matplotlib figure from the notebook is embedded as expected, but the variable calculation_result cannot be embedded. šŸ’„

The following is printed in the terminal, possibly indicating that calculation_error cannot be found in the namespace available to report.md:

šŸ“¬ Performing exports:
   report.md -> report.pdf
šŸ’æ Executing Notebook (report.md) [no execution cache found]
šŸ’¾ Adding Cached Notebook Outputs (calculations.ipynb)
šŸ“– Built calculations.ipynb in 22 ms.
šŸš€ Starting new Jupyter server
šŸŖ Jupyter server started
   http://localhost:8888/?token=3ce1d702a24b5985ac5c53a9717a45c105d6b0eb03cfc90d
Starting WebSocket: ws://localhost:8888/api/kernels/d2db867b-6de2-4d70-9f6f-a91bb3566705
ā›”ļø report.md An exception occurred during expression evaluation, halting further execution:

NameError: name 'calculation_result' is not defined

šŸ“– Built report.md in 3.15 s.
...

It would be extremely powerful to have the ability to embed variables like this in markdown files and not only notebooks šŸš€