Flask - Server side JSON to Python block generation

200 views
Skip to first unread message

TheFoxCoder

unread,
Nov 11, 2023, 1:50:46 PM11/11/23
to Blockly
Hey Everyone,

I hope you're all doing well! I've been diving into Blockly within my Flask app and, while I've got things up and running with custom blocks, I'm facing a bit of a puzzle on the server-side code generation.

I've been exploring Blockly Headless, but I hit a few roadblocks, especially given my Python environment. It seems like the JavaScript generator is the key, but I'm not quite sure how to integrate it into my Flask setup on the server side.

Just to add a bit more context, I'm using the JSON format for the Blockly elements.

I've successfully implemented Blockly on the client side, but now I'm looking for a way to leverage the JavaScript generator for code generation on the server using Python

If anyone has experience or insights into using Blockly's JavaScript generator on the server side within a Flask app, I'd greatly appreciate your guidance!

Sharing code snippets or pointing me in the right direction would be incredibly helpful.
Thanks alot :D

Cheers,
TheFoxCoder

Maribeth Moffatt

unread,
Nov 13, 2023, 2:28:05 PM11/13/23
to Blockly
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

TheFoxCoder

unread,
Nov 14, 2023, 12:40:45 AM11/14/23
to blo...@googlegroups.com
Hi there,

Thanks for reaching out!! 
I've been making progress on my project and wanted to share more details with you. I'm working on a Flask web app that lets users create workflows using a Blockly interface. They can save their work as JSON files, load existing ones, or sync with the live platform.

Now, here's where I could use some advice. I'm trying to set up a separate Python environment outside the Flask app to handle the execution of Python code created in the Blockly interface. The idea is to send this code to another server for runtime processing. However, I'm a bit stuck on the best way to securely generate the code from the Flask app & then pass it to this runtime server.

I've been thinking about using a Node.js server, but I'm not entirely sure how to smoothly integrate it with my Flask app. I also tried running Blockly as a standalone JS file, but that didn't go out as expected. I'm wondering if it's even possible to use local modules like compressed Blockly, Python, and my custom block definitions within a standalone JS file for code generation.

I'm sorry if I'm bombarding you with questions, but I'm feeling a bit stuck and any guidance or insights you could provide would be incredibly helpful.

Thanks a bunch,
TheFoxCoder

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/64cd7873-c21d-421b-b0ba-4acd3ae51f18n%40googlegroups.com.

Maribeth Moffatt

unread,
Nov 14, 2023, 2:09:29 PM11/14/23
to Blockly
Hi,

Since you want to generate Python code, you'll need the Python generator. If you have a node.js server, you can run Blockly in headless mode, take the user's JSON representation of their workspace and load it onto your headless workspace, and then generate Python from that workspace using the generator.

I'm not sure what you mean by "running Blockly as a standalone JS file." If you can clarify that, I'd be happy to try to help.

Unfortunately, I can't make recommendations on how to run the generated Python code or how to connect your flask app to a node.js server. Those kinds of tasks are outside the scope of Blockly and would be best supported in another forum, perhaps one focused on python development.

Best,
Maribeth

TheFoxCoder

unread,
Nov 17, 2023, 11:42:36 AM11/17/23
to Blockly
Sure!
the standalone JS file i was reffering to was about having a JS function not depending on the HTML, 
means a javascript file which isnt connected to a html directly, how they use to be (e.g script imports such as blockly compressed throught the html from unpkg)
But using a node.js server is a great idea, thanks! But how will i integrate that to my existing flask app... ?
Thanks and have a good weekend
Yours sincerely, TheFoxCoder

Maribeth Moffatt

unread,
Nov 17, 2023, 4:59:41 PM11/17/23
to Blockly
Hi,

To run JavaScript, you need some kind of runtime environment. That is usually either the web browser or Node.js. If you use Node.js you don't need html.

Unfortunately I'm unable to provide recommendations on how to connect your flask server to a node.js server. Since that topic isn't related to blockly you would get the best advice elsewhere.

Best,
Maribeth

TheFoxCoder

unread,
Nov 18, 2023, 7:54:18 AM11/18/23
to Blockly
Thanks alot, i really appreciate your help.
I look forward of using a Node.js server.

Have a nice Weekend,
TheFoxCoder

Reply all
Reply to author
Forward
0 new messages