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

Cannot eval variable from notebook in markdown file #1557

Open
mortenengen opened this issue Sep 30, 2024 · 0 comments
Open

Cannot eval variable from notebook in markdown file #1557

mortenengen opened this issue Sep 30, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@mortenengen
Copy link

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:

  • mystmd v1.3.11
  • windows

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 🚀

@mortenengen mortenengen added the bug Something isn't working label Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant