Giter Site home page Giter Site logo

Comments (1)

personalizedrefrigerator avatar personalizedrefrigerator commented on August 31, 2024 3

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;
            }
        }
    };

from matplotlib-pyodide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.