PyScript - Real Python In The Browser

51 views
Skip to first unread message

Thomas Passin

unread,
Dec 3, 2022, 8:55:42 AM12/3/22
to leo-editor
A new project named PyScript, built on PyIodide, runs Python in the browser.  It can actually run many of the most important scientific packages such as matplotlib, numpy, scipy, etc., and do graphics with Bokeh and others.  This might be an alternative route to Leo in a browser.


I tried an example from a PyScript "Getting Started" tutorial, one that uses matplotlib to plot some random numbers.  I copied the code below verbatim into a Leo node, opened viewrendered3, and the plot rendered in VR3.  There was some delay while all the imported code got compiled to WebAssembly, downloaded, and initialized.

<html>
    <head>
      <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
      <script defer src="https://pyscript.net/latest/pyscript.js"></script>
    </head>

  <body>
    <h1>Let's plot random numbers</h1>
    <div id="plot"></div>
    <py-config type="json">
        {
          "packages": ["numpy", "matplotlib"]
        }
    </py-config>
    <py-script output="plot">
      import matplotlib.pyplot as plt
      import numpy as np
      x = np.random.randn(1000)
      y = np.random.randn(1000)
      fig, ax = plt.subplots()
      ax.scatter(x, y)
      fig
    </py-script>
  </body>
</html>

Edward K. Ream

unread,
Dec 3, 2022, 9:05:01 AM12/3/22
to leo-e...@googlegroups.com
On Sat, Dec 3, 2022 at 7:55 AM Thomas Passin <tbp1...@gmail.com> wrote:
A new project named PyScript, built on PyIodide, runs Python in the browser.  It can actually run many of the most important scientific packages such as matplotlib, numpy, scipy, etc., and do graphics with Bokeh and others.  This might be an alternative route to Leo in a browser.


Thanks for this link!  Hard to keep up with all the splendid things in the Python world.

I tried an example from a PyScript "Getting Started" tutorial, one that uses matplotlib to plot some random numbers.  I copied the code below verbatim into a Leo node, opened viewrendered3, and the plot rendered in VR3.  There was some delay while all the imported code got compiled to WebAssembly, downloaded, and initialized.

Excellent. That's very cool!

Edward

Edward K. Ream

unread,
Dec 3, 2022, 9:11:50 AM12/3/22
to leo-e...@googlegroups.com
On Sat, Dec 3, 2022 at 7:55 AM Thomas Passin <tbp1...@gmail.com> wrote:


I tried an example from a PyScript "Getting Started" tutorial, one that uses matplotlib to plot some random numbers.  I copied the code below verbatim into a Leo node, opened viewrendered3, and the plot rendered in VR3.  There was some delay while all the imported code got compiled to WebAssembly, downloaded, and initialized.

The VR3 pane shows "Let's plot random numbers", but the "loading runtime" message never goes away. Presumably a firewall issue.

Edward

Thomas Passin

unread,
Dec 3, 2022, 12:03:35 PM12/3/22
to leo-editor
When I tried it,  the plot did finally appear, but after a much longer wait time than I expected after seeing that message.  For a long-running GUI program (as a Leo implementation would be), a long initial wait would be annoying but once it was over, the program should interact normally.

Also, in daily use one would probably copy the PyScript javascript scripts, package files, and the WebAssembly compile resources to one's own computer.  This should make startup much faster.

David Szent-Györgyi

unread,
Dec 9, 2022, 11:51:12 AM12/9/22
to leo-editor
An article on change coming with an upcoming update to the Dart language mentions that there is as yet no standard for garbage collection for WebAssembly. A group working on that has been chartered.  From the article:

"Following the release of Dart 3, the next significant milestone for the language is likely to be support for compiling Dart code into WebAssembly (Wasm), which will allow Flutter Web apps to run as native code in browsers. That effort requires cooperation from the W3C and browser vendors to add support for garbage collected languages (like Dart) to Wasm by way of the WasmGC extension."
Reply all
Reply to author
Forward
0 new messages