英文:
Brython python to javascript converter
问题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<textarea name="input" id="input" cols="30" rows="10">Here write your python code...</textarea>
<button id="convert" onclick="convert()">Convert</button>
<textarea name="output" id="output" cols="30" rows="10">Here you will receive your javascript code...</textarea>
<script>
function convert()
{
var input = document.getElementById("input");
var output = document.getElementById("output");
output.innerHTML = input.innerHTML;
}
</script>
</body>
</html>
英文:
I want to create a tool using brython that translates python to js without running it. I know that there is a way to do it (i was doing some research: http://www.brython.info/tests/precompile.html or https://stackoverflow.com/questions/22595989/python-to-javascript-converter) but still i can't make it. The goal is to make html file like this below, but instead of copying and pasting same value to the second textarea actualy convert it to javascript?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<textarea name="input" id="input" cols="30" rows="10">Here write your python code...</textarea>
<button id="convert" onclick="convert()">Convert</button>
<textarea name="output" id="output" cols="30" rows="10">Here you will receive your javascript code...</textarea>
<script>
function convert(content)
{
var input = document.getElementById("input");
var output = document.getElementById("output");
output.innerHTML = input.innerHTML;
}
</script>
</body>
</html>
I tried to make python to js converter without frameworks such as Flask or Django, obviously it won't be a problem If you will suggest to use another js library for this like pyjs, javascrypthon or anything else.
答案1
得分: 0
Brython不会按照您的意图执行,如果您考虑的是“Google翻译”的话:Brython可以在javascript虚拟机中运行转译后的Python代码,但在这样做时,它会运行嵌入在Brython javascript代码中的数百或数千行代码。
所以,为了运行一个“简单的代码”如下:
for i in range(10):
print(i)
它不会简单地将其替换为javascript的for
语句和console.log
。它将运行这段代码,模拟Python的内部运行,直到某个深度,此时它会执行Javascript的基本操作 - 这意味着它将具有一个带有__iter__
方法的range
对象 - for
命令将为其创建一个Python迭代器,并调用其__next__
方法,直到引发StopIteration
异常,然后在Python的except
语句中捕获它并退出循环。即使Brython可以选择只运行Python的这个子集所需的行,这也将等于将近100行代码。
英文:
Brython won't do what you intend there, if you are thinking in terms of "Google Translate": Brython can run transpiled Python in the javascript VM, but in doing so, it runs hundreds or thousands of lines of code embedded in the Brython javascript code itself.
So, in order to run a "simple code" as:
for i in range(10):
print(i)
It won't simply do a search and replace of this for a javascript for
statement and a console.log
. It will run this mimicking Python's internals up to a certain depth, at which point it executes Javascript primitives - that means it will have a range
object with a __iter__
method - the for
command will create a Python iterator for that, and call its __next__
method until a StopIteration
exception is raised, then it catch it in a Python except
statement and exit the loop. Even if Brython could select only the lines needed to run this subset of Python, that would amount to nearly 100 lines of code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论