Browsers can run Python code, and Python may become a front-end and back-end take-all language

thumbnail

As a Python programmer, I envy those Java developers who can write amazing web pages at every turn. But this year, we can also write front-end in Python: the Anaconda team developed Py, you can write and run Python code in HTML, call Java libraries, in other words, you can use Python for the entire front-end and back-end development.

What is Py

Py [1] is a Javascipt library developed by the Anaconda team, which can embed Python code in HTML tags and run Python code without a server.

For example, we use Python to display the date on the page, first write a hello_world.html file, the content is as follows:

Open it with a browser, and its running effect is like this:

This means that we can write and run Python code in HTML, call Java libraries in Py, and do all of our web development in Python.

How Py Works

Py is currently built on Pyodide, a port of CPython to WebAssembly/Emen.

Py supports writing and running Python code in the browser, and will provide support for other languages ​​in the future.

You might be wondering, what is WebAssembly? The following is from Wikipedia:

> > WebAssembly (wasm for short) is an experimental low-level programming language used on the client side within the browser. WebAssembly is a portable abstract syntax tree designed to provide faster compilation and execution than Java. WebAssembly allows developers to compile in a programming language they are familiar with (initially targeting C/C++), and then execute it in the browser using a virtual machine engine. > > > The development teams of WebAssembly are from Mozilla, Google, Microsoft, and Apple, representing the four major web browsers: Firefox, Chrome, Microsoft Edge, and Safari. In November 2017, the above four browsers all started to experimentally support WebAssembly. > > > WebAssembly became a recommendation of the World Wide Web Consortium (W3C) on December 5, 2019, joining HTML, CSS and Java as the fourth language of the web. > > >

WebAssembly (wasm for short) is an experimental low-level programming language used on the client side within the browser. WebAssembly is a portable abstract syntax tree designed to provide faster compilation and execution than Java. WebAssembly allows developers to compile in a programming language they are familiar with (initially targeting C/C++), and then execute it in the browser using a virtual machine engine.

The development teams of WebAssembly are from Mozilla, Google, Microsoft, and Apple, representing the four major web browsers: Firefox, Chrome, Microsoft Edge, and Safari. In November 2017, the above four browsers all started to experimentally support WebAssembly.

WebAssembly became a recommendation of the World Wide Web Consortium (W3C) on December 5, 2019, joining HTML, CSS and Java as the fourth language of the web.

  1. Py enables more front-end developers to use Python.

  2. Lower programming threshold. Because almost everyone has access to a web browser, whether on a computer or on a mobile phone. This means you can start programming without installing any additional software.

  3. With Py, you no longer need to worry about deployment. Py provides an "architectural shift beyond the cloud" where everything happens in a web browser.

How to use Py

Py writes Python in html using the following three main components:

  • py-env defines the packages needed to run Python code.
  • py - is where Python code is written in web pages.
  • py-repl creates a REPL (read-evaluate-print loop) component that evaluates user-entered code and displays the results.

py-env defines the packages needed to run Python code.

py - is where Python code is written in web pages.

py-repl creates a REPL (read-evaluate-print loop) component that evaluates user-entered code and displays the results.

For example, the two tags in the code todo.html [2] :

Execution effect:

Let's look at an example of repl [3]:

Execution effect:

More examples can be found here [4].

How to run these demos?

git clonehttps://github.com/py/py.git

cdpy

npm install

npm run dev

Then the browser visits http://localhost:8080 and you will see this interface:

The future of Py

Py brings Python to the browser. We think it is still very valuable, and we are looking forward to this technological innovation. At present, GitHub has 2.7K stars. If you are also interested, you may wish to use it to make a website.

References

[1]

Py: https://github.com/py/py

[2]

todo.html: https://github.com/py/py/blob/main/pyjs/examples/todo.html

[3]

repl: https://github.com/py/py/blob/main/pyjs/examples/repl.html

[4]

here: https://github.com/py/py/tree/main/pyjs/examples

  • EOF -

Click on the title to jump

  1. 25 cases of Python text processing have been sorted out and collected!

  2. One line of Python code to easily build a tree-like heatmap

  3. Eight useful "no-code" features of Python

Think this article was helpful to you? please share with more people

It is recommended to pay attention to "Python developers" to improve Python skills

Like and watching is the biggest support ❤️

Latest Programming News and Information | GeekBar

Related Posts