matplotlib / matplotlib/pytest-mpl

Generalize pytest-mpl to support non-Matplotlib visual output

Ouverte
#201 6 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
Python
Étoiles
272
Forks
53
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

I am currently trying to use the Solara testing framework to try and do unit/regression testing of the rendering of Jupyter widgets, and I was wishing that I could just re-use all the pytest-mpl infrastructure to do figure hashes, galleries, etc. In a discussion with @Cadair we realised that it is possible, although a little ugly right now:

import ipywidgets as widgets
import matplotlib.pyplot as plt
import playwright.sync_api
import pytest
from IPython.display import display
from PIL import Image


@pytest.mark.mpl_image_compare(savefig_kwargs={"dpi": 100})
def test_scatter_solara(
    tmp_path,
    solara_test,
    page_session: playwright.sync_api.Page,
    assert_solara_snapshot,
):

    from glue_jupyter import jglue
    from IPython.display import display

    app = jglue()
    app.add_data(a={"x": [1, 2, 3], "y": [2, 3, 4]})
    scatter = app.scatter2d(show=False)
    display(scatter._layout)

    button_sel = page_session.locator("text=No selection")
    button_sel.wait_for()
    screenshot = page_session.screenshot()

    with open(tmp_path / "screenshot.png", "wb") as f:
        f.write(screenshot)

    image = Image.open(tmp_path / "screenshot.png")
    size_x, size_y = image.size

    fig = plt.figure(figsize=(size_x / 100, size_y / 100))
    ax = fig.add_axes([0, 0, 1, 1])
    ax.imshow(image)
    plt.axis("off")

    return fig

Basically one can in principle take any image output and show it in a Matplotlib figure. However this is a bit hacky, and what would be a lot nicer would be if we could generalize this plugin to allow for alternatives to what actually generates the images, with Matplotlib being just one (and given the plugin name the default) option.

To keep this simple, we could also simply allow the returned value from tests to be a PIL Image instance as this would already allow any arbitrary image generation in the tests.

Does anyone have any thoughts on this? Any reason to not at least allow PIL returns?

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par le point d’entrée existant pytest.mark.mpl_image_compare et le test d’exemple dans l’issue, puis suivez la manière dont les figures Matplotlib renvoyées sont converties pour les hashes et les galeries. Définissez le chemin de retour pris en charge sans Matplotlib, notamment PIL.Image, et vérifiez que la comparaison d’images, les hashes et les galeries fonctionnent toujours sans le wrapper Matplotlib.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
jupyter, playwright, python
Domaine
testing-qa
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.