Pyscript: A refreshing new world in the browser space

There has been multiple ways of creating backend applications in python over the years: flask, django, fastapi

But if you wanted to do something in the frontend you had to switch to a frontend framework in JavaScript such as React, Vue.js, Svelte in order to get your users some sort of interface.

This annoyance of having to use JavaScript have been solved by our good friends at Anaconda.

Pyscript

Pyscript allows you to write native python code directly inside of your HTML code . You are able to use python libraries you are already familiar with.

Getting started

Getting started is pretty easy, all you have to do is download the css and js dependencies inside of your HTML.

<!DOCTYPE html>
<html lang="en">
  <head>
  ...
  <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
  <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
  ...
  </head>

</html>

Doing this is going to give us access to new html tags we can use.

<py-script> tag

The <py-script> tag is what will allow us to run our python code

<body>
   
    <py-script>
my_name = "friend"
def greeter(name: str) -> str:
    print(f"hello {name}, from pyscript!")
        
greeter(my_name)
        
    </py-script>
    
</body>

Output from code above

Feel free to view the page from this post to look at the python code yourself.

my_name = "friend" def greeter(name: str) -> str: print(f"hello {name}, from pyscript!") greeter(my_name)

Resources

Pyscript.net
Run Python code in your HTML.