Code Generation with JSON.

516 views
Skip to first unread message

Murali Manohar Varma

unread,
Jan 10, 2023, 6:22:56 AM1/10/23
to Blockly
Hi Blockly Team,

With latest improvements to the new blockly, it is recommended to use JSON for defining new blocks and exporting workspace in JSON and importing workspace in JSON. I think this is a great way to move forward, since JSON is much more flexible in JavaScript world.

I observed something, in order to complete the blockly, using blocks and defining blocks is only half of the story. Using the code generated through blocks is actually the heart of blockly.

Is there a way i can define the generators through JSON, instead of JavaScript functions. Because i want to take my block generation to the cloud, but only block(🚫) i encountered is that, i need to have the code generator functions in local file and import them. 

Please help me. Any suggestions are so much appreciated. 

Beka Westberg

unread,
Jan 10, 2023, 12:08:41 PM1/10/23
to blo...@googlegroups.com
Hello! Thank you for your question =)

Currently there is no way to define generators through JSON :/ But I'm a bit puzzled about why this is preventing you from moving your code generation to the cloud. Could you elaborate?

Hopefully with a bit more info we can find a solution for you =)
--Beka

--
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/c09a2f67-3d2e-47d6-b6d5-77e4e8678d6cn%40googlegroups.com.

Murali Manohar Varma

unread,
Jan 10, 2023, 3:51:20 PM1/10/23
to Blockly
Thanks beka for your quick response ❤️.

My Use case:
I have a library in python, which consists of helpful functions. 

I wrote some custom generators to convert those functions into blocks and store them in my database(JSON Format - Block definitions).

Every time i make changes to that functions or add new functions to that python library, i run my generators and add those blocks to my database.

Current Approach

Since its a JSON array to define blocks,  making that api call and adding to a file and adding the code generators(Javascript Functions) to one file for the corresponding blocks and publishing the Blockly app. So if i add new blocks to the database, i have rebuild my app and publish it. 

Desired Approach

Have a Api endpoint, get the blocks array, and code generators array and make them work. No need to make some janky local file rewrites, or have some external dependencies. Everytime i update my database, magically blocks are updated. So version releases or complex app rebuilds.


PS
Is it possible to write a plugin to extend JSON block definition to generate code through it. Just a thought 🤭
Something like
code1.png

Beka Westberg

unread,
Jan 10, 2023, 5:24:53 PM1/10/23
to blo...@googlegroups.com
Hello!

You can definitely make a separate processor that looks at the same JSON block definition if you want!

I imagine it would look something like:
```
function createBlockCodeGenerators(jsonBlockArray) {
  for (const block of jsonBlockArray) {
    Blockly.JavaScript[block['type']] = function() {
      return block['generators']['javascript'];
    };
  }
}
```

It will probably get tricky to figure out a good syntax for your 'generators' strings if you ever want to start creating blocks with inputs. But generally I think the approach is sound!

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

Murali Manohar Varma

unread,
Jan 10, 2023, 6:44:13 PM1/10/23
to Blockly
Excellent, thank you for your inputs. I will test some syntax changes, but overall i got some general picture on how to do that. Thank you once again beka 😊

Pavitra Golchha

unread,
Nov 22, 2023, 2:41:50 PM11/22/23
to Blockly
Using templates to generate code sounds like a good idea!
Reply all
Reply to author
Forward
0 new messages