g9.py
automatically interactive graphics - in python?

Welcome to the g9py gallery! Drag all the graphics.

This is a shameless copy of g9.js, written in Python. This prototype shows off some of the cool things you can do with Pyodide for running Python demos in the browser with minitorch.

What is this?

In g9.py you write a little renderer as a standard Torch NN. It then is optimized to match the movement of the users movment. In order to update the graphics we use backpropagation. In theory any parameterized NN function could be used this way.

The examples and text here is adapted directly from the g9.js examples page.

Basic

A minimal example with only two points. Our render function always draws one point at (x, y), and the other point at (y, x), so when you drag one of the points, the other kind of mirrors it. You do this by making a class where the parameters can change. The model returns of dictionary of svg objects to render. Dragging points only changes the parameters of the module.

Rings

Lets add a few more points and use a bit or trigonometry to arrange them into two circles. Just for fun, we make the inner points red. The target argument tells us which of the points we need to calculate on the forward pass, which helps with efficiency. Try dragging the points!

The affects option

All of the default shapes in g9 accept an affects option, which tells them what parts of the data they can change. Here are three lines with different values of affects.

tongs

Using Max and Min as limits

dragon

This one is a little slow at the moment because of minitorch overhead, but it is still neat.

tree