PyScript : Python in the Browser

PyScript : Python in the BrowserPhoto by Sai Kiran Anagani on UnsplashIf you only write code into python but you are also interested to build fancy websites in a browser then at PyCon US 2022, Anaconda’s CEO Peter Wang announced a new technology called…


This content originally appeared on Level Up Coding - Medium and was authored by Amit Kumar Manjhi

PyScript : Python in the Browser

Photo by Sai Kiran Anagani on Unsplash

If you only write code into python but you are also interested to build fancy websites in a browser then at PyCon US 2022, Anaconda’s CEO Peter Wang announced a new technology called PyScript that allows users to write Python in the browser.

What is PyScript?

  • PyScript is developed by the team from Anaconda including Peter Wang, Fabio Pliger, and Philipp Rudiger.
  • PyScript is newly created framework or system for executing Python code in the browser. Sounds Interesting !
  • There is no any infrastructure barriers
  • PyScript is currently built on Pyodide and WebAssembly/Emscripten which connect JavaScript and Python.
  • With the help of PyScript as a data scientists, we can share our dashboards and our models in an html file, which will run code whenever others open the file in a web browser

How to use PyScript?

  • There’s nothing to install. Only you need to add 2 resources to your HTML head, a JavaScript file, and a CSS.
  • The alpha release of PyScript can be found on pyscript.net.
  • PyScript has three main components:
  • py-env defines the Python packages needed to run your Python code
  • py-script is where you write your Python code that gets executed within. the web page
  • py-repl creates a REPL (read-eval-print loop) component that evaluates the code users enter and displays the results.

Let’s understand PyScript with few examples:

To understand PyScript in detail follow below steps one by one:

1. Create Simple HTML File

Result of above file looks like

2. Add a JavaScript file and a CSS file to HTML Head

3. Add Python code to your HTML file

The result of above file looks like as below:

In this example you have seen that we can run our Python code in the browser without any external dependency. Sounds amazing!

4. PyScript with more complex examples

So in this example, we’ve introduced a for loop and result looks like as below:

5. Add Python Package

In PyScript we can easily add python packages using py-env tag

Let’s use this python packages into next example

6. Plotting a Distplot With the Histogram

And the result is here

7. py-repl examples

With the help of py-repl you can create a custom repl and you can run your code within that.

Conclusion :

PyScript brings Python to the browser. I’m very excited about this new technology . Again it is very simple and easy to learn. Hope you enjoyed reading this article. Please give it a try, have fun and let me know your feedback!

References :


PyScript : Python in the Browser was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Amit Kumar Manjhi


Print Share Comment Cite Upload Translate Updates
APA

Amit Kumar Manjhi | Sciencx (2022-05-13T10:43:22+00:00) PyScript : Python in the Browser. Retrieved from https://www.scien.cx/2022/05/13/pyscript-python-in-the-browser/

MLA
" » PyScript : Python in the Browser." Amit Kumar Manjhi | Sciencx - Friday May 13, 2022, https://www.scien.cx/2022/05/13/pyscript-python-in-the-browser/
HARVARD
Amit Kumar Manjhi | Sciencx Friday May 13, 2022 » PyScript : Python in the Browser., viewed ,<https://www.scien.cx/2022/05/13/pyscript-python-in-the-browser/>
VANCOUVER
Amit Kumar Manjhi | Sciencx - » PyScript : Python in the Browser. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/05/13/pyscript-python-in-the-browser/
CHICAGO
" » PyScript : Python in the Browser." Amit Kumar Manjhi | Sciencx - Accessed . https://www.scien.cx/2022/05/13/pyscript-python-in-the-browser/
IEEE
" » PyScript : Python in the Browser." Amit Kumar Manjhi | Sciencx [Online]. Available: https://www.scien.cx/2022/05/13/pyscript-python-in-the-browser/. [Accessed: ]
rf:citation
» PyScript : Python in the Browser | Amit Kumar Manjhi | Sciencx | https://www.scien.cx/2022/05/13/pyscript-python-in-the-browser/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.