2

I was following the example Blockly Code Generators and was able to generate Python codes. But when I run the Python code, I get an error. It seems the error is 'eval(code)' below, what should I do if I want to execute the Python code right inside the browser? Thanks for any help!

Blockly.JavaScript.addReservedWords('code');
var code = Blockly.JavaScript.workspaceToCode(workspace);
try {
  eval(code);
} catch (e) {
  alert(e);
}

here is the snapshot Unfortunately i dont have enough points to post the image here

ShungChing
  • 153
  • 1
  • 5
  • Can you please update the question with the complete error you are getting? – Anand S Kumar Jun 16 '15 at 05:02
  • I changed all Blockly.JavaScript.xyz... to Blockly.Python.xyz..., as suggested by the linked tutorial above. I got the following error when I run the generated code: JavaScript Alert: Syntax Error: Unexpected number. – ShungChing Jun 16 '15 at 14:38
  • Please update that in the question, also, what is the 'code' that you are trying to run? – Anand S Kumar Jun 16 '15 at 14:41
  • @Anand, thanks for the reply. I just took a snapshot and post it as a link, i just found a free image upload site, please ignore everything else in the site – ShungChing Jun 16 '15 at 15:10
  • 2
    You can't directly run the python code generated by Blockly. You are trying to use Javascript to execute python, which simply wont work. If you really want to run the python code generated by blockly you need to investigate something like [Brython](http://www.brython.info/) – TheIronKnuckle Sep 02 '15 at 03:09
  • Check out [skulpt](https://skulpt.org/) or [py-script](https://pyscript.net/). – ggorlen Jun 29 '22 at 20:45

3 Answers3

1

Can you try this with a simple code , like - print('Hello World!')

According to the image , the issue could be with indentation , and indentation is very important in python, othewise it can cause syntax errors .

You should have also changed the code to -

Blockly.Python.addReservedWords('code');
var code = Blockly.JavaScript.workspaceToCode(workspace);
try {
  eval(code);
} catch (e) {
  alert(e);
}
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • I am trying to use Blockly to generate Python code. The code seems OK. I removed the whole if block and replace it with a simple 'print'hello' blocks, when i run the code, it actually try to print the whole web page as an image to a printer – ShungChing Jun 16 '15 at 15:17
  • I change back to the original if block and change 6*7==42 to a single True block. Now when I run the code, it say "Unexpected Identifier" – ShungChing Jun 16 '15 at 15:22
  • Have you changed the code like i indicated in my update? – Anand S Kumar Jun 16 '15 at 15:28
  • i changed the code as you suggested, now it does not give me any error when i run the code, and does not try to print the whole web page as an image to a printer. But it does not do anything either. So I am not sure if the code is running now. Can you suggest how can I see the printed output (may be via an JavaScript Alert r the like)? Thanks! – ShungChing Jun 16 '15 at 15:30
  • yes, i did, i am trying to upload the file, here is the link: http://textuploader.com/nytt i am not sure if you can see the text file, the file is basically the original Blockly code generator demo file PLUS your code (see the runCode at the bottom) – ShungChing Jun 16 '15 at 15:44
  • Also , If the above doesn't work, I moved one of the lines from `Blockly.Python` back to `Blockly.JavaScript` , can you check if it works now? – Anand S Kumar Jun 16 '15 at 16:12
  • (i was suggested to move discussion to chat, but i dont have reputation to chat) I tried different approach to no avail, is the 'eval()' function for JavaScript code only? how do we invoke the Python's corresponding version? – ShungChing Jun 16 '15 at 16:23
  • I just checked all over internet, I believe for eval to work, it has to be java script code. But `var code = Blockly.JavaScript.workspaceToCode(workspace);` should be returning java script code from python code. – Anand S Kumar Jun 16 '15 at 16:38
  • Thanks for the effort! Blockly.Python.workspaceToCode(workspace) does generate the correct Python code from the blocks. The question seems to be how to execute the resulting Python code. May be it requires external Python runtime not provided by Blockly? – ShungChing Jun 16 '15 at 16:48
  • no , do `var code = Blockly.JavaScript.workspaceToCode(workspace);` `JavaScript` and then do `alert(code)` , and then do `eval(code)` – Anand S Kumar Jun 16 '15 at 16:49
0

Try to eval with a Python interp written is JS, like http://brython.info/.

Gra
  • 1,542
  • 14
  • 28
0

as you using eval function of javascript you are actually saying to print the page as print() means to print the page in javascript.

The solution to this is there in the Blockly itself as to rn the code they call this function.

   function() {
  Blockly.JavaScript.INFINITE_LOOP_TRAP = 'checkTimeout();\n';
  var timeouts = 0;
  var checkTimeout = function() {
    if (timeouts++ > 1000000) {
      throw MSG['timeout'];
    }
  };
  var code = Blockly.JavaScript.workspaceToCode(Code.workspace);
  Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
  try {
    eval(code);
  } catch (e) {
    alert(MSG['badCode'].replace('%1', e));
  }

The above code is from code.js in demos/code/code.js line 553.