Pyscript
My Insights On This New Awesome Technology
Want some music while reading? 🎵📻
Introduction
Recently, the development world is “shaken” by a new and fresh piece of technology: Pyscript. Why does Pyscript catch a lot of attention? Previously, when we talk about web development, we think of Javascript and HTML. There are many other popular choices such as Java and HTML, PHP, Python and Django etc. as well.
Javascript has always been the first choice for web development because it is natively supported by HTML. For example, we can create a very basic website like this:
|
|
The website only contains a “Hello World” title and a button to toggle the title’s color. We can inject Javascript into HTML simply using the <script>
tag and this method only works for Javascript. But what if you could inject Python into HTML like this without any extra effort? Mind-blowing right? 😉
Basics
Pyscript is very new, therefore, there’s not so many features yet. It’s very simple to use it, you don’t have to install the package or anything, you could just add the <link>
and <script>
tags in the document’s head like this
|
|
If everything is corrects, you will see that before rendering, there is a quick spinner “Loading runtime…” appears on the page. After that the “Hello World” text will show up.
By using this <pyscript>
tag, you could inject python code in there and it will be rendered on the screen.
Code Editor In The Web
The <py-repl>
tag creates a REPL component that is rendered to the page as a code editor, allowing you to write executable code inline. If you know Jupyter Notebook, this is quite similar.
|
|
With this, you could see a a component that looks like an input on the webpage that allows you to write Python code in it. For example, you could write print("Hello World")
in there then press Shift + Enter
, “Hello World” will be printed on the screen.
If you want the REPL to be auto generated every time you press Shift + Enter
, here is the code for that
|
|
Separate Python File
If we want to make our code cleaner, we could separate the python code into its own file
|
|
This piece of code allows us to shuffle an array of numbers then display that on the webpage via the output
element.
|
|
Since we’re using an external Python file, we declare the src
path to that file in the <pyscript>
tag. We have to add the <py-env>
tag and declaring numpy
value there because we are using numpy
in our code.
For HTML, we have a div
with id output
for displaying the array. Then we add a button to fire the shuffle_arr
function on click. It’s very important that the button has an id, if you don’t include that, Pyscript will complain about it in the console and code doesn’t work.
With only this, we have a functional webpage running by Python code!
Conclusion
Pyscript is a promising and brilliant new technology. At the moment, these are the main features it provides but I believe, there will be much more stuff coming up next.
You might have a question, will Python replace Javascript if we now can do DOM manipulation with Javascript? From my point of view, this is not going to happen because Javascript is a mature language and it’s just so powerful. Pyscript makes it easier for Python developers to work with web development but I think it can never replace Javascript.
As always, thanks for reading! I hope this gives you new ideas and happy hacking! 😉