Code automatically at the top

343 views
Skip to first unread message

Björn T

unread,
Jul 23, 2018, 12:59:32 PM7/23/18
to Blockly
I am working on a Python version of Blockly. In my version every Block should look like real Python Code, so that the students learn the Python words like "for i in range..." during the lesson.

My questions:
1) I have a "from modul import *" Block. The corresponding code should be at the top like the initializiation of a variable (e.g. i=None). How can i achieve it?
2) Which generator file have to be modified and how, so that the variables are not initialized at the top? Python doesn't need that.

Andrew n marshall

unread,
Jul 23, 2018, 1:44:05 PM7/23/18
to blo...@googlegroups.com
Regarding the import statements,  you could try Generator.provideFunction_(). It isn't exactly what the function was intended for, but I did a quick test and it does look like the code is also inserted at the top, above other code, without duplication. Look at the example usage in the list_sort Python generator.

Your code might look like:
Blockly.Python.provideFunction_('import_modul', ['from modul import *;'])

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Björn T

unread,
Jul 23, 2018, 2:51:20 PM7/23/18
to Blockly
Thanks Andrew for your help!

This is close to what i am searching, but now there are two lines in the code: "from modul import *" at the top but a second line with "import modul" in the code. I just need the one line at the top without the second line.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.

Andrew n marshall

unread,
Jul 23, 2018, 6:02:37 PM7/23/18
to blo...@googlegroups.com
I think you may be returning the 'import' line from the generator function, in addition to generating it via the provideFunction_(..) call.


The following worked for me:

Blockly.defineBlocksWithJsonArray([

 {

   "type": "import_modul",

   "message0": "import modul",

   "previousStatement": null,

   "nextStatement": null

 }]);


Blockly.Python['import_modul'] = function(block) {

// Insert import at the top of the file.

 Blockly.Python.provideFunction_(

     'import_modul', ['from modul import *;'])

 return ''; // Does not generate inline code.

};



To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.
Message has been deleted

Björn T

unread,
Jul 24, 2018, 12:44:14 AM7/24/18
to Blockly
Thats it! In Addition, its inserted just once in the code even if i am inserting the block twice. Thats perfect for imports. Thank you Andrew.  

Any idea how i can getting rid of the initialization of the variables (i = None)?

Rachel Fenichel

unread,
Jul 24, 2018, 2:34:02 PM7/24/18
to Blockly
Variable initialization happens here.  It just makes a list of every variable in use and outputs "var <name> = None" for each one of those.  The resulting string goes into the generated code.  

You can modify that in the provided generator file, or you can make your own generator that extends our python generator and only override that one file.  The second option is better because you won't change core code.

Björn T

unread,
Jul 25, 2018, 11:50:59 AM7/25/18
to Blockly
Thank you Rachel, thats exactly what i am searching. 

I am interested in the best practice method you metioned. I am copied the python.js file and commented that part out. Is that your suggestion?

Rachel Fenichel

unread,
Jul 25, 2018, 12:50:43 PM7/25/18
to Blockly
Copying the python.js file and commenting out the code you don't want is fine, as long as you're not also including the original python.js.
Reply all
Reply to author
Forward
0 new messages