4 Developer Takeaways from a Pyscript Demo
Pyscript works with or without JavaScript and offers the Python ecosystem inside HTML, explains a senior developer advocate from Anaconda.
Sep 30th, 2022 11:34am by
Image via Shutterstock
pyscript.”
He proceeded to demonstrate exactly how useful Pyscript, Anacando’s new language, could be by demonstrating the code with multiple data visualization tools. But first, he offered a developer’s perspective of what he’s learned about Pyscript. Here are four takeaways from his talk:
1. What the Heck Is Pyscript, Again?
Maggio spent a good deal of time really drilling into what Pyscript is and how it differs or does not differ from JavaScript and HTML — more on that below. As for the basics — what is Pyscript — he explained that it’s a client-side technology that doesn’t require a server. Strictly speaking, it’s implemented by a few lines of code in HTML to call Pyscript from Anaconda. You can download it or add some code to your page:
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
2. Pycharm Supports Pyscript
If you’d prefer IDE support before you try Pyscript, Maggio recommended PyCharm, an IDE for Python. In fact, he used it to create the presentation seen during the webinar. “Pycharm has done a great job in supporting Pyscript syntax highlighting within the Pyscript times,” he said. “If you ask me, I would definitely recommend using PyCharm as the go-to solution to use Pyscript immediately. And to be fair, this is what I’ve been using to prepare for this presentation. It’s working perfectly.” The syntax highlighting within the Pyscript helps to make it easier to reuse code because, as he put it, “It would become cumbersome to have HTML, JavaScript and Python altogether and Python not highlighted properly.”3. Pyscript Is Not a Javascript Replacement
Pyscript is a single-page application (SPA); from a more technical perspective it is just written in TypeScript, Maggio said. It’s not meant to be a JavaScript killer, he stressed. “What’s the deal here? What’s going on exactly?” he said. “Well, Pyscript allows the use of Python in the browser to build interactive web apps, something you would normally do at the moment with JavaScript. Pyscript allows you to work with or without JavaScript.” In fact, some of the demos he used in the webinar involved interacting directly with JavaScript, while others did not. It’s also more than Python, he added. “First off, a selling feature is Python in the browser; but it’s not just Python, the language in the browser, it is more like the Python ecosystem in the browser,” he said. “It’s Python with JavaScript.“4. PHP Vibes
The first time Maggio saw Pyscript, it gave him “PHP vibes,” he confessed. It was a joke, he said, but he added a best practice of PHP is to use something else. “Are we really doing the dark side here? How are we going into the direction of making Python a new PHP, which by the way, means person on page anyway, […] so we really joined the dark side.” That made him take a more careful look at Pyscript. “I understood that it’s completely a whole new ecosystem in front of me,” he said.”The answer is very simple [but] is actually not, because Pyscript is much much more than that — it is absolutely [in] no way related to PHP kind of technology.” With Pyscript, developers can avoid mixing HTML and Python code if that bothers them — and it should bother them, he added. “It’s not a good practice mixing presentation and programming logic,” he said. “In Pyscript, you can actually have the Python code separated in a separated module, you can write everything in a timer.py. In this example, I’ve imported using the auto — see the source attribute in the pyscript time. So the Pyscript enables best practice.” He demoed the timer script:
# timer.py
from datetime import datetime
now = datetime.now()
now.strftime("%m/%d/%Y, %H:%M:%S")
<py-script src="./timer.py"></py-script>
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.