How to use Python Mode for Processing in the Trinket.io Web Based Coding Environment

This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io

Quick little how-to on running processing code in the trinket.io web based on coding environment. You’ll need to add three extra lines of code to your project that aren’t normally necessary when programming in a traditional python environment when working with processing on trinket.io. First remember to import sin from math and import all from processing. Last, you will need to call run() as the last line in your script (code example by Peter Farrell from his book Math Adventures with Python):

# NOTE: To run processing in the trinket.io environment,
# you need to import sin from math and import all from
# processing. Also, make sure to call run() at the very
# end of your python script!
# Code by Peter Farrell from his book
# Math Adventures with Python
from math import sin
from processing import *
#CircleSineWave.pyde
r1 = 100 #radius of big circle
r2 = 10 #radius of small circle
t = 0 #time variable
circleList = []
def setup():
size(600,600)
def draw():
global t, circleList
background(200)
#move to left-center of screen
translate(width/4,height/2)
noFill() #don't color in the circle
stroke(0) #black outline
ellipse(0,0,2*r1,2*r1)
#circling ellipse:
fill(255,0,0) #red
y = r1*sin(t)
x = r1*cos(t)
#add point to list:
circleList.insert(0,y)
ellipse(x,y,r2,r2)
stroke(0,255,0) #green for the line
line(x,y,200,y)
fill(0,255,0) #green for the ellipse
ellipse(200,y,10,10)
if len(circleList)>300:
circleList.remove(circleList[-1])
#loop over circleList to leave a trail:
for i,c in enumerate(circleList):
#small circle for trail:
ellipse(200+i,c,5,5)
t += 0.05
# Remember to call run() at the very end of your script!
run()
 

topherPedersen