Today I learned about PyScript, a tool that lets you run Python in your HTML!

import datetime output = Element("py-script-target") now = format(datetime.datetime.now(), "%H:%M, %A, %d %B %Y.") output.write(now)

PyScript

Run Python in Your HTML

Today I learned about PyScript, a tool that lets you run Python from within your HTML!

I got so excited when I found out about this tool (which was presented to the world today, if I am not mistaken!) that I had to give it a try right away!

For example, you opened this page, it loaded, and then it inserted a timestamp here:

(If the timestamp doesn't show right away, wait a couple of seconds. If it still doesn't show, take into account that the PyScript version I am using here has only been tested on Chrome.)

This timestamp, which corresponds roughly to when the page finished loading, was put there by Python. I just had to write some code in the HTML of this page:

<py-script>
import datetime

output = Element("py-script-target")
now = format(datetime.datetime.now(), "%H:%M, %A, %d %B %Y.")
output.write(now)
</py-script>

The output = Element("py-script-target") assigns a <span> tag I have in the source of the page to the variable output. Then, I use standard Python to format the current date (datetime.datetime.now()) into a format I like. Finally, I write that string to the <span> element from before!

I am very excited to see what people build with this tool! Just bear in mind that it is still in its early stages of development.

By the way, I found out about PyScript from a tweet:

That's it for now! Stay tuned and I'll see you around!

Become a better Python 🐍 developer πŸš€

+35 chapters. +400 pages. Hundreds of examples. Over 30,000 readers!

My book β€œPydon'ts” teaches you how to write elegant, expressive, and Pythonic code, to help you become a better developer. >>> Download it here πŸπŸš€.

Previous Post Next Post

Blog Comments powered by Disqus.