Brython Python 到 JavaScript 转换器

huangapple go评论72阅读模式
英文:

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?

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;textarea name=&quot;input&quot; id=&quot;input&quot; cols=&quot;30&quot; rows=&quot;10&quot;&gt;Here write your python code...&lt;/textarea&gt;
    &lt;button id=&quot;convert&quot; onclick=&quot;convert()&quot;&gt;Convert&lt;/button&gt;
    &lt;textarea name=&quot;output&quot; id=&quot;output&quot; cols=&quot;30&quot; rows=&quot;10&quot;&gt;Here you will receive your javascript code...&lt;/textarea&gt;

    &lt;script&gt;
        function convert(content)
        {
            var input = document.getElementById(&quot;input&quot;);
            var output = document.getElementById(&quot;output&quot;);
            
            output.innerHTML = input.innerHTML;
        }
    &lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;

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.

huangapple
  • 本文由 发表于 2023年3月9日 18:59:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定