Create Custom Python Blocks

1,149 views
Skip to first unread message

Sathya Dayanithi

unread,
May 31, 2021, 1:03:46 AM5/31/21
to Blockly
Hi All,

I am planning to create custom blocks for python (like dictionaries, sets, tuples). what will be the best way to implement this. I also tried to add custom blocks through Blockly developer tools to design those custom blocks (and i am not sure on the part to implment the logic there.) So is there any other way this can be done?

Beka Westberg

unread,
May 31, 2021, 5:33:13 PM5/31/21
to blo...@googlegroups.com
Hello,

There are two different ways you can define blocks (regardless of the language you're generating).
  1) You can define them using Javascript
  2) You can define them using JSON
The blockly developer tools just help you create one of these representations =)

Then you must separately create a block-code generator for the block. In your case it should generate Python.

If you want to create dictionaries, sets, and tuples, you will probably also want to add mutators to your blocks, so that the blocks can have a variable number of inputs. But you can always save that bit for later hehe.

All of your blocks should either be defined inside of <script> tags in your html page, or included in a separate .js file, which you import using a <script> tag. You may also want to check out the getting started codelab if this is your first time using blockly.

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

On Sun, May 30, 2021 at 10:03 PM Sathya Dayanithi <sat...@wiselywise.com> wrote:
Hi All,

I am planning to create custom blocks for python (like dictionaries, sets, tuples). what will be the best way to implement this. I also tried to add custom blocks through Blockly developer tools to design those custom blocks (and i am not sure on the part to implment the logic there.) So is there any other way this can be done?

--
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/d2deba2f-8627-4893-8a7f-d638b30b7ce5n%40googlegroups.com.

Sathya Dayanithi

unread,
Jun 1, 2021, 5:03:54 AM6/1/21
to Blockly
Hi,

The thing is i already took up the code from developer tools(Block exporter) for generator stubs as well as block definitions. For each of the block the logic of the code should be implemented in python. But should i write it in javascript? I need some help around the logic design of the code generation part of the custom blocks.

download (28).png

Beka Westberg

unread,
Jun 1, 2021, 4:28:34 PM6/1/21
to blo...@googlegroups.com
Oh yeah! All of the block definitions have to be written in JS or JSON, and the block-code generators have to be written in JS. This is true even if you generate another language (like python, dart, c++, etc).

> I need some help around the logic design of the code generation part of the custom blocks.

Well first of all, I can point you to this docs page about it =) And I'll also recommend you checkout the built-in python block-code generators, for example the ones for logic blocks. But could you provide a bit more detail about what you're looking for information about? I don't want to flood you with a bunch of stuff you already know hehe.

I hope that gives you some places to start!
--Beka

Reply all
Reply to author
Forward
0 new messages