Run Python in HTML: Pyscript by Anaconda

Pyscript HomepagePyscript is a new framework developed by anaconda which allows you to run python in your HTML code.This means you can execute python code in the browser, using <py-script> tags. It is super simple to set up, you do not need to in…


This content originally appeared on Level Up Coding - Medium and was authored by Arjun Gulbadhar

Pyscript Homepage

Pyscript is a new framework developed by anaconda which allows you to run python in your HTML code.

This means you can execute python code in the browser, using <py-script> tags. It is super simple to set up, you do not need to install anything.

Let’s try it out.

Create a simple HTML document using the following code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Title: PyScript</title>
</head>
<body>
</body>
</html>

Now in the head tag add the following lines

<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>

The final code should look like this:

As demonstrated in the above code, python code is enclosed in <py-script> tags.

Running the HTML file will give you this output.

Output of Example 1

It’s possible that you’ll wish to send variables from your Python code to HTML while using PyScript. This may be accomplished using the write method from the pyscript module inside the pyscript> element. You are allowed to pass strings that are displayed as normal text utilizing the id attribute.

The write method accepts two variables: the id value and the variable that will be provided.

Example 2:

Let’s use the datetime module in pyscript to print the date in HTML. In the following example, we use the label method.

The output of the above code:-

The output of Example 2

You can also run python IDE in HTML to run, modify and delete python code in the browser.

Here is the code for that:

The <py-repl> tag adds a code editor component to the webpage.

The Output of Example 3

External Libraries and Python packages that can be used in pyscript.

The project is still in its beginning phase, but does allow us to use several libraries, given below are a few of them.

· NumPy

· Pandas

· pytest

· Jinja2

· beautifulsoup4

· matplotlib

· PIL

· scikit-learn

A comprehensive list of third-party packages that are supported, is available in the Pyodide documentation.

The pyscript also allows you to play Mario in the web browser. Visit the examples in pyscript GitHub repository for more information.

Pyscript Example

Also, Read What Will The Last Human Look Like according to AI?

Hey Guys,

I am pretty new to writing on Medium and would love to hear your feedback. If you like what I am writing about, don’t hesitate to put a thumb up. Please feel free to leave a comment if you have a question or recommendation. I read every message and try to answer as soon as I can.

Arjun Gulbadhar


Run Python in HTML: Pyscript by Anaconda 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 Arjun Gulbadhar


Print Share Comment Cite Upload Translate Updates
APA

Arjun Gulbadhar | Sciencx (2022-09-20T12:58:55+00:00) Run Python in HTML: Pyscript by Anaconda. Retrieved from https://www.scien.cx/2022/09/20/run-python-in-html-pyscript-by-anaconda/

MLA
" » Run Python in HTML: Pyscript by Anaconda." Arjun Gulbadhar | Sciencx - Tuesday September 20, 2022, https://www.scien.cx/2022/09/20/run-python-in-html-pyscript-by-anaconda/
HARVARD
Arjun Gulbadhar | Sciencx Tuesday September 20, 2022 » Run Python in HTML: Pyscript by Anaconda., viewed ,<https://www.scien.cx/2022/09/20/run-python-in-html-pyscript-by-anaconda/>
VANCOUVER
Arjun Gulbadhar | Sciencx - » Run Python in HTML: Pyscript by Anaconda. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/20/run-python-in-html-pyscript-by-anaconda/
CHICAGO
" » Run Python in HTML: Pyscript by Anaconda." Arjun Gulbadhar | Sciencx - Accessed . https://www.scien.cx/2022/09/20/run-python-in-html-pyscript-by-anaconda/
IEEE
" » Run Python in HTML: Pyscript by Anaconda." Arjun Gulbadhar | Sciencx [Online]. Available: https://www.scien.cx/2022/09/20/run-python-in-html-pyscript-by-anaconda/. [Accessed: ]
rf:citation
» Run Python in HTML: Pyscript by Anaconda | Arjun Gulbadhar | Sciencx | https://www.scien.cx/2022/09/20/run-python-in-html-pyscript-by-anaconda/ |

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.