Using ipympl logic inside python shell or ipython?

Open
#68 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
28/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
jupyter, python
Domain
backend, testing

Research direction

Start with the MyVis example and reproduce the TraitError in IPython, then compare it with the notebook setup using the %matplotlib widget magic. Check how the matplotlib canvas is selected and determine what test setup would let the pytest cases use a compatible widget canvas outside Jupyter; done means the class can be instantiated and tested without the Tk canvas error.

Written by the indexing model from the issue text.

Description

I have a class that inherits from the ipywidgets.widgets.VBox and adds a figure.canvas instance of a matplotlib figure to its children like so:

import matplotlib.pyplot as plt
from ipywidgets import widgets

plt.ioff()

class MyVis(widgets.VBox):
    def __init__(self):
        super().__init__()
        self.fig = plt.figure()
        self.children = [self.fig.canvas]

m = MyVis()

This works fine when I use this class within jupyter notebooks using the %matplotlib widget magic before executing the above code.
When executed within ipython I get
TraitError: Element of the 'children' trait of a MyVis instance must be a Widget, but a value of <matplotlib.backends.backend_tkagg.FigureCanvasTkAgg object at 0x7f0c48d731d0> <class 'matplotlib.backends.backend_tkagg.FigureCanvasTkAgg'> was specified.
since of course ipympl and its magic are not active.

Since I also want to have unit-tests for this class (and others that make use of this class) using pytest, it would be nice to get around this problem.
Can I enable the ipympl way of handling figures somehow in the python shell (similar to using the %matplotlib widget magic in the notebook) that would allow my tests to run?

Dominant language
Jupyter Notebook
Stars
1.7k
Forks
234
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from matplotlib/ipympl

All issues in matplotlib/ipympl

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.