Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

How can I avoid generating the same code repeatedly when using definitions_?

30 views
Skip to first unread message

ziyang

unread,
May 8, 2025, 11:27:51 AMMay 8
to Blockly
I have two kinds of block that all need to import the math python module
// in A Block
 (pythonGenerator as any).definitions_['import_math'] = 'import math';
 // in B Block
 (pythonGenerator as any).definitions_['import_math'] = 'import math

if these two blocks all exist in my workspace, the output code will be 
`
import math

import math

... ...(some code)
`

I just want to import math module once, how to get rid of another?

Neil Fraser

unread,
May 8, 2025, 11:33:22 AMMay 8
to blo...@googlegroups.com
The definitions_ map is specifically designed to prevent duplications.  And the code you posted looks like it is correct.  Double-check that you don't have any typos.

Here's a demo showing that in the python code there's only a single 'import math': https://blockly-demo.appspot.com/static/demos/code/index.html#dgc8mj

--
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 visit https://groups.google.com/d/msgid/blockly/40627b7d-309e-4f6f-8368-8c856b941e77n%40googlegroups.com.


--
Neil Fraser, Switzerland
https://neil.fraser.name
Reply all
Reply to author
Forward
0 new messages