Run Python Code in the Browser with VS Code: A Step-by-Step Guide

Ali Hassan
3 min readNov 1, 2024

--

Running Python code in the browser directly from Visual Studio Code (VS Code) is a convenient setup for developers who want an integrated workflow for development and testing. This guide will walk you through setting up your VS Code to run Python code on a browser.

Step 1: Install VS Code and Set Up Python

  1. Download and Install VS Code: If you haven’t installed Visual Studio Code, download it from VS Code’s official website and complete the installation.
  2. Install Python: If Python is not installed on your machine, download and install it from Python’s official website.
  3. Add Python to Path: During installation, check the box that says “Add Python to PATH.” This will make Python accessible from the terminal.

Step 2: Install Python Extensions in VS Code

To run Python code in VS Code efficiently, you’ll need to install the Python extension:

  1. Open VS Code.
  2. Go to Extensions: Click on the Extensions icon on the left sidebar.
  3. Search for “Python”: Install the official Python extension provided by Microsoft. This will enable syntax highlighting, code completion, and more.

Step 3: Set Up Live Server to Run Code in the Browser

The Live Server extension in VS Code allows you to run HTML files in the browser, making it easier to view results when embedding Python with PyScript or Brython.

  1. Install Live Server:
  • In VS Code, go to Extensions and search for “Live Server.”
  • Install the extension, which will allow you to open your HTML files directly in a browser.

2. Configure Your Project:

  • Create a new folder for your project and open it in VS Code.
  • Inside your project folder, create an index.html file where you'll embed your Python code.

Step 4: Running Python Code in Browser Using PyScript

PyScript is a popular way to run Python code in a browser with HTML. Here’s how to set it up:

  1. Add PyScript to Your HTML File:
  • Open index.html and add the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python in Browser</title>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<h1>Hello, Python in Browser!</h1>
<py-script>
print("Hello from PyScript!")
</py-script>
</body>
</html>

Run the HTML with Live Server:

  • Right-click index.html and select "Open with Live Server." Your browser will open a new tab displaying the HTML page with Python running inside.

Check Output: Any output from the py-script will appear directly on the webpage or in the browser console.

Step 5: Running Python Code in Browser Using Brython

Brython offers an alternative way to run Python in the browser and is ideal for interactive applications.

  1. Add Brython Library:
  • Open index.html and modify it like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python with Brython</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.9.5/brython.min.js"></script>
</head>
<body onload="brython()">
<h1>Hello from Brython!</h1>
<script type="text/python">
print("Hello from Brython!")
</script>
</body>
</html>
  1. Run Using Live Server:
  • Open the modified index.html file with Live Server as before.
  • The browser will display the output either on the page or in the console, running the Python code embedded with Brython.

Summary

With VS Code, running Python code in the browser becomes seamless. Using Live Server and tools like PyScript or Brython, you can write, test, and view Python code directly on a webpage. This workflow simplifies development, especially for beginners and those looking to create interactive web applications. Try it out, and enjoy coding Python right in your browser!

Need Portfolio, Landing Page Website with modern vibe Contact me

--

--

Ali Hassan
Ali Hassan

Written by Ali Hassan

I’m a Web Developer 💻, UI/UX Designer 🎨, and Blogger ✍️ creating stunning websites and sharing insights on tech and design. Let’s connect and innovate! 🌟

Responses (1)