1: Getting Started

Let’s get started #

Alright, let’s jump into Learning PyScript.

What is PyScript, and why would it be useful to know? If you already know what PyScript is, you can skip this section and move onto the next one.

Skip the intro and just get started!

Prerequisites #

These tutorials are written for those that have some experience with programming - but not much. If you’ve done any programming before, or even some fancy equations in Excel, then you have all you need to get started. Do be mindful that you will need to run some commands from the command line, but not much, and I’ll give you the commands you need.

If you haven’t done any programming before, I recommend finding a tutorial on building a basic HTML website. Here is a great tutorial, but it contains more information than you need for this site - as long as you’ve got a website up and running, you are good to go. (In that particular page, if you get up to “Inline-level Elements”, you are good to go.) If you prefer video, check out this video on YouTube.

PyScript is a mix of HTML and Python. On the Python side, these lessons do not assume you already know Python, but the lessons do not explain everything either. Either before you tackle this site, or in combination with this site, I recommend going through a tutorial on Python, such as A Byte of Python, which is free, and very high quality.

What is PyScript? #

PyScript is a way to run Python in your web browser, client side.

Normally when we think of running python, it is as a script running on a server, or doing data analysis with a Jupyter Notebook. Python has typically been seen as a programming language for writing scripts, or programs that run “in the background”. You can write web applications in Python too, for exampe with Django, Flask, Dash or Streamlit.

However, the python program here runs on the background web server, with websites sent to clients. Communication between the user and the python program happens with the browser (running HTML and JavaScript), sending data to the server using web requests.

This is how it has been for a long time - Python (or some other language) running on the web server, and HTML + JavaScript running on the web browser (i.e. Chrome, Firefox, Edge, Safari).

As far as programming langauges running on the web browser go, it was just JavaScript for a long time. Recently, a set of tools known as WebAssembly has changed all of that. WebAssembly is low-level code for web browsers (among other things, but that’s the bit of interest to us at the moment). By using WebAssembly, we can write programs that run in web browsers with (1) langauges other that JavaScript, and (2) that run a lot faster than if they had of been written in JavaScript natively.

To cut a long story short, you “compile” Python itself in WebAssembly and this gives you a version of Python that can run on your web browser. Hello PyScript!

Still, why learn PyScript? #

Python is the most popular programming language in the world.

Web browsers are the most popular target platform in the world.

Merging the two gives an ability to use a great programming language in an environment that runs on nearly all modern systems (including linux, mobile phones etc).

Are there instances I shouldn’t use PyScript? #

At the moment - definitely!

PyScript is in its alpha stage of development. This means it is not ready for production usage, so you should steer clear of it for work projects… for now. PyScript is being actively worked on, and will likely come out of alpha in the near future.

PyScript is also quite slow to start, specifically the first time you launch an application. I expect this to improve, a lot, in the near future.

If you need the speed, I recommend ensuring you have only the dependencies (libraries) you need, but your users will need a fast internet connection to download the files needed quickly!

Let’s get started!