Pyscript — Javascript framework that supports python in browser

WebOccult Technologies
WebOccult Technologies Pvt Ltd
4 min readMay 12, 2022
https://pyscript.net/

Hello folks,
This is an open letter for data scientists and python developers. Are you facing trouble writing basic functions in javascript on the web?

Anaconda came up with an interesting solution, Python in the browser, a Javascript framework — pyscript. A framework that runs your Python code in a browser without any installation of platforms like python or anaconda itself. Amazing isn’t it? There’s a famous saying that, necessity is the mother of invention. Often there’s a need for displaying the charts and analytics of certain things on the web. But being a data scientist, one is probably more proficient in working with python.

A javascript framework, launched in pycon 2022, by Anaconda which has a pretty good developer community, that gave a hand to rescue python developers from writing javascript.

Pyscript has already given several examples to users to get an idea about the things that can be easily developed on the web. You can play around with the examples shared at pysript-examples.

Let’s explore one of example :

For this example, two libraries are used folium and pandas, which we can import in pyscript as shown below :

<py-env>
- folium
- pandas
</py-env>

Code that needs to be added in the pyscript tag is as given below :

import folium
import json
import pandas as pdfrom pyodide.http import open_urlurl=(“https://raw.githubusercontent.com/python-visualization/folium/master/examples/data")
state_geo = f”{url}/us-states.json”
state_unemployment = f”{url}/US_Unemployment_Oct2012.csv”
state_data = pd.read_csv(open_url(state_unemployment))
geo_json = json.loads(open_url(state_geo).read())
m = folium.Map(location=[48, -102], zoom_start=3)folium.Choropleth(
geo_data=geo_json,
name=”choropleth”,
data=state_data,
columns=[“State”, “Unemployment”],
key_on=”feature.id”,
fill_color=”YlGn”,
fill_opacity=0.7,
line_opacity=0.2,
legend_name=”Unemployment Rate (%)”,
).add_to(m)folium.LayerControl().add_to(m)
m
You can try the above code to see the below-given results.

Why Pyscript?

Using Pyscript it will be really easy to run a python function in the browser without any installation of Python or Anaconda. Also, you can access it anywhere even on a mobile device without any kind of deployment.

What is pyscript?

A javascript framework build on web assembly designed to perform basic python functions.

To understand the base of the pyscript, Anaconda published a perfect article with an explanation of how the pyscript is built.

pyscript-anaconda

Where can pyscript help you?

To perform basic functions of python like split, strip, lambda, map, etc. in HTML and Mime rendering using Matplotlib

Perform Data Visualisation Tasks

Pyscript provides library support of Matplotlib, Pandas, Numpy, and many more which are very useful for machine learning engineers and data scientists to analyze data and create analytical reports. You can install and import these libraries to perform any experiments related to Machine learning and Data Science.

You might be feeling, that anaconda came up with a very helpful solution but it’s necessary to discuss its limitations too. As every coin has two sides, there are pros and cons to everything.

Let’s discuss a few of them that I discovered.

→ Importing the os module is working, but you cannot access your local file storage using python’s os module in pyscript as it is a javascript framework running in web assembly instead of the actual system.

→ For Machine Learning development, there are libraries like tensorflow.js available which allow you to even train a Machine Learning model. All those computations require high computational power.

→ If you know javascript, there are other frameworks available to perform session and database operations or to generate charts from CSV without using pandas which would eliminate the use of pyscript.

Basically, pyscript is a more useful framework for those who find it difficult to code in javascript, but those who are proficient in javascript can easily find the alternative solution to python libraries in javascript.

To python developers and data scientists who have been facing trouble writing code in javascript until now. Hope you enjoy coding in pyscript.

Pyscript,
A Bridge that supports bidirectional communication between javascript and python.

Credits:

A whole-heartedly thanks to our team and especially Shailja for contributing to this article.

--

--

WebOccult Technologies
WebOccult Technologies Pvt Ltd

WebOccult is an acclaimed technology company with a team of passionate and forward-thinking IT professionals.