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

Matplotlib output using pyodide.runPython() #12

Open
hemant-hari opened this issue Jun 25, 2019 · 1 comment
Open

Matplotlib output using pyodide.runPython() #12

hemant-hari opened this issue Jun 25, 2019 · 1 comment

Comments

@hemant-hari
Copy link

I hope this is the right place to put this - how do I access the plots made by the matplotlib package? I am using the pyodide package independent of iodide and I don't know how to use the plots made by matplotlib. I have a similar problem to pyodide/pyodide#365 but it never received a response. Thanks in advance!

@personalizedrefrigerator

If self-building Pyodide...
Consider subclassing FigureCanvasWasm in pyodide/packages/matplotlib/src/wasm_backend.py and overriding the create_root_element method to create a custom output element.
It currently reads:

    # Around line 100 on Nov. 4, 2019...
    def create_root_element(self):
        # Designed to be overridden by subclasses for use in contexts other
        # than iodide.
        try:
            from js import iodide
            return iodide.output.element('div')
        except ImportError:
            return document.createElement('div')

If you do create a custom class, be sure to change the initialization code at the end of wasm_backend.py:

# Around line 580 on 11/4/19
@_Backend.export
class _BackendWasmCoreAgg(_Backend):
    FigureCanvas = FigureCanvasWasm # Change this!
    FigureManager = FigureManagerWasm

    @staticmethod
    def show():
        from matplotlib import pyplot as plt
        plt.gcf().canvas.show()

If unable to modify the source...
Given the section of the source included above, it looks like defining some object, iodide, in JavaScript that contains another object, output, with method, element, returning the desired element should work.
For example,

    /**
     *  Dummy object to act like that used by Iodide.
     * Only do this if you need to! Bugs related to
     * the successful import of this object might
     * appear!
     */
    window.iodide = 
    {
        output:
        {
            // Create a new element with tagName
            // and add it to an element with id "root".
            element: (tagName) =>
            {
                let elem = document.createElement(tagName);

                document.querySelector("#root").appendChild(elem);

                return elem;
            }
        }
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants