# Numpy with Trinket and-or with Python mode

**URL:** <https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409>\
**Category:** Processing.py\
**Created:** [March 28, 2020, 6:14pm UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409 "2020-03-28T18:14:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [March 28, 2020, 6:14pm UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409/1 "2020-03-28T18:14:56Z")

</div>

I have been trying to use numpy with Processing on trinket but it seems that most of the basic functions are either not available or not working.

ex:

```auto
arr[1:H-1][0:W-2] + arr[1:H-1][2:W] // addition of 2D matrices doesn't work
np.linalg.norm(vec) // simple normalization doesn't work

//for normalization better import math module instead

from math import hypot

vec = np.array([x, y]) 
normalized_vec = vec / abs(hypot(x, y))

```

Though, it still can be useful when simple PVector operations are needed

ex:

```auto
PVector(20, 80) -> np.array([20, 80])

vec = np.array([2, 4])
vec * 3 // np.array([6, 12])
vec / 2 // np.array([1, 2])
vec + vec // np.array([4, 8])
...

```

_test_

https://trinket.io/embed/python/dcc4be3c66?outputOnly=true&runOption=run

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 28, 2020, 6:18pm UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409/2 "2020-03-28T18:18:13Z")

</div>

@solub – do you think upgrading processing.py [to Jython 2.7.2 final](https://github.com/jdf/processing.py/issues/351) would matter? can you tell if trinket is using numpy 13.3, the recommended version for maximum JyNI compatability?

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [March 28, 2020, 6:50pm UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409/3 "2020-03-28T18:50:43Z")

</div>

I thinks it really depends on the quality of JyNI’s numpy implementation.

If it is as limited as the version that trinket is using for Python 2.7 then the upgrade is clearly not worth it. On the contrary, a more mature and stable implementation would bring **a lot** (a lot!) to the table.

I don’t know what version of numpy is currently being used by trinket (Python 2.7) but it is far from being workable: simple matrix operations return errors, basic functions (that are available in ver. 13.3) like `zeros_like()` or `meshgrid()` are missing, slicing is deficient…

I think the problem is not the version (13.3 is more than decent) but the relative exhaustiveness of the implementation.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 28, 2020, 6:55pm UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409/4 "2020-03-28T18:55:39Z")

</div>

@solub’s sketch w/ its source code as well: 👼

https://trinket.io/embed/python/dcc4be3c66?toggleCode=true&runOption=run&start=result

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 28, 2020, 7:12pm UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409/5 "2020-03-28T19:12:45Z")

</div>

Looks like Trinket has numpy 1.15.4 installed. Not sure if that is the same version available for Trinket’s processing.py, because `numpy. __version__ ` and `numpy.version.version` don’t work in its processing.py sketch window – although they do in a general python window?

- [https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-1.php](https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-1.php)

15.4 numpy is bad news for JyNI, which has many known problems above 13.3.

Also, it looks like Trinket uses Skulpt to host their generic Python (python2) sketches. Not sure how that relates to how they serve python mode.

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [March 28, 2020, 8:19pm UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409/6 "2020-03-28T20:19:04Z")

</div>

– GoToLoop: I don’t really like to have the code alongisde the canvas. It always feels like we only get to see half of the code and half of the output. Having the text editor above or below the canvas would make more sense in my opinion, especially when embedding in a forum post. I might prefer leaving the full width to the canvas and pasting the corresponding code beneath.

– JeremyDouglass: My intuition is that 15.4 is the version used in Python 3 Trinkets not in processing.py Trinkets. The 2 versions are like night and day.

A few questions:

- Is upgrading Processing.py to Jython 2.7.2 on the roadmap ? (regardless of JyNI)
- Is it “difficult” (time consuming, complex) to make the upgrade ? (How likely is it to happen ?)
- How 15.4 numpy in Trinket would be bad news for JyNI and its numpy 13.3 support ?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 28, 2020, 11:48pm UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409/7 "2020-03-28T23:48:08Z")

</div>

> [@solub](#):
>
> It always feels like we only get to see half of the code and half of the output.

That’s true! But then you could leave another link just for the source code.  
I had to use browser’s inspection in order to find out your source code’s link.

P.S.: Just found out the embed option “toggleCode=true” which allows us to toggle display & code! 🥳

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 29, 2020, 1:40am UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409/8 "2020-03-29T01:40:37Z")

</div>

> [@solub](#):
>
> Is upgrading Processing.py to Jython 2.7.2 on the roadmap ?

No, in the sense that it doesn’t have a roadmap – it is in maintenance. If a developer got involved, upgraded it and tested it, I think jdf (Jonathan Feinberg) would probably accept the patch and make a new release – or / in the meantime the mode could just be released from someone’s fork and manually installed.

> [@solub](#):
>
> Is it “difficult” (time consuming, complex) to make the upgrade ? (How likely is it to happen ?)

I honestly don’t know. It might be an easy drop-in replacement, or it might be a lot of work.

> [@solub](#):
>
> How 15.4 numpy in Trinket would be bad news for JyNI and its numpy 13.3 support ?

Well, 15.4 numpy would be bad news **for JyNI** , which Jython needs to load numpy. My understanding is that the latest JyNI (2.7-alpha5) is best able to load numpy 1.13.3. Later numpy breaks it [Jython 2.7.2, NumPy advice · Issue #37 · Stewori/JyNI · GitHub](https://github.com/Stewori/JyNI/issues/37)  
This is just based on what I have read, not experience.

That said, it looks like perhaps Trinket is using a hand-crafted partial numpy replacement running on Skulpt, since their target is JS, not JVM. I can’t tell. So that would have a completely different set of limits.

> **[GitHub - trinketapp/skulpt\_numpy: Partial numpy implementation for skulpt](https://github.com/trinketapp/skulpt_numpy)**
>
> Partial numpy implementation for skulpt. Contribute to trinketapp/skulpt\_numpy development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [April 21, 2020, 9:39pm UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409/9 "2020-04-21T21:39:41Z")

</div>

Hi @solub, I don’t have the necessary skills to test it, but I think that another ‘road’ to the ‘promissed land of numpy’ would be to access Processing’s `PApplet` class using Python 3 + PyJNIus ([https://github.com/kivy/pyjnius](https://github.com/kivy/pyjnius)) as one would using Java in Eclipse ([https://happycoding.io/tutorials/java/processing-in-java](https://happycoding.io/tutorials/java/processing-in-java)).

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [April 22, 2021, 10:26am UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409/10 "2021-04-22T10:26:24Z")

</div>

Sorry for bumping the thread.

I just found about [jni4net](http://jni4net.com/), a bridge between Java and the .NET ecosystem. Do you think it would be possible to use it with Processing in order to access a .NET binding for Numpy (like [SciSharp](https://github.com/SciSharp/Numpy.NET)) ?

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [June 22, 2021, 1:48pm UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409/11 "2021-06-22T13:48:25Z")

</div>

Hi @solub !  
The mantainer thinks it’ s not ready yet for the limelight, but **py5** is the Python 3 + Processing way to go, check ou this wonderful write up by @tabreturn:

> **[Portable Thonny and py5](https://tabreturn.github.io/code/python/thonny/2021/06/21/thonny_and_py5.html)**
>
> Combine Thonny and py5 for a Processing-esque, portable Python 3 development environment.

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [June 25, 2021, 9:52am UTC](https://discourse.processing.org/t/numpy-with-trinket-and-or-with-python-mode/19409/12 "2021-06-25T09:52:22Z")

</div>

Hey @villares thank you for the poke !  
Yes, I had a quick peek at it and it looks really promising ! I can’t wait to have some time on my hands to try it out.

Also thank you to @tabreturn for this very comprehensive write up, and to @monkstone for the helpful examples. This small Python-oriented Processing community brings joy to my heart.
