I’m not familiar with CodeMirror, but it looks like creating the custom mode will be the easy part (it already appears to have a Python mode you could extend upon).
I’m guessing you’d like to create something like this?
https://trinket.io/processing
For ideas about running the code, check out this post:
https://discourse.processing.org/t/running-python-code-in-a-browser/236/6