1

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 :

enter image description here

Dropdown list content :

enter image description here

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,

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
user2196728
  • 2,915
  • 3
  • 16
  • 15
  • Sorry for the confusion, but are you just doing this to prevent syntax errors if the variable happens to be undeclared? Also, blockly has `if`, equality operators, and variables built-in, so you do not need to roll your own. – Justin Ethier Sep 10 '13 at 19:23
  • I know that blockly has built-in variable generator, but i can't find the way to use it in my case. In demos provided, variables are automatically initiliazed, i just want to do the same to generate clean code ! – user2196728 Sep 10 '13 at 19:27
  • Have you tried running `Blockly.Generator.workspaceToCode('Python');` like the `code` demo application does? The built-in generator should take care of setting these declarations up for you. – Justin Ethier Sep 10 '13 at 19:35
  • seen this but don't know where to write it in my code – user2196728 Sep 10 '13 at 19:38
  • You should be able to call `workspaceToCode` directly from your application code. Exactly how to do that depends on your app, whether you are sending the code to the back-end using AJAX, displaying it to the user, etc... – Justin Ethier Sep 10 '13 at 19:49
  • FYI, the best place to get answers to questions about Blockly is the support group [https://groups.google.com/forum/#!forum/blockly], although @JustinEthier did a fine job here. – Ellen Spertus Nov 30 '13 at 01:19

1 Answers1

2

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.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • You mean that i should generate the code at the end ? ok, i will try this. But if you try this demo http://blockly-demo.appspot.com/static/apps/code/index.html variable init is generated on the fly (they use var objects that i don't use) – user2196728 Sep 10 '13 at 19:51
  • @user2196728 - You can generate the code whenever you want to, my point is just that when you do so, you need to use an algorithm such as the one Blockly uses, and follow it up with a finishing pass that adds your variable declarations. – Justin Ethier Sep 10 '13 at 20:12