研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
生成和运行代码
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您的应用可以随时生成代码。例如,它可能会在最终用户点击按钮时或用户每次进行更改时生成代码。
如需了解代码生成的概览,请参阅代码生成。
导入语言代码生成器
如需生成代码,您需要使用语言代码生成器。您可以使用以下任一方法导入语言代码生成器。
模块
import {javascriptGenerator} from 'blockly/javascript';
import {pythonGenerator} from 'blockly/python';
import {phpGenerator} from 'blockly/php';
import {luaGenerator} from 'blockly/lua';
import {dartGenerator} from 'blockly/dart';
// Generate code for all the blocks in the workspace.
const jsCode = javascriptGenerator.workspaceToCode(workspace);
const pythonCode = pythonGenerator.workspaceToCode(workspace);
const phpCode = phpGenerator.workspaceToCode(workspace);
const luaCode = luaGenerator.workspaceToCode(workspace);
const dartCode = dartGenerator.workspaceToCode(workspace);
Unpkg
添加 Blockly 后,您必须添加生成器。
<script src="https://unpkg.com/blockly"></script>
<script src="https://unpkg.com/blockly/javascript_compressed"></script>
<script src="https://unpkg.com/blockly/python_compressed"></script>
<script src="https://unpkg.com/blockly/php_compressed"></script>
<script src="https://unpkg.com/blockly/lua_compressed"></script>
<script src="https://unpkg.com/blockly/dart_compressed"></script>
// Generate code for all the blocks in the workspace.
const jsCode = javascript.javascriptGenerator.workspaceToCode(workspace);
const pythonCode = python.pythonGenerator.workspaceToCode(workspace);
const phpCode = php.phpGenerator.workspaceToCode(workspace);
const luaCode = lua.luaGenerator.workspaceToCode(workspace);
const dartCode = dart.dartGenerator.workspaceToCode(workspace);
本地脚本
添加 Blockly 后,您必须添加生成器。
<script src="blockly_compressed.js"></script>
<script src="javascript_compressed.js"></script>
<script src="python_compressed.js"></script>
<script src="php_compressed.js"></script>
<script src="lua_compressed.js"></script>
<script src="dart_compressed.js"></script>
// Generate code for all the blocks in the workspace.
const jsCode = javascript.javascriptGenerator.workspaceToCode(workspace);
const pythonCode = python.pythonGenerator.workspaceToCode(workspace);
const phpCode = php.phpGenerator.workspaceToCode(workspace);
const luaCode = lua.luaGenerator.workspaceToCode(workspace);
const dartCode = dart.dartGenerator.workspaceToCode(workspace);
生成代码
如需生成代码,请使用生成器的 workspaceToCode
函数。
const code = javascriptGenerator.workspaceToCode(workspace);
持续更新
借助持续更新,您可以随时在用户进行更改时显示或运行代码。生成代码是一项快速操作,因此执行此操作对性能的影响很小。这可以通过使用事件监听器来实现。
const supportedEvents = new Set([
Blockly.Events.BLOCK_CHANGE,
Blockly.Events.BLOCK_CREATE,
Blockly.Events.BLOCK_DELETE,
Blockly.Events.BLOCK_MOVE,
]);
function updateCode(event) {
if (workspace.isDragging()) return; // Don't update while changes are happening.
if (!supportedEvents.has(event.type)) return;
const code = javascriptGenerator.workspaceToCode(workspace);
document.getElementById('textarea').value = code;
}
workspace.addChangeListener(updateCode);
添加前言或后序代码
生成代码后,您可以选择在生成的代码开头或结尾添加代码。
let code = javascriptGenerator.workspaceToCode(workspace);
// Add a preamble and a postscript to the code.
const preamble = 'import {MyLibrary} from \'/path/to/my/library\';\n'
const postscript = 'MyLibrary.myKickoffFunction();\n';
code = preamble + code + postscript;
序言代码通常用于在代码开头包含外部定义。PostScript 代码通常用于调用函数,以便在代码结束时启动行为。
如果生成的代码可以按原样运行,则无需添加序言或后序言。
执行代码
生成代码后,您需要确定如何执行该代码。决定如何执行它非常依赖于应用,超出了 Blockly 的范围。
如果您想执行 JavaScript 代码,请参阅生成和运行 JavaScript。本文将介绍 JavaScript 代码生成器的一些特殊功能,以及 Blockly 团队建议用于安全执行 JavaScript 的 JSInterpreter。
其他语言需要使用其他工具。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[],[],null,["# Generate and run code\n\nYour application can generate code at any time. For example, it might generate\ncode when the end user clicks a button or every time the user makes a change.\n\nFor an overview of code generation, see [Code\ngeneration](/blockly/guides/create-custom-blocks/code-generation/overview).\n\nImport a language code generator\n--------------------------------\n\nTo generate code, you need a language code generator. You can import language\ncode generators with any of the following methods. \n\n### Modules\n\n import {javascriptGenerator} from 'blockly/javascript';\n import {pythonGenerator} from 'blockly/python';\n import {phpGenerator} from 'blockly/php';\n import {luaGenerator} from 'blockly/lua';\n import {dartGenerator} from 'blockly/dart';\n\n // Generate code for all the blocks in the workspace.\n const jsCode = javascriptGenerator.workspaceToCode(workspace);\n const pythonCode = pythonGenerator.workspaceToCode(workspace);\n const phpCode = phpGenerator.workspaceToCode(workspace);\n const luaCode = luaGenerator.workspaceToCode(workspace);\n const dartCode = dartGenerator.workspaceToCode(workspace);\n\n### Unpkg\n\nYou must include the generator after you include Blockly. \n\n \u003cscript src=\"https://unpkg.com/blockly\"\u003e\u003c/script\u003e\n \u003cscript src=\"https://unpkg.com/blockly/javascript_compressed\"\u003e\u003c/script\u003e\n \u003cscript src=\"https://unpkg.com/blockly/python_compressed\"\u003e\u003c/script\u003e\n \u003cscript src=\"https://unpkg.com/blockly/php_compressed\"\u003e\u003c/script\u003e\n \u003cscript src=\"https://unpkg.com/blockly/lua_compressed\"\u003e\u003c/script\u003e\n \u003cscript src=\"https://unpkg.com/blockly/dart_compressed\"\u003e\u003c/script\u003e\n\n // Generate code for all the blocks in the workspace.\n const jsCode = javascript.javascriptGenerator.workspaceToCode(workspace);\n const pythonCode = python.pythonGenerator.workspaceToCode(workspace);\n const phpCode = php.phpGenerator.workspaceToCode(workspace);\n const luaCode = lua.luaGenerator.workspaceToCode(workspace);\n const dartCode = dart.dartGenerator.workspaceToCode(workspace);\n\n### Local scripts\n\nYou must include the generator after you include Blockly. \n\n \u003cscript src=\"blockly_compressed.js\"\u003e\u003c/script\u003e\n \u003cscript src=\"javascript_compressed.js\"\u003e\u003c/script\u003e\n \u003cscript src=\"python_compressed.js\"\u003e\u003c/script\u003e\n \u003cscript src=\"php_compressed.js\"\u003e\u003c/script\u003e\n \u003cscript src=\"lua_compressed.js\"\u003e\u003c/script\u003e\n \u003cscript src=\"dart_compressed.js\"\u003e\u003c/script\u003e\n\n // Generate code for all the blocks in the workspace.\n const jsCode = javascript.javascriptGenerator.workspaceToCode(workspace);\n const pythonCode = python.pythonGenerator.workspaceToCode(workspace);\n const phpCode = php.phpGenerator.workspaceToCode(workspace);\n const luaCode = lua.luaGenerator.workspaceToCode(workspace);\n const dartCode = dart.dartGenerator.workspaceToCode(workspace);\n\nGenerate code\n-------------\n\nTo generate code, use the generator's `workspaceToCode` function. \n\n const code = javascriptGenerator.workspaceToCode(workspace);\n\n### Continuous updates\n\nContinuous updates allow you to show or run the code whenever the user makes a\nchange. Generating code is a fast operation, so there's little performance\nimpact for doing this. This is done using an [event listener](/blockly/guides/configure/web/events). \n\n const supportedEvents = new Set([\n Blockly.Events.BLOCK_CHANGE,\n Blockly.Events.BLOCK_CREATE,\n Blockly.Events.BLOCK_DELETE,\n Blockly.Events.BLOCK_MOVE,\n ]);\n\n function updateCode(event) {\n if (workspace.isDragging()) return; // Don't update while changes are happening.\n if (!supportedEvents.has(event.type)) return;\n\n const code = javascriptGenerator.workspaceToCode(workspace);\n document.getElementById('textarea').value = code;\n }\n\n workspace.addChangeListener(updateCode);\n\nAdd preamble or postscript code\n-------------------------------\n\nAfter you've generated your code you can optionally include code before the\nstart or after the end of the generated code. \n\n let code = javascriptGenerator.workspaceToCode(workspace);\n // Add a preamble and a postscript to the code.\n const preamble = 'import {MyLibrary} from \\'/path/to/my/library\\';\\n'\n const postscript = 'MyLibrary.myKickoffFunction();\\n';\n code = preamble + code + postscript;\n\nPreamble code is usually used to include external definitions at the beginning\nof the code. Postscript code is usually used to call functions to kick off\nbehavior at the end of the code.\n\nIf your generated code is runnable as-is, then there's no need to include a\npreamble or a postscript.\n\nExecute code\n------------\n\nAfter you've generated the code, you need to figure out how to execute it.\nDeciding how to execute it is very application-specific, and outside the scope\nof Blockly.\n\nIf you want to execute JavaScript code, see [Generate and run\nJavaScript](/blockly/guides/app-integration/running-javascript). This discusses\nsome special features of the JavaScript code generator, as well as\n[JSInterpreter](https://github.com/NeilFraser/JS-Interpreter), which the Blockly team recommends as a way to\nsafely execute JavaScript.\n\nOther languages require other tools."]]