Better Programming

Advice for programmers.

Follow publication

Member-only story

Running Python Scripts on the Web Using PyScript

Yong Cui
Better Programming
Published in
6 min readMay 16, 2022
Photo by Jackson So on Unsplash

During the 2022 PyCon US conference, one of the keynote speakers Peter Wang unveiled PyScript — a way to write Python script directly within HTML. If you don’t know Peter, you may have heard of Anaconda, which is one of the most popular distributions of Python and R with a special focus on data science. Peter is Anaconda’s CEO and cofounder.

In other words, PyScript is developed by a reputable tech company, which may promise its viability in the near future. It has received a great deal of attention among Python and web developers, and it has over 10k stars on GitHub. However, whether it can become a successful product relies on long-term investment of time and development to make it a competitive product, which has some known drawbacks (to be discussed towards the end).

Without further ado, let’s take a quick look at this exciting product!

The py-script Tag

We know that the most common element of most websites is HTML files. When we make a website, our job is about producing HTML files directly or indirectly through some frameworks. In a typical HTML file, you’ll see various kinds of tags. For example, <head> defines the metadata and key information about the HTML page, <title> is the title for the document, and <h1>, <h2>, and so on define the different levels of headings.

As mentioned early, PyScript allows you to write Python scripts within HTML, and it uses a special tag — py-script. Within the tag, you can embed your Python script. To see how it works, you can create an HTML file, which contains the following code, and open the file using Chrome. In your Chrome, you should be able to see something like the below:

Screenshot by Author

In the above code snippet, you may notice that three things are significant.

  • The link tag defines an external style sheet. In our case, we use the css file that is provided by the PyScript.
  • We use the script tag to embed an external script hosted…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Yong Cui
Yong Cui

Written by Yong Cui

Work at the nexus of biomedicine, data science & mobile dev. Author of Python How-to by Manning (https://www.manning.com/books/python-how-to).

Responses (2)

Write a response