Hello,
It is a bit confusing to talk about both the code and the code that generates code at the same time, so bear with me. I'll start by defining some terms, so apologies if this is redundant to you.
Blockly is a JavaScript library, so all of the code related to running Blockly has to be executed in a JavaScript runtime. A code generator takes blocks on the Blockly workspace and converts them to a specific programming language, outputting the code for the blocks as a string. The JavaScript generator outputs JavaScript code strings, the Python generator outputs Python code strings, and so on. Regardless of the output language, all generators are implemented in JavaScript and must be executed with JavaScript.
Typically the way headless mode would be used is, you have a front-end web app where users can assemble blocks. You have a back-end server where you want to generate and do something with code. In the web app, you
save your user's workspace state as json data. You send that data to your server. Using headless mode, you can load that data to get an exact reconstruction of your user's workspace, but now on your server instead of in their web browser. Now you can use any of the generators to generate code from the headless workspace. What you do with that code afterwards is outside the scope of Blockly's involvement. You might run the code in some kind of sandboxed environment, or insert it into a Minecraft server, or something else.
I'm not sure if you want to
A) have a Python server that generates JavaScript code
B) generate Python code in your app and then run that Python code on your flask server
If A, then you need to figure out how to execute JavaScript code on your server. Since the library is written in JavaScript, you need to be able to run JavaScript code. Trying to do that in Python is outside the scope of Blockly so I'm unable to assist with that unfortunately.
If B, then you'd follow the flow I outlined above. Running the python code generated by a user in an app is also outside the scope of Blockly but I will note that any time you are running code generated by a user you are in dangerous territory and want to be sure the code is sandboxed as protection against malicious code. I'm unable to provide recommendations for how to do that safely in python however.
Does that help? If you could clarify your goals here then that would help us provide a more detailed response as well.
Best,
Maribeth