title: How to redirect/render Pyodide output in browser?
body: <p>I have recently come across the <a href="https://github.com/iodide-project/pyodide" rel="noreferrer">Pyodide project</a>.</p>
  
  <p>I have built a little demo using Pyodide, but although I've spent a lot of time looking at the source, it is not obvious (yet) to me how to redirect <code>print</code> output from python (other than modifying the CPython source), and also, how to redirect output from matplotlib.pyplot to the browser.</p>
  
  <p>From the source code, <a href="https://github.com/iodide-project/pyodide/blob/master/packages/matplotlib/src/wasm_backend.py" rel="noreferrer">FigureCanvasWasm</a> does have a <code>show()</code> method with the appropriate backend for plotting to the browser canvas - however, it is not clear to me how to instantiate this class and invoke it's <code>show()</code> method or indeed, if there is another more obvious way of redirecting plots to canvas.</p>
  
  <p>My questions therefore are:</p>
  
  <ol>
  <li>How do I redirect <code>print()</code> messages</li>
  <li>How do I force pyodide to plot matplotlib figures in the browser?</li>
  </ol>
  
  <p>Here is my test page:</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;html lang="en"&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Demo&lt;/title&gt;
      &lt;script src="../../pyodide/build/pyodide.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;/body&gt;
      &lt;script type="text/javascript"&gt;
        languagePluginLoader.then(() =&gt; {
        pyodide.loadPackage(['matplotlib']).then(() =&gt; {
            pyodide.runPython(`
                    import matplotlib.pyplot as plt
                    plt.plot([1, 2, 3, 4])
                    plt.ylabel('some numbers')
                    #fig = plt.gcf()
                    #fig.savefig(imgdata, format='png')                  
                    print('Done from python!')`
            );
            //var image = pyodide.pyimport('imgdata');
            //console.log(image);
        });});
  
      &lt;/script&gt;
  &lt;html&gt;
  </code></pre>