脚本宝典收集整理的这篇文章主要介绍了

Brython 在浏览器用Python取代JavaScript

脚本宝典小编觉得挺不错的,现在分享给大家,也给大家做个参考,希望能帮助你少写一行代码,多一份安全和惬意。
<p><code></p> <p>看到的一个有趣的 Open Source 项目(<a rel="nofollow" href="http://www.brython.info/index.html" target="_blank">Brython</a>), 虽然我觉得没什么实用性~ XD</p> <p>不过, 既然是蛇年, 就让 浏览器 也 蛇化 吧~</p> <p><span class="img-wrap"><img data-src="http://segmentfault.com/img/bVbHI8" src="http://segmentfault.com/img/bVbHI8" alt="brython" style="cursor: pointer; display: inline;"></span></p> <p>Brython 的说明:</p> <blockquote> <p>Brython的目标是用Python取代JavaScript,使Python成为web浏览器的脚本语言。</p> </blockquote> <h2 id="articleHeader0">Brython - 在浏览器用 Python 取代 JavaScript</h2> <p>此项目载入一个 js 文件, 然后就可以用 Python 语法来操作 <a href="http://www.js-code.com/tag/dom" title="DOM" target="_blank">DOM</a>、<a href="http://www.js-code.com/tag/ajax" title="AJAX" target="_blank">AJAX</a>... 等等的事情. (写在 <code>&lt;script type="text/<a href="http://www.js-code.com/tag/python" title="python" target="_blank">python</a>"&gt;</code>里面)</p> <ul> <li>项目首页: <a rel="nofollow" href="http://brython.info/index_en.html" title="Brython - browser python" target="_blank">Brython - browser python</a></li> <li>Google code: <a rel="nofollow" href="http://code.google.com/p/brython/" title="brython - Browser Python" target="_blank">brython - Browser Python</a></li> </ul> <h3 id="articleHeader1">Brython 使用方式(范例)</h3> <p>说明文件: <a rel="nofollow" href="http://brython.info/doc/en/index.html" title="Brython document" target="_blank">Brython document</a>,</p> <p>下述范例取自此篇:</p> <div class="widget-codetool" style="display:none;"> <div class="widget-codetool--inner"> <span class="selectCode code-tool" data-toggle="tooltip" data-placement="top" title="" data-original-title="全选"></span><br /> <span type="button" class="copyCode code-tool" data-toggle="tooltip" data-placement="top" data-clipboard-text="<html><br /> <head><br /> <script src=&quot;/brython.js&quot;></script><br /> </head><br /> <body onLoad=&quot;brython()&quot;><br /> <script type=&quot;text/python&quot;> def echo(): alert(doc[&quot;zone&quot;].value) </script><br /> <input id=&quot;zone&quot;><button onclick=&quot;echo()&quot;>clic !</button><br /> </body><br /> </html><br /> " title="" data-original-title="复制"></span> </div> </p></div> <pre class="hljs xml"><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"/brython.js"</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">body</span> <span class="hljs-attr">onLoad</span>=<span class="hljs-string">"brython()"</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/<a href="http://www.js-code.com/tag/python" title="浏览关于“python”的文章" target="_blank" class="tag_link">python</a>"</span>&gt;</span><span class="actionscript"> def echo(): alert(doc[<span class="hljs-string">"zone"</span>].value) </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"zone"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"echo()"</span>&gt;</span>clic !<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span> </code></pre> <h3 id="articleHeader2">Brython Syntax 笔记</h3> <p>Brython Syntax 说明: <a rel="nofollow" href="http://brython.info/doc/en/brython.html#syntaxe" title="Brython Syntax" target="_blank">Brython Syntax</a></p> <h4>建立 a link</h4> <div class="widget-codetool" style="display:none;"> <div class="widget-codetool--inner"> <span class="selectCode code-tool" data-toggle="tooltip" data-placement="top" title="" data-original-title="全选"></span><br /> <span type="button" class="copyCode code-tool" data-toggle="tooltip" data-placement="top" data-clipboard-text="link1 = A('Brython', href='http://www.brython.info') link2 = A(B('Python'), href='http://www.python.org') " title="" data-original-title="复制"></span> </div> </p></div> <pre class="hljs lisp"><code class="lang-python">link1 = A('Brython', href='http<span class="hljs-symbol">://www</span>.brython.info') link2 = A(<span class="hljs-name">B</span>('Python'), href='http<span class="hljs-symbol">://www</span>.python.org') </code></pre> <h4>建立 a 并 附加属性</h4> <div class="widget-codetool" style="display:none;"> <div class="widget-codetool--inner"> <span class="selectCode code-tool" data-toggle="tooltip" data-placement="top" title="" data-original-title="全选"></span><br /> <span type="button" class="copyCode code-tool" data-toggle="tooltip" data-placement="top" data-clipboard-text="link = A() link <= B('connexion') link.href = 'http://example.com' " title="" data-original-title="复制"></span> </div> </p></div> <pre class="hljs perl"><code><span class="hljs-keyword">link</span> = A() <span class="hljs-keyword">link</span> &lt;= B(<span class="hljs-string">'connexion'</span>) link.href = <span class="hljs-string">'http://example.com'</span> </code></pre> <h4><a href="http://www.js-code.com/tag/ajax" title="AJAX" target="_blank">AJAX</a></h4> <div class="widget-codetool" style="display:none;"> <div class="widget-codetool--inner"> <span class="selectCode code-tool" data-toggle="tooltip" data-placement="top" title="" data-original-title="全选"></span><br /> <span type="button" class="copyCode code-tool" data-toggle="tooltip" data-placement="top" data-clipboard-text="req = ajax() req.on_complete = on_complete req.set_timeout(timeout, err_msg) req.open('POST', url, True) req.set_header('content-type', 'application/x-www-form-urlencoded') req.send(data) " title="" data-original-title="复制"></span> </div> </p></div> <pre class="hljs kotlin"><code class="lang-python">req = ajax() req.on_comp<a href="http://www.js-code.com/tag/let" title="浏览关于“let”的文章" target="_blank" class="tag_link">let</a>e = on_complete req.set_timeout(timeout, err_msg) req.<span class="hljs-keyword">open</span>(<span class="hljs-string">'POST'</span>, url, True) req.set_header(<span class="hljs-string">'content-type'</span>, <span class="hljs-string">'application/x-www-form-urlencoded'</span>) req.send(<span class="hljs-keyword">data</span>) </code></pre> <h4>Local storage</h4> <div class="widget-codetool" style="display:none;"> <div class="widget-codetool--inner"> <span class="selectCode code-tool" data-toggle="tooltip" data-placement="top" title="" data-original-title="全选"></span><br /> <span type="button" class="copyCode code-tool" data-toggle="tooltip" data-placement="top" data-clipboard-text="local_storage['foo'] = 'bar' log(local_storage['foo']) del local_storage['foo'] log(local_storage['foo']) # prints None " title="" data-original-title="复制"></span> </div> </p></div> <pre class="hljs perl"><code class="lang-python">local_storage[<span class="hljs-string">'foo'</span>] = <span class="hljs-string">'bar'</span> <span class="hljs-keyword">log</span>(local_storage[<span class="hljs-string">'foo'</span>]) del local_storage[<span class="hljs-string">'foo'</span>] <span class="hljs-keyword">log</span>(local_storage[<span class="hljs-string">'foo'</span>]) <span class="hljs-comment"># prints None</span> </code></pre> <h4>text/<a href="http://www.js-code.com/tag/python" title="python" target="_blank">python</a> 写法范例</h4> <div class="widget-codetool" style="display:none;"> <div class="widget-codetool--inner"> <span class="selectCode code-tool" data-toggle="tooltip" data-placement="top" title="" data-original-title="全选"></span><br /> <span type="button" class="copyCode code-tool" data-toggle="tooltip" data-placement="top" data-clipboard-text="<script type='text/python'> def mouse_move(ev): doc[&quot;trace&quot;].value = '%s %s' %(ev.x,ev.y)</p> <p>doc[&quot;zone&quot;].onmousemove = mouse_move </script><br /> " title="" data-original-title="复制"></span> </div> </p></div> <pre class="hljs scala"><code class="lang-python">&lt;script <span class="hljs-class"><span class="hljs-keyword">type</span></span>=<span class="hljs-symbol">'text</span>/python'&gt; <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">mouse_move</span></span>(ev): doc[<span class="hljs-string">"trace"</span>].value = '%s %s' %(ev.x,ev.y) doc[<span class="hljs-string">"zone"</span>].onmousemove = mouse_move &lt;/script&gt; </code></pre> <h2 id="articleHeader3">Brython 相关 Library (库)</h2> <p>库的套件可见: <a rel="nofollow" href="http://brython.info/doc/en/modele.html" title="Brython - Compiling and running" target="_blank">Brython - Compiling and running</a></p> <p>这边有很多必须的 Library, 在这边列一些应用类的.</p> <ul> <li><code>py2js.js</code> : does the conversion between the tokens and the Javascript code</li> <li><code>py_dom.js</code> : interaction with the <a href="http://www.js-code.com/tag/html" title="HTML" target="_blank">HTML</a> document (<a href="http://www.js-code.com/tag/dom" title="DOM" target="_blank">DOM</a>)</li> <li><code>py_ajax.js</code> : Ajax implementation</li> <li><code>py_local_storage.js</code> : implementation of the <a href="http://www.js-code.com/tag/html" title="HTML" target="_blank">HTML</a>5 local storage</li> <li><code>py_svg.py</code> : SVG support (vector graphics)</li> </ul> <hr> <p>by <a rel="nofollow" href="http://blog.longwin.com.tw/2013/03/bryphon-browser-python-2013/" target="_blank">Tsung</a> under <a rel="nofollow" href="http://creativecommons.org/licenses/by-sa/3.0/" target="_blank">CC-by-sa</a></p> <p></code></p>

总结

以上是脚本宝典为你收集整理的

Brython 在浏览器用Python取代JavaScript

全部内容,希望文章能够帮你解决

Brython 在浏览器用Python取代JavaScript

所遇到的程序开发问题,欢迎加入QQ群277859234一起讨论学习。如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典网站推荐给程序员好友。 本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。

80%的人都看过