codolis header baner 2
codolis blog header
Blog

PyScript - Python in the Browser

Tamara Glisic PyScript

Last year we heard about the PyScript project as a new framework for Python users. Peter Wang, CEO of Anaconda company, presented the alpha version of this project at the PyCon conference in 2022. The motto he highlighted in his speech was “The next 100 million Pythonistas – or programming for everyone”.

While we keep an eye on the continuous upgrades since it was first presented, let’s talk about basic information and the advantages this framework brings.

The most common software developers’ perception of Python is that it’s an easy-to-learn programming language. That’s why it is often one of the top few most popular languages according to StackOverflow research.

Python scientific tools were the basis of the idea. Peter Wang and his colleague Travis Oliphant wanted to improve the data analysing and business calculations level, especially the big data. 

The key advantages they saw in this project are:

  • Designed for readability (“Fits in your head”)
  • Easy to learn
  • Rich library ecosystem
  • Does many things with modest effort
  • Very popular

PyScript is a framework invented to provide users with an opportunity to create Python applications directly in the browser. We can write the code directly in an HTML interface thanks to Pyodide, Wasm, and other modern web technologies. These are the six basic components:

  1. Python in the browser – allows drop-in content, hosting applications without configuration servers
  2. Python ecosystem – usage of various popular scientific tools such as Numpy, Pandas, Sciki-learn, etc.
  3. Python with JavaScript – BI-directional communication between Python and JavaScript objects and namespaces
  4. Environment management – we can define which packages and files to include in running page code
  5. Visual application development – ease to use of UI components, such as Buttons, Containers, Text boxes, etc.
  6. Flexible framework – can be used for creating and sharing new components that can be included or excluded directly in Python

1. <py-config>

With Py-config we can configure metadata and define dependencies needed for PyScript applications. Configuration is possible in TOML and JSON formats and we should place it within the <body> tag.

Attributes:

PyScript elements

2. <py-repl> 

Read Eval Print Loop is an interactive Python framework that runs multiple code lines. If we print any of the REPL elements – the output will be shown at the py-terminal.

Attributes:

PyScript Elements 3

3. <py-script>

Allows running Python scripts whether in code or in a specific folder imported by src attributes.

Attributes:

PyScript Elements 5

Plugins

<py-terminal>

This is one of the core plug-ins, and it’s active by default. It is used for printing across stdout and stderr and for listing export data on the py-terminal page.

PyScript Elements 7

PyScript or JavaScript?

By launching the PyScript project, now we have a choice to use the language we prefer. The best thing is that we don’t even have to decide about only one of them because PyScript allows us to use JavaScript libraries. This makes JavaScript and PyScript compatible. The only weakness may be the download duration, so in some cases, PyScript would not be the best choice.

If you ask if PyScript tends to replace JavaScript, the answer would be no. This framework should give extra value and increase flexibility for all Python users. In cases where one would never use HTML, CSS, and JavaScript, now have the motivation to combine benefits from both languages.

This blog post was written by our software engineer Tamara Glišić.