1. Home
  2. /
  3. Pyscript
  4. /
  5. Simple Plot Example

Simple Plot Example - Pyscript Typing CST Test

Loading…

Simple Plot Example — Pyscript Code

Uses matplotlib to create a simple plot directly in PyScript.

<!DOCTYPE html>
<html>
<head>
	<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
	<py-script>
		import matplotlib.pyplot as plt
		plt.plot([1,2,3], [4,5,6])
		plt.show()
	</py-script>
</body>
</html>

Pyscript Language Guide

PyScript is a framework that allows running Python code directly in web browsers using HTML and JavaScript integration. It bridges Python with the web platform, enabling interactive applications without traditional backend setup.

Primary Use Cases

  • ▸Embedding Python code in web pages for interactivity
  • ▸Educational platforms teaching Python in the browser
  • ▸Interactive data visualizations with Python libraries
  • ▸Prototyping web apps quickly using Python
  • ▸Combining Python and JavaScript for hybrid applications

Notable Features

  • ▸Write Python directly in HTML with `<py-script>` tags
  • ▸Access JavaScript objects and browser APIs from Python
  • ▸Support for popular Python libraries like NumPy, Pandas, and Matplotlib
  • ▸Live code execution in browser without server
  • ▸Integration with Pyodide for WebAssembly-based Python runtime

Origin & Creator

Created by Anaconda, Inc., first announced in 2022, inspired by the desire to make Python a first-class citizen for web development alongside JavaScript.

Industrial Note

Used in education, prototyping, and interactive demos; suitable for web-based Python experimentation but not yet a replacement for full-scale web frameworks.

Quick Explain

  • ▸PyScript leverages WebAssembly and Pyodide to execute Python in the browser.
  • ▸Python scripts can be embedded in HTML using `<py-script>` tags.
  • ▸Supports interaction with JavaScript, DOM, and browser APIs.
  • ▸Allows building web apps, interactive visualizations, and widgets using Python.
  • ▸Runs entirely client-side, no server is required for Python execution.

Core Features

  • ▸Python interpreter running in browser via WebAssembly
  • ▸Direct DOM manipulation and event handling from Python
  • ▸Import Python packages dynamically in browser
  • ▸Interactive widgets for GUI elements
  • ▸Hybrid code execution with Python and JavaScript

Learning Path

  • ▸Learn basic Python syntax
  • ▸Understand HTML structure and `<py-script>` tags
  • ▸Experiment with DOM manipulation from Python
  • ▸Use popular Python libraries in browser
  • ▸Build interactive browser-based applications

Practical Examples

  • ▸Print 'Hello World' in a webpage using Python
  • ▸Manipulate text content of an HTML element
  • ▸Plot a Matplotlib chart directly in the browser
  • ▸Create interactive buttons and form inputs using Python
  • ▸Load CSV data and display results dynamically

Comparisons

  • ▸PyScript vs Flask/Django: PyScript is client-side, no server required.
  • ▸PyScript vs Brython: PyScript uses Pyodide for better package support.
  • ▸PyScript vs Jupyter Notebook: PyScript is browser-integrated web app, Jupyter is interactive notebook.
  • ▸PyScript vs JavaScript: PyScript allows Python code for web, JS is native.
  • ▸PyScript vs Anvil: Anvil provides full backend, PyScript is front-end focused.

Strengths

  • ▸No backend server required for Python code
  • ▸Enables Python developers to build web apps without JavaScript knowledge
  • ▸Great for education and rapid prototyping
  • ▸Supports large Python ecosystem in browser
  • ▸Interactive notebooks and demos possible directly on web pages

Limitations

  • ▸Performance slower than native JavaScript for heavy computation
  • ▸Limited access to OS-level resources
  • ▸Browser memory constraints may limit large datasets
  • ▸Some Python packages not fully supported in WebAssembly
  • ▸Not yet ideal for production-scale web applications

When NOT to Use

  • ▸Heavy computation or large-scale applications
  • ▸Server-side Python logic required
  • ▸Security-critical web apps
  • ▸Offline use without browser WebAssembly support
  • ▸Production-grade full-stack applications needing databases

Cheat Sheet

  • ▸<py-script> ... </py-script> - embed Python code
  • ▸<py-env> - declare required Python packages
  • ▸Element('id').element.innerHTML = 'text' - manipulate DOM
  • ▸async def function_name(): ... - async Python code
  • ▸import numpy as np - use external Python library

FAQ

  • ▸Can PyScript run offline? -> Only if resources are cached.
  • ▸Which browsers are supported? -> Modern browsers with WebAssembly.
  • ▸Can I use Python packages? -> Yes, via py-env and Pyodide support.
  • ▸Does PyScript replace backend frameworks? -> No, mostly front-end.
  • ▸Is PyScript suitable for education? -> Yes, great for interactive Python learning.

30-Day Skill Plan

  • ▸Week 1: Run simple Python scripts in browser
  • ▸Week 2: Manipulate HTML elements with Python
  • ▸Week 3: Create interactive widgets and buttons
  • ▸Week 4: Integrate data visualization (Matplotlib/Plotly)
  • ▸Week 5: Build small interactive apps and demos

Final Summary

  • ▸PyScript enables Python to run directly in browsers.
  • ▸It integrates Python with HTML, CSS, and JavaScript.
  • ▸Supports interactive visualizations and widgets.
  • ▸Runs client-side using WebAssembly and Pyodide.
  • ▸Ideal for education, prototyping, and web-based Python demos.

Project Structure

  • ▸HTML file as entry point
  • ▸Optional linked Python scripts
  • ▸`<py-script>` tags contain main Python logic
  • ▸PyScript packages declared in `py-env` section
  • ▸Output rendered in browser DOM or interactive widgets

Monetization

  • ▸Educational course content
  • ▸Interactive demos for paid workshops
  • ▸Prototyping tools for client presentations
  • ▸Embedding interactive Python in blogs or documentation
  • ▸Open-source community contributions

Productivity Tips

  • ▸Start with small interactive examples
  • ▸Load only necessary Python packages
  • ▸Test DOM interactions incrementally
  • ▸Use async for smooth browser updates
  • ▸Organize HTML, CSS, and Python for maintainability

Basic Concepts

  • ▸`<py-script>` tag runs embedded Python code.
  • ▸Python can interact with DOM elements using `Element` API.
  • ▸Event listeners and callbacks work similar to JavaScript.
  • ▸Python packages can be imported dynamically using `py-env`.
  • ▸Python code executes asynchronously in browser sandbox.

Official Docs

  • ▸https://pyscript.net/
  • ▸https://github.com/pyscript/pyscript

More Pyscript Typing Exercises

Hello World in PyScriptPyScript Variables ExamplePyScript If Statement ExamplePyScript Loop ExamplePyScript Function ExamplePyScript Input ExamplePyScript Math ExamplePyScript List ExamplePyScript Conditional Loop Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher