Hello everyone, I'm DD, and it's the 51st day of being closed at home!
Recently, I have been updating the two columns of new Java features and IDEA Tips , and there is less dynamic attention to the content in other directions. When I was swiping and tweeting last night, I saw this magical thing. I thought it was very cool. I took it out and shared it:
I believe you saw the picture, you can guess what it is without me saying it? You can run python code in html !
I saw that many Python public accounts have begun to blow the future, but at first glance, why does it feel a bit like JSP? Or some templating engine? Progress or regression? Instead of thinking blindly, let's take a closer look at the ability of this stuff!
According to the official introduction, the core goal of this framework called PyScript is to provide developers with the ability to embed Python code in standard HTML, use Python to call JavaScript function libraries, and use Python to create Web applications.
Seeing that the introduction mentions the ability to call the JavaScript function library, it seems that it is still different from JSP or template engine.
PyScript Quick Experience
The official gave an example that can help us to feel the capabilities of this development framework. Let’s follow DD to see what it can do!
The first case, hello world
The code is very simple, just the following lines. You just need to create an html file and copy it in.
<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>
After saving, open it in your browser and you will see a page like this:
Look back at the content of this html, three core content:
- The style file that introduces pyscript:
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
- The script file that introduces pyscript:
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
-
<py-script>
Write specific python code in the tag to output Hello World
If you are too lazy to type the code yourself, I have packaged the two case codes in this article and put them on the official account. Friends who need it can follow the official account "Program Ape DD" and reply: pyscript to get it.
The second case, data definition + data display
First create a data.py
file, and then add the previous code. The function is very simple, it is to randomly generate (x, y) coordinates
import numpy as np
def make_x_and_y(n):
x = np.random.randn(n)
y = np.random.randn(n)
return x, y
Create another html file and add the following code
<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
- matplotlib
- paths:
- /data.py
</py-env>
</head>
<body>
<h1>Let's plot random numbers</h1>
<div id="plot"></div>
<py-script output="plot">
import matplotlib.pyplot as plt
from data import make_x_and_y
x, y = make_x_and_y(n=1000)
fig, ax = plt.subplots()
ax.scatter(x, y)
fig
</py-script>
</body>
</html>
It's a little more complicated here. In addition to a few points in hello world, here are a few points to pay attention to:
-
<py-env>
label: here declare the package to be imported and the file to be imported (created abovedata.py
) -
<py-script output="plot">
: This defines the content to be output in<div id="plot"></div>
, you can see that the logic here is written in python
The execution effect of this page is as follows:
Isn't it amazing? In the whole process, there is no familiar cs and js content, and the page realization of such a graph is completed.
summary
Finally, talk about a few feelings that gave me during the whole trying process:
- The development experience is highly unified. For python developers, the threshold for developing web applications can be lower
- I feel that the performance seems to be insufficient. The execution of several complex cases is a bit slow. At first, I thought it was due to some foreign CDNs. Later, after moving to the local area, it was still slow. This part may need further optimization.
This development framework is currently only an alpha version, and there will be more features and optimizations in the future. In general, I think this framework is still very cool, especially for those who have just learned Python, or only know Python, but want to quickly develop web applications It may be a good choice for my friends, so what do you think of this framework? Will it be hot in the future? Chat in the message area!
I packaged the two case codes in this article and put them on the official account. Friends who need it can follow the official account "Program Ape DD" and reply: pyscript to get it.
Welcome to my public account: Programmer DD. Learn about cutting-edge industry news for the first time, share in-depth technical dry goods, and obtain high-quality learning resources
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。