

Python News: What's New From April 2022
source link: https://realpython.com/python-news-april-2022/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PyScript: Python in Your Browser
During his keynote at PyCon US, Anaconda CEO Peter Wang unveiled the PyScript project. PyScript allows you to write Python directly inside HTML and run it in your browser. Consider the following example:
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<py-script> print('Hello, World!') </py-script>
</body>
</html>
Note the <py-script>
tag, which can contain any valid Python code. In this case, this is the traditional Hello, World!
greeting.
This is functioning code. You can copy the above code block into a file, such as hello.html
, and save that file to your computer. Then you can open it in your browser, for example by using Ctrl+O or Cmd+O and selecting hello.html
. Alternatively, you can test a similar example immediately on the PyScript demos page.
PyScript provides custom HTML tags, including <py-script>
, which you saw above. There are several other tags as well, with many still in development. However, here are a couple that are immediately useful:
<py-env>
lists packages that should be made available in the environment.<py-repl>
creates a working Python REPL to interact with the environment.
The following, slightly more involved, example shows how you can use these features:
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- numpy
</py-env>
</head>
<body>
<h1 id="title">Magic Squares - Loading ...</h1>
<py-script>
import numpy as np
# Initialize a magic square
magic = np.array([[6, 7, 2], [1, 5, 9], [8, 3, 4]])
# Update title to indicate that the page has finished loading
pyscript.write("title", "Magic Squares")
</py-script>
<py-repl id="magic-repl" auto-generate="true">
magic.sum(axis=0)
</py-repl>
</body>
</html>
In this example, you declare that you want to use numpy
in your environment. Then you import numpy
, create an array that represents a magic square, and update the title on the page to indicate that the page is done loading. The REPL will be populated with code that you can run interactively on the web page that you’ve specified with the HTML. A typical session can look like the following:
Note that pyscript.write()
can interact with the Document Object Model (DOM) and update the contents of named HTML elements. Additionally, code you write in the REPL can use variables initialized in earlier code.
PyScript makes all of this possible by building on top of Pyodide. Pyodide provides CPython compiled to WebAssembly so that it can run in the browser or with Node.js. Additionally, Pyodide makes it convenient to call JavaScript from Python, and PyScript takes advantage of this to wrap JavaScript libraries like D3.
PyScript is still very much experimental, but the possibilities of this new framework are very exciting. We’re looking forward to following the development of PyScript going forward.
Recommend
-
2
Python News: What's New From April 2021? by David Amos May 03, 2021 0 Comments
-
9
Python News: What's New From January 2022? In January 2022, the code formatter Black saw its first non-beta release and published a new stability policy. IPython, the powerful in...
-
2
Python 3.11 Alpha 5 Released While the final release of Python 3.11 is planned for October 2022, which is still months ahead, you can already check out what’s coming. According to the development and...
-
7
My workload has been mounting in recent weeks. As a result, I've not been consistent with this series this year. Hopefully, I'll be here next week to shout at the top of my voice that Yes! I made it this week. Nevertheles...
-
12
Popular Design News of the Week: March 28, 2022 – April 3, 2022 Every day design fans submit incredible industry stories to our sister-site, Webdesigner News. Our colleagues sift through it, selec...
-
2
Popular Design News of the Week: April 4, 2022 – April 10, 2022 Every day design fans submit incredible industry stories to our sister-site, Webdesigner News. Our colleagues sift th...
-
6
Python News: What's New From March 2022? In March 2022, the Python 3.11.0a6 pre-release version became available for you to test, so you can stay on top of Python’s latest features. This release...
-
10
Popular Design News of the Week: April 25, 2022 – May 1, 2022 Every day design fans submit incredible industry stories to our sister-site, Webdesigner News. Our colleagues sift through it, selecting the very best s...
-
9
Python News: What's New From May 2022 Remove ads The first days of May 2022 were the last days of this year’s PyCon US...
-
12
Python News: What's New From April 2023 Remove ads Spring is in bloom, bringing new and ex...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK