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ć. 

FAQ

While PyScript enables Python to run in the browser, it is not as optimized for web execution as JavaScript. JavaScript is designed for high-performance execution within browsers, benefiting from just-in-time (JIT) compilation and years of optimization. PyScript, on the other hand, relies on WebAssembly (via Pyodide) to interpret Python code, making it significantly slower. Depending on the complexity of the application, PyScript can run 1.5 to 5 times slower than JavaScript for DOM interactions, networking, and computational tasks. Therefore, while PyScript is excellent for prototyping, education, and integrating Python capabilities into the web, it is not yet a replacement for JavaScript in performance-critical applications.

Yes, PyScript does not support all Python libraries, especially those that rely on native C extensions, such as TensorFlow, OpenCV, and some parts of NumPy. Since PyScript runs in the browser using WebAssembly, it is limited to libraries that can be compiled for this environment. Many Python packages that depend on system-level access, threading, or GPU acceleration are either unavailable or require workarounds. However, PyScript provides access to a subset of Python’s ecosystem through Pyodide, which includes common libraries like pandas, Matplotlib, and SciPy. Developers should check library compatibility before integrating PyScript into production applications.

Security is a critical concern when using PyScript, particularly since Python code runs client-side and is exposed in the browser. Unlike server-side Python applications where sensitive data is protected, PyScript code can be inspected using browser developer tools. This means storing API keys, database credentials, or sensitive logic directly in PyScript is a major security risk. To mitigate this, developers should avoid hardcoding sensitive data and instead use secure server-side APIs to handle authentication, data processing, and database interactions. Additionally, PyScript inherits the same security restrictions as JavaScript, such as cross-origin policies and sandboxing, but developers must still be cautious about exposing sensitive information.