Skip to content

Commit

Permalink
Merge branch 'master' into eval-metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored Jun 27, 2024
2 parents ba91962 + 67f1643 commit 498cad5
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 36 deletions.
33 changes: 12 additions & 21 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ on:
pull_request:

jobs:
pre-commit:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: "3.10"
- uses: pre-commit/action@v3.0.0

tests:

Expand All @@ -27,28 +16,28 @@ jobs:
matrix:
os: [ubuntu-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"]
sphinx: [">=5.0,<=7.*"] # Newest Sphinx (any)
myst-parser: [">=1.0.0,<=2.*"] # Newest MyST Parser (any)
sphinx: [">=5,<8"] # Newest Sphinx (any)
myst-parser: [">=1,<3"] # Newest MyST Parser (any)
include:
# Just check the other platforms once
- os: windows-latest
python-version: "3.10"
sphinx: "~=7.0"
myst-parser: ">=2.0"
myst-parser: "~=2.0"
- os: macos-latest
python-version: "3.10"
sphinx: "~=7.0"
myst-parser: ">=2.0"
myst-parser: "~=2.0"
# Oldest known-compatible dependencies
- os: ubuntu-latest
python-version: "3.9"
sphinx: "~=5.0"
myst-parser: "~=1.0.0"
sphinx: "==5.0.0"
myst-parser: "==1.0.0"
# Newest known-compatible dependencies
- os: ubuntu-latest
python-version: "3.12"
sphinx: "~=7.0"
myst-parser: "~=2.0.0"
sphinx: "==7.0.0"
myst-parser: "==2.0.0"

runs-on: ${{ matrix.os }}

Expand Down Expand Up @@ -91,8 +80,10 @@ jobs:
# for some reason the tests/conftest.py::check_nbs fixture breaks pytest-cov's cov-report outputting
# this is why we run `coverage xml` afterwards (required by codecov)

# TEMPORARY FIX: Disable codecov until we can get it working again
- name: Upload to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v3.1.4
if: false
with:
name: myst-nb-pytests
flags: pytests
Expand All @@ -101,7 +92,7 @@ jobs:
publish:

name: Publish to PyPi
needs: [pre-commit, tests]
needs: [tests]
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ repos:
additional_dependencies:
- importlib_metadata
- myst-parser~=2.0.0
- "sphinx~=7.0"
- "sphinx~=7.3.7"
- nbclient
- types-PyYAML
files: >
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Tags are a list of strings under the `tags` key in the cell metadata, e.g.
"cell_type": "code",
"source": ["print('hello world')"],
"metadata": {
"tags": ["my-tag1", "my-tag2"],
"tags": ["my-tag1", "my-tag2"]
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/render/format_code_cells.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ You can also wrap the output in a [`figure`](https://docutils.sourceforge.io/doc
---
mystnb:
image:
width: 200px
width: 300px
alt: fun-fish
classes: shadow bg-primary
figure:
Expand Down
2 changes: 1 addition & 1 deletion myst_nb/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def render_image(self, data: MimeData) -> list[nodes.Element]:
# TODO backwards-compatible re-naming to image_options?
image_options = self.renderer.get_cell_level_config(
"render_image_options", data.cell_metadata, line=data.line
)
).copy()
# Overwrite with metadata stored in output
image_options.update(
{
Expand Down
13 changes: 8 additions & 5 deletions myst_nb/sphinx_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ def sphinx_setup(app: Sphinx):
for name, default, field in NbParserConfig().as_triple():
if not field.metadata.get("sphinx_exclude"):
# TODO add types?
app.add_config_value(f"nb_{name}", default, "env", Any)
app.add_config_value(f"nb_{name}", default, "env", Any) # type: ignore[arg-type]
if "legacy_name" in field.metadata:
app.add_config_value(
f"{field.metadata['legacy_name']}", _UNSET, "env", Any
f"{field.metadata['legacy_name']}",
_UNSET,
"env",
Any, # type: ignore[arg-type]
)
# Handle non-standard deprecation
app.add_config_value("nb_render_priority", _UNSET, "env", Any)
app.add_config_value("nb_render_priority", _UNSET, "env", Any) # type: ignore[arg-type]

# generate notebook configuration from Sphinx configuration
# this also validates the configuration values
Expand Down Expand Up @@ -130,7 +133,7 @@ def create_mystnb_config(app):
"""Generate notebook configuration from Sphinx configuration"""

# Ignore type checkers because the attribute is dynamically assigned
from sphinx.util.console import bold # type: ignore[attr-defined]
from sphinx.util.console import bold

values = {}
for name, _, field in NbParserConfig().as_triple():
Expand Down Expand Up @@ -227,4 +230,4 @@ def add_per_page_html_resources(
return
js_files = NbMetadataCollector.get_js_files(cast(SphinxEnvType, app.env), pagename)
for path, kwargs in js_files.values():
app.add_js_file(path, **kwargs) # type: ignore
app.add_js_file(path, **kwargs) # type: ignore[arg-type]
4 changes: 4 additions & 0 deletions tests/notebooks/complex_outputs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@
]
},
"metadata": {
"image/png": {
"width": 432,
"height": 288
},
"needs_background": "light"
},
"output_type": "display_data"
Expand Down
77 changes: 77 additions & 0 deletions tests/notebooks/metadata_image_output.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/test_parser/test_complex_outputs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
<container classes="cell_output" nb_element="cell_code_output">
<container nb_element="mime_bundle">
<container mime_type="image/png">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="288" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png" width="432">
<container mime_type="text/plain">
<literal_block classes="output text_plain" language="myst-ansi" xml:space="preserve">
<Figure size 432x288 with 1 Axes>
Expand Down Expand Up @@ -244,7 +244,7 @@
<container classes="cell_output" nb_element="cell_code_output">
<container nb_element="mime_bundle">
<container mime_type="image/png">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" height="400" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<container mime_type="text/latex">
<math_block classes="output text_latex" nowrap="False" number="True" xml:space="preserve">
\displaystyle \left(\sqrt{5} i\right)^{\alpha} \left(\frac{1}{2} - \frac{2 \sqrt{5} i}{5}\right) + \left(- \sqrt{5} i\right)^{\alpha} \left(\frac{1}{2} + \frac{2 \sqrt{5} i}{5}\right)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_render_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from myst_nb.core.render import EntryPointError, load_renderer
from sphinx.util.fileutil import copy_asset_file


def test_load_renderer_not_found():
Expand Down Expand Up @@ -116,6 +117,24 @@ def test_metadata_image(sphinx_run, clean_doctree, file_regression):
)


@pytest.mark.sphinx_params(
"metadata_image_output.ipynb",
conf={"nb_execution_mode": "force"},
)
def test_metadata_image_output(
sphinx_run, clean_doctree, file_regression, get_test_path
):
"""Test configuring image attributes to be rendered from cell metadata."""
asset_path = get_test_path("example.jpg")
copy_asset_file(str(asset_path), str(sphinx_run.app.srcdir))
sphinx_run.build()
assert sphinx_run.warnings() == ""
doctree = clean_doctree(sphinx_run.get_resolved_doctree("metadata_image_output"))
file_regression.check(
doctree.pformat().replace(".jpeg", ".jpg"), extension=".xml", encoding="utf-8"
)


@pytest.mark.sphinx_params(
"metadata_figure.ipynb",
conf={"nb_execution_mode": "off", "nb_cell_metadata_key": "myst"},
Expand Down
4 changes: 2 additions & 2 deletions tests/test_render_outputs/test_complex_outputs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
plt.ylabel(r'a y label with latex $\alpha$')
plt.legend();
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="288" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png" width="432">
<section ids="tables-with-pandas" names="tables\ (with\ pandas)">
<title>
Tables (with pandas)
Expand Down Expand Up @@ -208,7 +208,7 @@
f = y(n)-2*y(n-1/sym.pi)-5*y(n-2)
sym.rsolve(f,y(n),[1,4])
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" height="400" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<container cell_index="25" cell_metadata="{}" classes="cell" exec_count="7" nb_element="cell_code">
<container classes="cell_input" nb_element="cell_code_source">
<literal_block language="ipython3" linenos="False" xml:space="preserve">
Expand Down
4 changes: 2 additions & 2 deletions tests/test_render_outputs/test_complex_outputs_latex.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
plt.ylabel(r'a y label with latex $\alpha$')
plt.legend();
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="288" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png" width="432">
<section ids="tables-with-pandas" names="tables\ (with\ pandas)">
<title>
Tables (with pandas)
Expand Down Expand Up @@ -168,7 +168,7 @@
f = y(n)-2*y(n-1/sym.pi)-5*y(n-2)
sym.rsolve(f,y(n),[1,4])
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" height="400" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<container cell_index="25" cell_metadata="{}" classes="cell" exec_count="7" nb_element="cell_code">
<container classes="cell_input" nb_element="cell_code_source">
<literal_block language="ipython3" linenos="False" xml:space="preserve">
Expand Down
21 changes: 21 additions & 0 deletions tests/test_render_outputs/test_metadata_image_output.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<document source="metadata_image_output">
<section ids="output-metadata" names="output\ metadata">
<title>
Output metadata
<container cell_index="1" cell_metadata="{'tags': ['skip-execution']}" classes="cell tag_skip-execution" exec_count="1" nb_element="cell_code">
<container classes="cell_input" nb_element="cell_code_source">
<literal_block language="ipython3" linenos="False" xml:space="preserve">
# Outputs included with width/height in output metadata,
# cell is not executed
from IPython.display import Image
Image(filename="./example.jpg", width=500, height=100)
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" height="100" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg" width="500">
<container cell_index="2" cell_metadata="{}" classes="cell" exec_count="1" nb_element="cell_code">
<container classes="cell_input" nb_element="cell_code_source">
<literal_block language="ipython3" linenos="False" xml:space="preserve">
# No outputs, cell is executed, image should have original size (370, 254)
from IPython.display import Image
Image(filename="./example.jpg")
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">

0 comments on commit 498cad5

Please sign in to comment.