if I want to block by block running blockly app for python dose I need make a python interpreter

147 views
Skip to first unread message

m lz

unread,
Oct 4, 2023, 8:09:06 AM10/4/23
to Blockly
hi,guys:
I want to step by step running debug my block based app (under that was python code) and I find a demo  JS Interpreter (google.github.io) dose it means I need implement my own python interpreter myself? currently I want to use jupyter notebook as my python interpreter,any suggestion was prefered? thanks

Beka Westberg

unread,
Oct 6, 2023, 12:38:16 PM10/6/23
to Blockly
Hello!

Doing step-by-step running has three parts:
1) Being able to pause interpretation of the code.
2) Knowing when to pause interpretation of the code.
3) Highlighting blocks in the workspace.

So first you need to check if (1) is true. Can Jupypter notebooks pause interpretation? If yes, then you're good. If not, then you would need to find or implement another python interpreter.

Next you need to know when to pause interpretation of the code. You probably want to pause the interpreter before each new block. So you can add a statement prefix that prepends code to each statement to let you know when to pause interpretation. Check out the step execution demo for more info.

Finally for highlighting the block, you can call `workspace.highlightBlock(blockId)` where the `blockId` is the ID of the block you want to hightlight.

I hope that helps! If you have any further questions please reply =)
--Beka

Zoey Li

unread,
Oct 8, 2023, 11:30:12 PM10/8/23
to Blockly
Thanks to Beka's tips I was able to get an actual python interpreter running!  So first things first, if you want to run blockly in a jupyter notebook you may want to look into this extension which may do just want you want in terms of using jupyter notebook as your python interpreter: https://github.com/QuantStack/jupyterlab-blockly.  Step-by-step debugging in python is more involved and you would usually use `pdb`, `breakpoint` or `sys.settrace()` as you normally would.

In order to get a JS-Interpreter like demo working, I had to get a Python interpreter working on the web.  I ended up creating the following demo: https://alicialics.github.io/blockly-samples/
It does not include the backwards-execution example but otherwise the async and step demos work pretty well.  The source code is linked in the demo.

Pausing the execution is a bit more tricky. Since the main JS thread is single threaded you'll either have to rely on per-step execution (like JS-interpreter), or run it in a worker thread in order to make blocking happen.  I ended up using the latter approach and block right after every `workspace.highlightBlock(blockId)` call.

jayce liang

unread,
Jan 14, 2024, 9:53:09 PM1/14/24
to Blockly

If you want to execute python code generated by building blocks (provided that python has established communication with the web), 
you can use the python library 'pdb' implementation



Reply all
Reply to author
Forward
0 new messages