In case you missed, yesterday I learned about PyScript, a tool that allows you to use Python inside your HTML! In other words, PyScript enables you to run Python on the client side.
For example, to use
console.log from within PyScript, you just type
from js import console in your PyScript tag.
Then, you can call
<py-script> from js import console console.log("Hey there, from 'console.log' inside PyScript!") </py-script>
If you include that in one of your pages and then check the console,
you will find the message
Hey there, from 'console.log' inside PyScript! in there.
Let me show another example that is slightly more involved.
setInterval that lets you run a function periodically.
If we write
from js import setInterval, that function becomes available to us.
That is what is being used to update the timestamp in the next paragraph.
It is now .
(If the paragraph above doesn't contain a timestamp that updates every second, something is broken! Right now, PyScript is in alpha, so that might be it... But it may be my fault as well 🙃)
<py-script> import datetime from js import setInterval from pyodide import to_js def update_timestamp(elem): elem.write( format(datetime.datetime.now(), "%H:%M:%S, %A, %d %B %Y") ) elem = Element("timestamp_updater") _ = setInterval( to_js(lambda: update_timestamp(elem)), 1000 # Update every 1000 ms ) </py-script>
Pretty cool, right?
I learned this by reading a thread that was published on Twitter:
That's it for now! Stay tuned and I'll see you around!
Espero que tenhas aprendido algo novo! Se sim, considera seguir as pisadas dos leitores que me pagaram uma fatia de pizza 🍕. O teu pequeno contributo ajuda-me a manter este projeto grátis e livre de anúncios aborrecidos.