Reputation: 3037
I've just discovered "blockly" and it is exacly what i was looking for to take my webApp to the next level. The problem i have is that i don't realy understand how i can initiate python or js code variables.
Here is my block :
Blockly.Language.myapp_ifdo = {
helpUrl: '',
init: function() {
this.setColour(210);
this.appendDummyInput()
.appendTitle("if")
.appendTitle(new Blockly.FieldDropdown([["Temperature", "Temperature"], ["Humidity", "Humidity"]]), "SENSOR")
.appendTitle(" ")
.appendTitle(new Blockly.FieldDropdown([["=", "="], ["≠", "≠"], ["<", "<"], ["≤", "≤"], [">", ">"], ["≥", "≥"]]), "OPERATOR")
.appendTitle(" ")
.appendTitle(new Blockly.FieldTextInput("0"), "SENSORVALUE");
this.appendStatementInput("DO")
.appendTitle("do");
this.setInputsInline(true);
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setTooltip('');
}
};
The rendering is :
Dropdown list content :
What i'm trying to do :
If "temperature" is selected then i want to initialize variable at the beginning of the generated code :
temperature = None
if temperature <= '30':
pass
Just the same if "humidity" is selected :
humidity = None
if humidity >= '60':
pass
In my "template.soy" file i have this :
<block type="myapp_ifdo"></block>
Hope i am clear enough... Thanks for your help !
Regards,
Upvotes: 1
Views: 3527
Reputation: 134255
The built-in python generator will take care of this for you, if you use built-in Blockly variables and other constructs.
The code demo uses the function Blockly.Generator.workspaceToCode
to generate code from the blocks. Once all the blocks have been processed, it calls into the generator's finish
function to prepend variable declarations.
You can see the finish
for yourself in python.js:
/**
* Prepend the generated code with the variable definitions.
* @param {string} code Generated code.
* @return {string} Completed code.
*/
Blockly.Python.finish = function(code) {
...
You will need to roll your own generator code if you can't use the built-in constructs from Blockly. You may be able to use this code from Blockly as a starting point, although it will be tricky because you will need to maintain your own list of variable declarations.
Upvotes: 2